Skip to content

Commit

Permalink
v2.7: remove leading spaces in placeholderfromcode
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasBora committed Mar 11, 2024
1 parent 763ee38 commit 406fb68
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
Binary file modified doc/robust-externalize.pdf
Binary file not shown.
31 changes: 28 additions & 3 deletions doc/robust-externalize.tex
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
{\Large\bfseries Cache anything (\tikzname, tikz-cd, python…),\\in a robust, efficient and pure way.}

\vspace{1em}
{Léo Colisson \quad Version 2.6}\\[3mm]
{Léo Colisson \quad Version 2.7}\\[3mm]
{\href{https://github.com/leo-colisson/robust-externalize}{\texttt{github.com/leo-colisson/robust-externalize}}}
\end{center}

Expand Down Expand Up @@ -1711,8 +1711,8 @@ \subsubsection{Setting a value to a placeholder}


\begin{pgfmanualentry}
\extractenvironement{PlaceholderFromCode}\opt{*}\marg{name placeholder}\@@
\extractenvironement{SetPlaceholderCode}\opt{*}\marg{name placeholder}\@@
\extractenvironement{PlaceholderFromCode}\opt{*}\opt{\oarg{style}}\marg{name placeholder}\@@
\extractenvironement{SetPlaceholderCode}\opt{*}\opt{\oarg{style}}\marg{name placeholder}\@@
\pgfmanualbody

These two (aliased) environments are useful to set a verbatim value to a given placeholder: the advantage is that you can put inside any code, including \LaTeX{} comments, the downside is that you cannot use it inside macros and some environments (so you typically define it before the macros and call it inside).
Expand Down Expand Up @@ -1758,6 +1758,27 @@ \subsubsection{Setting a value to a placeholder}
}
Note that of course, you can define a placeholder before a macro and call it inside (explaining how we can generate this documentation).

You can use the optional style (only after 2.7; note that it defaults to |defaultPlaceholderFromCodeStyle|, which contains by default |remove leading spaces|) to apply some operations on the style. This way, by default, it will remove any leading indentation, allowing you, for instance, to indent your python code based on the surrounding LaTeX environment):
\begin{codeAndResult}
\begin{PlaceholderFromCode}{__TMP_MAIN_CONTENT__}
# See, you can indent this code with two spaces without disturbing python (that expects no indentation)
import matplotlib.pyplot as plt
year = [2014, 2015, 2016, 2017, 2018, 2019]
tutorial_count = [39, 117, 111, 110, 67, 29]
plt.plot(year, tutorial_count, color="#6c3376", linewidth=3)
plt.xlabel('Year')
plt.ylabel('Number of futurestud.io Tutorials')
plt.savefig("__ROBEXT_OUTPUT_PDF__")
\end{PlaceholderFromCode}

\fbox{\begin{CacheMeNoContent}
python,
set includegraphics options={width=.8\linewidth},
set main content={__TMP_MAIN_CONTENT__},
\end{CacheMeNoContent}}
\end{codeAndResult}


Note that the star and no import version does NOT import the placeholder it the main group (unless you try to optimize the compilation time you should not need it, but see \cref{sec:importSystem} for details).
\end{pgfmanualentry}

Expand Down Expand Up @@ -4911,6 +4932,10 @@ \section{TODO and known bugs:}
\section{Changelog}
\begin{itemize}
\item v2.7:
\begin{itemize}
\item apply |remove leading spaces| by default also in |placeholderFromCode| and |PlaceholderPathFromCode|, and allow the user to customize the style. This produces a very minor backward incompatibility as it might disturb code where all lines start with a space.
\end{itemize}
\item v2.6:
\begin{itemize}
\item Added |\lenToCmNoUnit|
Expand Down
54 changes: 36 additions & 18 deletions robust-externalize.sty
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
\ProvidesPackage{robust-externalize}[2.6 Cache anything (tikz, latex, python) in a robust, efficient and pure way.]
\ProvidesPackage{robust-externalize}[2.7 Cache anything (tikz, latex, python) in a robust, efficient and pure way.]
% todo:
% change order argument replace from list, it is hard to read this way

