# Inverting a boolean to create a list of alternate items

Does anyone know how to invert a boolean in LaTeX? It looks like once you are inside a logical test, resetting the logical value has no effect. Using the etoolbox package, I tried

\foreach \n in {1,2,3,4,5,5,6,7,8,9,e}{
\iftoggle{pauper}{
True
\togglefalse{pauper}
}{
False
\toggletrue{pauper}
}
}


It should produce True False True False True False True False True False, but produces only a string of ten True. How can I make it come out True False True False ....

If this problem is solved, I can solve a far larger problem. It might be that the larger problem has a more elegant solution I have not though of, so I state it as well:

The context is a textbook, where I write the assignment and the answer together, and then call the assignment file twice. The first time, the boolean value assignment is true, the second time it is false.

\begin{enumerate}
\item \ifthenelse{assignment}{$2+2$}{$4$}
...
\end{enumerate}


Now, I would like for the answers section to have answers to only the odd numbered problems, except for a teacher's version which would have all. So the \item should ideally be something like this

  % if assignment=false, then ...
% if item counter is odd:
\item \ifthenelse{assignment}{$2+2$}{$4$}
% but if item counter is odd:
\stepcounter{enumi}
% if assignment=true, then, regardless of counter:
\item \ifthenelse{assignment}{$2+2$}{$4$}


I have tried

 \ifthenelse{\boolean{assignment}}{$2+2$}{
\ifthenelse{\boolean{oddItem}}{
\item $4$ \setboolean{oddItem}{false}
}{
\stepcounter{enumi} \setboolean{oddItem}{true}
}
}


But the boolean stays the same value. I have tried several combinations, and also setting it outside the inner ifthenelse like this,

\setboolean{oddItem}{\not\boolean{oddItem}}


but to no avail. Same with toggle instead of boolean.

## 1 Answer

First, the body of \foreach is in a group so the effect of local commands is restricted to a single iteration. What it means for your first example is that \toggletrue/\togglefalse only have an effect within the same iteration. To fix this, as suggested in the etoolbox manual, you can prefix them with \global which extends their effect beyond groups:

\foreach \n in {1,2,3,4,5,5,6,7,8,9,e}{
\iftoggle{pauper}{
True
\global\togglefalse{pauper}
}{
False
\global\toggletrue{pauper}
}
}


shoud give the desired result.

Without further context, a simpler solution can be provided by using LaTeX \newif and \ifodd without the need for etoolbox and ifthenelse.

First you can create a new conditional with \newif\ifassignment and set it to true or false using \assignmenttrue \assignmentfalse. Then, if you have a counter (either \theenumi or \n in your \foreach) you can check if it is odd by using \ifodd.

\begin{itemize}
\foreach\n in {1,...,10}{
\item Problem \n
\ifassignment%
\ifodd\n%
\relax%
\else%
-- Solution \n%
\fi
\else%
-- Solution \n%
\fi%
}
\end{itemize}


The ugly part is that the solution has to be repeated twice. THere are multiple ways to solve this but the way I am presenting is by introducing a macro \printproblem that encapsulates this logics:

\newcommand{\printproblem}{
\item #1%
\ifassignment%
\ifodd\n%
-- #2%
\else%
\relax%
\fi
\else%
-- #2%
\fi%
}


An then you can do

\begin{itemize}
\foreach\n in {1,...,10}{
\printproblem{Problem \n}{Solution \n}
}
\end{itemize}


If you are not using an actual \foreach loop, you can use \theenumi instead of \n:

\newcommand{\solution}{
\ifassignment%
\ifodd\theenumi%
-- #1%
\else%
\relax%
\fi
\else%
-- #1%
\fi%
}


and in the body:

\begin{enumerate}
\item Problem 1 \solution{Solution 1}
\item Problem 2 \solution{Solution 2}
\item Problem 3 \solution{Solution 3}
\item Problem 4 \solution{Solution 4}
\item Problem 5 \solution{Solution 5}
\end{enumerate}

• I am not using the foreach loop, so the latter is more like what I am doing. To be precise, I have defined two boolean variables, that turn on and off the visibility of two environments, oText and oSolution. An assignment looks like this: \item Description \oText{$2+2$}\oSolution{$4$} The file is called twice: first with Text on, Solution off. Then later with Solution on, Text off. I will try to tweak your solution command, so that it's on when Text is, and has \item inside. Then item calls will be like this: \solution{ \item Description \oText{$2+2$}\oSolution{$4$} } Look OK? – Svein Olav Nyberg May 1 '16 at 11:04
• If you make progress but find other problems you can always post a new question. I believe the first part of my answer addresses your question quite directly. If this answered your question please consider marking it as an accepted answer. – Bordaigorl May 1 '16 at 16:10