I finally developed from egreg's answer in my previous question ( Defining a macro with three optional arguments in the form \newmacro{a}{b}[c]{d}[e][f] and \newmacro{a}{b}[c]{d}*[f] ) While my original answer was about making a shorter code, I ended up with a ~190 lines package. For the curious you can check it here in github, and it is also documented here.
The macro works fine, except for a display mode I would like to achieve: I want
the macro to work inside tabular environments such as array
or align
:
that would allow to typeset several function definitions preserving some alignment.
However I found a problem.
Let's took a look of the following code:
\documentclass{article}
\def\tempi{0}
\def\tempii{1}
\newcommand\mymacro[1]{
\let\tempiii\tempi
\def\tempi{#1\tempiii}
\def\tempii{#1}
\mymacroi
}
\newcommand\mymacroi{\tempi & \tempii}
\begin{document}
\[
\begin{array}{cccc}
a & \mymacro{1} & b \\
\mymacro{2} & \mymacro{3}
\end{array}
\]
\end{document}
It gets this result:
Changing the definition of \mymacroi
\newcommand\mymacroi{\tempi , \tempii}
The new result is:
In the first code, the changes in \tempii
are not preserved after the tab character &
. The second one shows the modification of \tempii
(but does not align).
Using \begingroup
in \mymacro
:
\newcommand\mymacro[1]{
\begingroup
\let\tempiii\tempi
\def\tempi{#1\tempiii}
\def\tempii{#1}
\mymacroi
\endgroup
}
And, with the original \mymacroi
(the one with &
) I get an error.
So the problem is indeed trying to use &
from the macro, while preserving
changes local to my macro, while expanding these changes over several cells.
(In this MWE there is a circular reference which does not exist in my original macro.)
Update:
My original example was too simple to show some desired result, and given that my macro should work both inside tabular environment and non-tabular environments, I will mimic that in this MWE by redefining \mymacroi
. I copy only the document environment.
\begin{document}
\[
\begin{array}{cccccc}
a & \mymacro{7} & b & \tempi & \tempii \\
\mymacro{2} & \mymacro{3} & \tempi & \tempii
\end{array}
\]
\renewcommand\mymacroi{%
\edef\tmp{\tempi,\tempii}%
\tmp
}
\par Test 1 \(\mymacro{5};\tempi,\tempii\)
\par Test 2 \(\mymacro{4};\tempi,\tempii\)
\end{document}
The expected result is:
and