0

I got stuck with the following piece of TeX:

\def\foo{A}
\def\bar{\foo}
\let\baz\bar
1: \baz
\def\foo{B}
\\
2: \baz
\\

\begin{array}[t]{r}
\def\foo{C}
3: \baz
\\
\def\foo{D}
4: \baz
\\
5: \baz
\end{array}

\begin{enumerate}
\def\foo{E}
\item[6:] \baz

\item[7:]
\def\foo{F}
\baz

\item[8:] \baz
\end{enumerate}

This piece of TeX is rendered as follows:

1: A
2: B

3: C
4: D
5: B

6: E
7: F
8: F

Now, lines 1 and 2 behave as expected, after redefining \foo, the new value is printed. However, when wrapped in an array, line 5 prints B where I would expect D. The macros work as expected when inside enumerate (lines 6-8).

How is it that line 5 prints as B instead of D and how to fix the definition of the macros? I presume there is something weird going on with the expansion inside array. I tried to change array for align but that manifests the same problem.

2
  • 1
    Each element of the array is group scoped. You need \gdef instead of \def to communicate outside of the group. – Steven B. Segletes Nov 6 '20 at 11:31
  • Blah, you are right, gdef works. Can you perhaps make this into a full answer and add some appropriate tex phrases? I seriously spent ages to try to figure this myself but it is very hard to google this. – fari Nov 6 '20 at 11:45
2

This is completely expected

\def\foo{A}     % now \foo expands to A
\def\bar{\foo}  % now \bar expands to \foo
\let\baz\bar    % now \baz expands to \foo
1: \baz         % we get \foo, then A
\def\foo{B}     % now \foo expands to B
\\
2: \baz         % we get \foo, then B
\\

\begin{array}[t]{r}
\def\foo{C}     % now \foo expands to C
3: \baz         % we get \foo, then C
\\              % end of cell, \foo reverts to B
\def\foo{D}     % now \foo expands to D
4: \baz         % we get \foo, then D
\\              % end of cell, \foo reverts to B
5: \baz         % we get \foo, then B
\end{array}

\begin{enumerate}
\def\foo{E}     % now \foo expands to E
\item[6:] \baz  % we get \foo, then E

\item[7:]
\def\foo{F}     % now \foo expands to F
\baz            % we get \foo, then F

\item[8:] \baz  % we get \foo, then F
\end{enumerate}

Cells in array form groups. It's not clear what you want to achieve, but if you want that the definition of \foo transcends groups, you need \gdef rather than \def.

In particular, what's the role of \bar? You never use it, except when defining \baz as an alias to it.

1
  • You are right, it the groups. I had no idea cells form groups neither did it pop to me in any related question when I was trying to find a fix. I was writing a macro that issues a newline and prepends some text to it,whcih I want to change mid-array. After Steven's previous comment above I ended up with: \def\baz{\gdef\foo{D}}, \def\qux{\gdef\foo{E}} and \def\NL{\newline\foo} – fari Nov 6 '20 at 12:14

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.