6

I want to use etoolbox's toggles to achieve the following: In the preamble a toggle is created, during typesetting the document its value might change and at the end I want to write the value to the aux file to access it next time. However, setting toggles in the aux file does not seem to work, as the following MWE illustrates:

\documentclass{minimal}
\usepackage{etoolbox}
\newtoggle{testtoggle}

\begin{document}
\makeatletter
\immediate\write\@auxout{\noexpand\toggletrue{testtoggle}}
\makeatother
\iftoggle{testtoggle}{true}{false}
\end{document}

After running this, the aux file contains

\relax 
\toggletrue {testtoggle}

as it should. However, in the document created it says "false" instead of "true". How can this be fixed?

The same problem also occurs when I use \newif statements instead of toggles.

1
  • 1
    Don't use the minimal class!
    – user31729
    Commented Feb 6, 2019 at 9:08

1 Answer 1

6

The .aux file is (usually) read in a group, so all changes that you want to propagate to the entire document need to be applied globally. In this case

\immediate\write\@auxout{\noexpand\global\noexpand\toggletrue{testtoggle}}

should be enough so that the .aux file reads

\relax 
\global \toggletrue {testtoggle}

In case it is not easily possible to append \global to a command (or to switch to a \global version like \gdef instead of \def) or in case you need to do this more often you might want to consider writing an auxiliary function for the .aux file.

\documentclass{article}
\usepackage{etoolbox}
\newtoggle{testtoggle}

\begin{document}
\makeatletter
\immediate\write\@auxout{\noexpand\global\noexpand\toggletrue{testtoggle}}
\makeatother
\iftoggle{testtoggle}{true}{false}
\end{document}

true

You must log in to answer this question.

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