0

I have the following example. It's magic because defining a macro ones (\ParametersAGAaAA and \ParametersAGAa) seems to interact to each other. Is this because \ParametersAGAa is part of the string \ParametersAGAaAA? \PutLongTableRows is resetted through \renewcommand{\PutLongtableRows}{} each time before the result is written to \Parameters...

\documentclass{article}
\usepackage{longtable}
\usepackage{listofitems}

\newcommand\PutLongtableRows{}
\makeatletter
\newcommand\AddLongtableRow[2][;]{%
    \g@addto@macro\PutLongtableRows{\@gobble}%
    \setsepchar{#1}% LISTOFITEMS SEP CHAR
    \readlist\z{#2}% READ THE LIST INTO \z
    \foreachitem\tmp\in\z{% LISTOFITEMS DO LOOP
        \expandafter\g@addto@macro\expandafter\PutLongtableRows
        \expandafter{\expandafter&\tmp}%
    }%
    \g@addto@macro\PutLongtableRows{\\\hline}%
}

\makeatother

\begin{document}
    \renewcommand{\PutLongtableRows}{}
    \AddLongtableRow{5,1; 5,2}
    \AddLongtableRow{5,3; 5,4}
    \newcommand{\ParametersAGAa}{\PutLongtableRows}

    \renewcommand{\PutLongtableRows}{}
    \AddLongtableRow{5,9; 5,8    ;5,7   }
    \AddLongtableRow{5,6; 5,5    ;5,4}
    \newcommand{\ParametersAGAaAA}{\PutLongtableRows}   

    \begin{longtable}{p{2cm}|p{3cm}}
        \caption{Tab 1}
        \\\hline
        \ParametersAGAa
        5,5 &85,6 \\\hline
    \end{longtable}

    \begin{longtable}{p{2cm}|p{3cm}|p{3cm}}
        \caption{Tab 2}
        \\\hline
        \ParametersAGAaAA
        5,3 &5,2 &5,1 \\\hline
    \end{longtable}
\end{document}

The first table show up nonsense since the \ParametersAGAaAA came up to be written also to \ParametersAGAa. But there was no command to do so. Maybe I have a misunderstanding - however \ParametersAGAaAA and \ParametersAGAa should be independent from each other? Why is \ParametersAGAa overwritten with \ParametersAGAaAA

2
  • 1
    TeX offers a macro expansion mechanism. With the first \newcommand{\ParametersAGAa}{\PutLongtableRows} you are not giving \ParametersAGAa the present value of \PutLongtableRows but simply instructing TeX to replace ParametersAGAa by \PutLongtableRows once it finds it.
    – campa
    Feb 14, 2020 at 10:07
  • Thx, knowing this it absolutely makes sense!
    – konsens
    Feb 14, 2020 at 19:38

1 Answer 1

2

This is because macro defined by \newcommand expands its definition during the macro are used, no during defining. So when you use \ParametersAGAa it is equal last definition of\PutLongtableRows, that respects \ParametersAGAaAA.

To avoid that use internal TeX command \let instead of \newcommand:

\let\ParametersAGAa\PutLongtableRows

and

\let\ParametersAGAaAA\PutLongtableRows

Of course, no any interaction between macros with similar names is possible.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .