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.
pdf-tikz-figs/spiral-pred_dec.log. – muzimuzhi Z Apr 21 '22 at 01:01logrelative 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\makeatletter \newcommand\ifSecondaryAxisT{\ifSecondaryAxis \expandafter\@firstofone \else \expandafter\@gobble \fi} \makeatotherthen\ifSecondaryAxis{\begin{axis} ... \end{axis}}? – muzimuzhi Z Apr 21 '22 at 17:13\fior\iftrueetc. 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