0

I use the toggles of the etoolbox package in my document. However, the document is long and gets modified a lot so sometimes the current state of the toggle is not clear when editing somewhere in the middle.

I thought it would be nice to define an environment that ensures:

  1. the value of the toggle as I want it
  2. reverts the value of the toggle at its end (so that the rest of the code is not effected by my edit)

I came up with this pseudocode/MWE that of course does not work but illustrates the idea:

\documentclass{article}

\usepackage{etoolbox}

\providetoggle{sometoggle}

\newenvironment{sometoggle}[1]
{%
    % oldtogglevalue = \gettoggle{sometoggle}
    \settoggle{sometoggle}{#1}
}{%
    % \settoggle{sometoggle}{oldtogglevalue}
    \ignorespacesafterend{} % ... this is a hidden second question as it does not work
}


\begin{document}

    \begin{sometoggle}{true}
        code that needs sometoggle to be true
        \begin{sometoggle}{false}
            code that need sometoggle to be false%
        \end{sometoggle}
    \end{sometoggle}

\end{document}

The arbitrary nesting of the new environment is especially difficult I feel.

Any ideas? Ty for reading, Franz

2
  • You don't need to revert the toggle at the end of the environment since LaTeX will put the whole environment in a group. Unless you alter values globally the old value will be restored automatically. Don't use {} after \ignorespacesafterend. You're putting an extra space at the end of your environment. ({}% would help but you don't need that empty group to begin with.) Sep 25 at 16:53
  • Ty ... about the \ignorespaceafterend{} does {} really add a space. I add the {} behind many command since my linter chktex complains otherwise.
    – Franz
    Sep 26 at 21:20

1 Answer 1

2

Your code seems to work just fine.

\documentclass{article}
\usepackage{etoolbox}

\providetoggle{sometoggle}
\newenvironment{sometoggle}[1]
{%
    \settoggle{sometoggle}{#1}
}{}

\newcommand{\togglevalue}{``TOGGLE IS \iftoggle{sometoggle}{\textbf{TRUE}}{\textbf{FALSE}}''}

\begin{document}
    No env: \togglevalue
    
    \begin{sometoggle}{true}
        In env: \togglevalue
        
        \begin{sometoggle}{false}
            In second env: \togglevalue
        \end{sometoggle}
        
        In env: \togglevalue
    \end{sometoggle}
    
    No env: \togglevalue
\end{document}

produces

enter image description here

1
  • .... stupid me. I did not even tried my code just assumed it would not work. Ty ..
    – Franz
    Sep 26 at 21:19

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.