Expand Down Expand Up @@ -653,7 +653,7 @@ if __name__ == '__main__':

\NewDocumentCommand{\robExtShowPlaceholder}{sm}{
\cs_if_exist:cTF {l__robExt_placeholder_#2_str} {
\message{Placeholder ~ #2 ~ contains:^^J~ \use:c{l__robExt_placeholder_#2_str}}
\message{Placeholder ~ #2 ~ contains:^^J \use:c{l__robExt_placeholder_#2_str}}
}{
\message{Placeholder ~ #2 ~ does ~ not ~ exist.}
}
Expand Down Expand Up @@ -932,6 +932,18 @@ if __name__ == '__main__':
}
\let\addBeforePlaceholderNoImport\robExtAddBeforePlaceholderNoImport

\ExplSyntaxOff

% except inside setplaceholderfromcode, we want to modify by default the main content orig placeholder
\def\robExtCurrentPlaceholderName{__ROBEXT_MAIN_CONTENT_ORIG__}

\pgfqkeys{/robExt}{
defaultPlaceholderFromCodeStyle/.style={
remove leading spaces,
},
}

\ExplSyntaxOn

% Usage:
% \begin{placeholderFromCode}{HELPERFUNCTION}
Expand All @@ -940,8 +952,8 @@ if __name__ == '__main__':
% \end{placeholderFromCode}
% HELPERFUNCTION will contain at the end "def ..."
% This environment cannot be placed inside any other macro/align/...
\NewDocumentEnvironment{RobExtPlaceholderFromCode}{sm}{%
\checkIfPlaceholderNameIsLegal{#2}%
\NewDocumentEnvironment{RobExtPlaceholderFromCode}{sO{defaultPlaceholderFromCodeStyle}m}{%
\checkIfPlaceholderNameIsLegal{#3}%
% % debug part
% \str_set:Nn \l_test_str {#1}
% \show\l_test_str
Expand All @@ -960,14 +972,17 @@ if __name__ == '__main__':
\ior_str_map_inline:Nn \g__robExt_read_ior {%
\str_gput_right:Nx \l__robExt_tmp_str {\tl_to_str:n{##1}^^J}%
}%
\str_set_eq:cN {l__robExt_placeholder_#2_str} \l__robExt_tmp_str%
\str_set_eq:cN {l__robExt_placeholder_#3_str} \l__robExt_tmp_str%
%% We apply the style, useful for instance to remove indentation
\def\robExtCurrentPlaceholderName{#3}%
\pgfqkeys{/robExt}{#2}%
\IfBooleanTF {#1} {} {
\robExtAddPlaceholderToList{#2}
\robExtAddPlaceholderToList{#3}
%% Otherwise they will be lost when the environment ends
\robExtKeepaftergroup{l__robExt_placeholder_group_main_seq}%
}
}%
%% for other variable
\robExtKeepaftergroup{l__robExt_placeholder_#2_str}%
\robExtKeepaftergroup{l__robExt_placeholder_#3_str}%
}%
\let\PlaceholderFromCode\RobExtPlaceholderFromCode
\let\endPlaceholderFromCode\endRobExtPlaceholderFromCode
Expand Down Expand Up @@ -1050,7 +1065,7 @@ if __name__ == '__main__':
%% \end{PlaceholderPathFromCode}
%% This will copy "some code" in the cache, and set MYLIBPATH to the name of the file in the cache like
%% MYLIBPATH = robExt-abc.py
\NewDocumentEnvironment{RobExtPlaceholderPathFromCode}{sO{}m}{
\NewDocumentEnvironment{RobExtPlaceholderPathFromCode}{sO{}O{defaultPlaceholderFromCodeStyle}m}{
\XSIMfilewritestart*{\jobnameNoQuotes-robExt-tmp-file-you-can-remove.tmp}
}{
\XSIMfilewritestop
Expand All @@ -1068,13 +1083,16 @@ if __name__ == '__main__':
\iow_now:NV \g__robExt_write_iow \l__robExt_tmp_contain_file_str
\iow_close:N \g__robExt_write_iow
%% sets the template name to the relative path to the file
\str_set:cx { l__robExt_placeholder_#3_str } {\robExtPrefixFilename\l__robExt_tmp_filename_no_prefix_str}
\str_set:cx { l__robExt_placeholder_#4_str } {\robExtPrefixFilename\l__robExt_tmp_filename_no_prefix_str}
%% We apply the style, useful for instance to remove indentation
\def\robExtCurrentPlaceholderName{#4}%
\pgfqkeys{/robExt}{#3}%
\IfBooleanTF {#1} {} {
\robExtAddPlaceholderToList{#3}
\robExtAddPlaceholderToList{#4}
\robExtKeepaftergroup{l__robExt_placeholder_group_main_seq}
}
%% Otherwise they will be lost when the environment ends
\robExtKeepaftergroup{l__robExt_placeholder_#3_str}
\robExtKeepaftergroup{l__robExt_placeholder_#4_str}
}
\let\PlaceholderPathFromCode\RobExtPlaceholderPathFromCode
\let\endPlaceholderPathFromCode\endRobExtPlaceholderPathFromCode
Expand Down Expand Up @@ -2618,7 +2636,7 @@ if __name__ == '__main__':
remove placeholders/.style={
remove placeholder/.list={#1},
},
set main content/.style={
set main content/.style={
set placeholder={__ROBEXT_MAIN_CONTENT_ORIG__}{#1}
},
copy placeholder/.code 2 args={\robExtCopyPlaceholder{#1}{#2}},
Expand Down Expand Up @@ -2661,19 +2679,19 @@ if __name__ == '__main__':
placeholder replace in place eval/.code n args={3}{\robExtPlaceholderReplaceInplaceEval{#1}{#2}{#3}},
placeholder prepend all lines/.code 2 args={\robExtPlaceholderPrependAllLines{#1}{#2}},
prepend all lines/.style={
placeholder prepend all lines={__ROBEXT_MAIN_CONTENT_ORIG__}{#1},
placeholder prepend all lines={\robExtCurrentPlaceholderName}{#1},
},
placeholder remove spaces until/.code 2 args={\robExtPlaceholderRemoveSpacesUntil{#1}{#2}},
remove spaces until/.style={
placeholder remove spaces until={__ROBEXT_MAIN_CONTENT_ORIG__}{#1},
placeholder remove spaces until={\robExtCurrentPlaceholderName}{#1},
},
placeholder strictly remove spaces until/.code 2 args={\robExtPlaceholderRemoveSpacesUntil{#1}[0]{#2}},
strictly remove spaces until/.style={
placeholder remove spaces until nospace={__ROBEXT_MAIN_CONTENT_ORIG__}{#1},
placeholder remove spaces until nospace={\robExtCurrentPlaceholderName}{#1},
},
placeholder remove leading spaces/.code={\robExtPlaceholderRemoveLeadingSpaces{#1}},
remove leading spaces/.code={\robExtPlaceholderRemoveLeadingSpaces{__ROBEXT_MAIN_CONTENT_ORIG__}},
remove leading spaces if not disabled/.code={\ifdefined\robExtDoNotRemoveLeadingSpaces\else\robExtPlaceholderRemoveLeadingSpaces{__ROBEXT_MAIN_CONTENT_ORIG__}\fi},
remove leading spaces/.code={\robExtPlaceholderRemoveLeadingSpaces{\robExtCurrentPlaceholderName}},
remove leading spaces if not disabled/.code={\ifdefined\robExtDoNotRemoveLeadingSpaces\else\robExtPlaceholderRemoveLeadingSpaces{\robExtCurrentPlaceholderName}\fi},
do not remove leading spaces/.code={\def\robExtDoNotRemoveLeadingSpaces{}},
% Interface to set template
set template/.style={
Expand Down

0 comments on commit 406fb68

Please sign in to comment.