MartinScharrer / standalone

A LaTeX class and package is provided which allows LaTeX diagrams or other LaTeX code to be compiled standalone or as part of a main document. Special support for pictures with beamer overlays is also provided.
LaTeX Project Public License v1.3c
9 stars 2 forks source link

Support for derived pictures using `conditionally` environment #11

Open Flowdalic opened 6 months ago

Flowdalic commented 6 months ago

I've asked on tex.stackexchange about Modular and re-usable (tikz) pictures. One interesting answer, courtesy of cfr, included the definition of a new conditionally enviornment:

\ExplSyntaxOn
\bool_new:N \l_conditionally_bool
\bool_set_false:N \l_conditionally_bool
\keys_define:nn { cond }
{
  enable .clist_set:N = \l_conditionally_clist,
  enable .initial:n = ,
  enable .default:n = ,
  standalone ~ options .tl_set:N = \l_cond_standalone_tl,
  standalone ~ options .default:n = ,
  standalone ~ options .initial:n = {mode = tex},
}
\NewDocumentEnvironment { conditionally } { m +b }
{
  \clist_if_in:NnT \l_conditionally_clist { #1 }
  { \bool_set_true:N \l_conditionally_bool } 
  \clist_if_in:NnT \l_conditionally_clist { * }
  { \bool_set_true:N \l_conditionally_bool } 
  \bool_if:NT \l_conditionally_bool { #2 }
}{  }
…

and a new includestandalonecond command

…
\NewDocumentCommand \includestandalonecond { O {} m }
{
  \group_begin:
    \keys_set:nn { cond } { #1 }
    \cond_includestandalone:Vn \l_cond_standalone_tl { #2 }
  \group_end:
}
\cs_new_protected:Nn \cond_includestandalone:nn
{
  \includestandalone [ #1 ] { #2 }
}
\cs_generate_variant:Nn \cond_includestandalone:nn { Vn }
\ExplSyntaxOff

As an example on how those are used, consider the following modular tikz picture:

\begin{filecontents}[overwrite]{\jobname-fig.tex}
\documentclass{standalone}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}  
  \pgfmathsetmacro{\myRadius}{2} 
  \coordinate (loop-start) at (-170:\myRadius);  
  \draw[->,line width=7pt,]
  (loop-start)
  arc (-170:170:\myRadius)
  coordinate[pos=0.75] (detour-start)
  coordinate[pos=0.25] (detour-end)
  coordinate[pos=0.27] (detour-end-2)
  ;  
  \begin{conditionally}{detour}
    \draw[
    ->,
    line width=3pt,
    red,
    ] (detour-start) to[out=160, in=220, distance=6cm] (detour-end);
  \end{conditionally}  
  \begin{conditionally}{longer-detour}
    \draw[
    ->,
    line width=3pt,
    red,
    ] (detour-start) to[out=45, in=-30, distance=10cm] (detour-end-2);
  \end{conditionally}
\end{tikzpicture}
\end{document}
\end{filecontents}

which can be included in the main tex file, for example, like this:

\includestandalonecond[enable=*]{\jobname-fig}

\includestandalonecond[enable=detour]{\jobname-fig}

\includestandalonecond[enable={detour,longer-detour}]{\jobname-fig}

this is great and achieves exactly what I want.

However, note that includestandalonecond set mode=tex. Otherwise, if, for example, mode=buildnew, then standalone would include a wrong version of the cached picture.

Therefore, this is a feature request to

  1. incooperate the enabled= feature of includestandalonecond into includestandalone, while additionally,
  2. make includestandalone use the values of enabled= to create and selected the cached version of a picture

I think 2. just meas that on \includestandalone[enabled=detour,shortcut]{foo}, standalone should use foo-detour-shortcut.pdf as name for the cached pdf.

I am very much like the API and functionality that was created here and I hope you consider adding support for it to the standalone package. Then we just have to find a place where conditionally is defined (maybe a new ctan package?).