I often find myself wanton of a LaTeX utility with the following semantics:
In a file path/foo.tex
:
\OneTimeOnly{ % Code that is inserted only once, the first time the file is included. ... } % Code that is inserted with each inclusion. ... \OnlyFor{instance-A}{ % Code that is inserted only when file is included with 'instance-A' argument. ... } \OnlyFor{instance-B,instance-C}{ % Code that is inserted only when file is included with either 'instance-B' % or 'instance-C' arguments. ... } % More code inserted with each inclusion. ... ...
In the main .tex file:
\begin{document} ... % Inserts foo code in 'instance-A' mode. \InsertModule{path/foo}{instance-A} ... % Inserts foo code in 'instance-B' mode. \InsertModule{path/foo}{instance-B} ... % Inserts foo code in 'instance-C' mode. \InsertModule{path/foo}{instance-C} ... \end{document}
That is, foo.tex
behaves like a command with a static one-time-only component (for initialization, etc.) and a single argument controlling which part(s) of the code are inserted. However, the code is directly injected and has no special scope (i.e. commands in foo.tex
would use and modify the same counters as the base file).
In addition to facilitating code reuse, the main feature I'm looking for here is simple, clean code in both the base file and foo.tex
. Something comparable to the example above, where all the logic, states, definitions, conditionals, key lookup, etc. have been spirited away to a class file, etc.
The ideal utility would also have the properties that:
setup in the base file is limited to an
\input
or\usepackage
command and (optionally) a single initialization command, e.g.\InitModule{path/foo}
multiple modules can be used in the same base file, e.g.
\InsertModule{path/foo}{instance-A} \InsertModule{another/longer/path/bar}{another-instance}
Each module retains its own persistent state for the\OneTimeOnly
command (presumably keyed to its unique file path). I realize this might not be possible in LaTeX, and an acceptable alternative is having to write either\DefineModule{path/foo}{ \OneTimeOnly{ ... } \OnlyFor{instance-A}{ ... } }
or\OneTimeOnly{path/foo}{ ... } \OnlyFor{instance-A}{ ... }
infoo.tex
.the parameter passed to the module (e.g.
instance-A
) can contain numbers and at least one of: hyphens, spaces, or underscoresthe parameter passed to the module is accessible within the module using a command or token, e.g.
\ForArg
or#1
. Even better: if the\InsertModule
command can append additional arguments accessible by the injected code, e.g.\InsertModule{path/foo}{instance-A}{\lambda}
, with the second argument accessible as\ForArg{2}
or#2
, etc.code injection can occur in math mode (this is entirely optional, but would be generally useful)
I realize this is a tall order, and I'd intended on coding such a utility myself, but the macro writing advice consistently given to programmers not intimately familiar with coding TeX macros, packages, etc. is: don't.
Is there an existing package that serves this function?
If not, can I appeal to any TeX guru out there to provide me with a class, etc. implementing this behaviour? I shall forever be indebted to you. :)