0

I've spent a few hours trying to debug the following. I did manage to fix it, but mostly by chance. Can someone enlighten me on what actually happened?

Initial setup

I'm using \usetikzlibrary{external}. I \input the same file multiple times, which generates a pgfplots graph. Sometimes I want to display a secondary axis, so this file looks something like this:

\tikzset{external/export next=false}  % since I'm running it multiple times
\begin{tikzpicture}

% Primary axis and charts …

% Optional secondary axis and charts \ifSecondaryAxis \begin{axis} … \end{axis} % line 58 \fi

\end{tikzpicture}

This was creating nasty errors for the following pictures which didn't have the \tikzset{external/export next=false} directive. This was extremely hard to trace back, since the breaking code effect was observed on later figures externalisation. For example, I was getting the following error on a random spiral-pred_dec.tex:

! Package tikz Error: Sorry, the system call 'pdflatex -shell-escape -halt-on-e rror -interaction=batchmode -jobname "pdf-tikz-figs/spiral-pred_dec" "\def\tikz externalrealjob{main}\input{main}"' did NOT result in a usable output file 'pdf -tikz-figs/spiral-pred_dec' (expected one of .pdf:.jpg:.jpeg:.png:).

Commenting out spiral-pred_dec.tex was giving an error on the following picture, and so on.

What fixed everything (and here comes the question) has been adding a pair of curly brackets around the axis environment, so to have something like:

% Optional secondary axis and charts
\ifSecondaryAxis
{\begin{axis}
…
\end{axis}}
\fi

Question

What does adding a pair of curly brackets around an environment accomplish, when dealing with TeX conditional rendering?

Additional debugging information

) (./ellipse-figs/E23.tex)
! Incomplete \ifx; all text was ignored after line 58.
<inserted text> 
                \fi 
l.131     \input{ellipse-figs/E23}
                                  % 
Here is how much of TeX's memory you used:
…

! ==> Fatal error occurred, no output PDF file produced!

And line 58 corresponds to \end{axis} in the first code snippet, one line above the \fi.

Atcold
  • 1,727
  • Perhaps there's sth useful in pdf-tikz-figs/spiral-pred_dec.log. – muzimuzhi Z Apr 21 '22 at 01:01
  • Without a minimal working example it's quite impossible to tell... but generally speaking you can't arbitrarily include command A inside command B, but TeX evaluates from outside to inside so command B will "see" command A and might do something different. – user202729 Apr 21 '22 at 05:53
  • @muzimuzhiZ, I've added the content of the log relative to the breaking part. @user202729, I could try to put together a minimal working example, but I've already found a fix. So, I'm here just to get some pointers about the behaviour of conditionals interacting with arbitrary environments, just in case there's something obvious I'm missing. – Atcold Apr 21 '22 at 16:59
  • How about \makeatletter \newcommand\ifSecondaryAxisT{\ifSecondaryAxis \expandafter\@firstofone \else \expandafter\@gobble \fi} \makeatother then \ifSecondaryAxis{\begin{axis} ... \end{axis}}? – muzimuzhi Z Apr 21 '22 at 17:13
  • For one, if there's some token with meaning (← read TeXbook etc. if the terms doesn't make sense) equal to \fi or \iftrue etc. then it may mess up the expansion. (I consider this unlikely) Then, the conditional leave the \fi token after, which might be problematic if the environment for some weird reason look after the token list. (see ^ && macros - What do @firstoftwo and @secondoftwo do? - TeX - LaTeX Stack Exchange) for explanation on this point) – user202729 Apr 22 '22 at 00:46
  • Look, remote debugging isn't going to work, but at least give us the more complete traceback (enable error context lines macros - What does \errorcontextlines do? - TeX - LaTeX Stack Exchange) – user202729 Apr 22 '22 at 00:47
  • Another possibility would be that it undo some local assignment etc. (read e.g. "the grouping mechanism" in TeX by topic) – user202729 Apr 22 '22 at 00:48

0 Answers0