12

I'm a complete newbie but have a little background in programming. I've been looking at a MWE located here provided by Yiannis Lazarides.

In the macro definition, we have

%% add an image to the DB, use the LaTeX macro
%% \g@addto@macro to store them at the DB location
\def\addtoDB#1#2{%
    \ifx\@empty#1
        \g@addto@macro{#1}{#2}
    \else
        \g@addto@macro#1{,#2}
    \fi
}

What is the difference between \g@addto@macro{#1}{#2} and \g@addto@macro#1{,#2} ?

  • Welcome to TeX.SX! I believe that the macro is aimed to build a comma separated list of items. When the first item is added, no comma is wanted. – egreg Jan 2 '14 at 20:18
  • @egreg Did you mean 'when the first item is empty'? – cfr Jan 2 '14 at 20:33
  • @cfr No, it's what I wrote: #1 is a macro name, that corresponds to the list to which some item has to be added. – egreg Jan 2 '14 at 21:15
  • @egreg Ah, now I see. Thanks. It is when the first argument is empty, where the first argument is the existing list... – cfr Jan 2 '14 at 21:22
  • @cfr If the first argument is empty, \@empty would be compared to \g@addto@macro, resulting in a disaster. – egreg Jan 2 '14 at 21:25
6

The macro is aimed to incrementally build a list macro; in the document you find something like

\def\alist{fig167,fig168,fig169,fig176,%
  fig180,fig181,fig182,fig183,fig185,fig186,fig187,fig188}

Well, one can obtain the same result by

\newDB{alist}

and then

\addtoDB\alist{fig167}
\addtoDB\alist{fig168}
\addtoDB\alist{fig169}
\addtoDB\alist{fig176}

and so on.

At the first \addtoDB call, the macro \alist defined by \newDB is still empty, so the conditional is true and

\g@addto@macro\alist{fig167}

is executed. At the second macro call, \alist is not empty, so

\g@addto@macro\alist{,fig168}

is executed, resulting in \alist expanding to fig167,fig168. And so on for the successive calls.

2

In this particular case I think the two uses of \g@addto@macro are meant to be the same apart from the extra comma prepended to the second argument in the second case. In fact since the first argument of \g@addto@macro is meant to be simply the name of a macro, the brackets of the first case are not strictly necessary. However, for reasons we'll see in a second, they are a good precaution and, in my opinion, should be used in the second case as well.

To illustrate the difference they make in general (when the first argument is not necessarily a macro name) let's consider the following example:

\documentclass{article}

\def\test#1#2{this #1 is #2 a test}
\def\testA#1#2{A \test{#1}{#2} A}
\def\testB#1#2{B \test#1{#2} B}

\begin{document}
 \testA{{1}{2}}{3}

 \testB{{1}{2}}{3}
\end{document}

The \test macro takes two arguments, \testA uses \test as in your first case (with brackets around #1) while \testB uses \test as in your second case.

In the body we use \testA and \testB with the same two arguments: #1 = {1}{2} and #2 = 3.

Since \testA uses brackets around #1, it expands to A \test{{1}{2}}{3} A, as expected, binding {1}{2} to the first argument of \test. \testB, instead, expands to B \test{1}{2}{3} B which means that \test will get 1 as first argument and 2 as second argument; {3} is not consumed by \test and gets typeset after \test is expanded.

Hence the output is

A this 12 is 3 a test A

B this 1 is 2 a test3 B

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.