# Loop Multi-Contingency using etoolbox

I want to use the etoolbox package to manage some loop contingencies over a counting index. In particular, I'd like to do something different on the first and last times through the loop.

Now I can formulate ways to do that, for example in this MWE. In the first case, I use a nested \ifboolexpr whereas in the second example, I set up an arcane switching system for toggles. Both of these approaches accomplish the underlying goal...

\documentclass{article}
\usepackage{etoolbox}
\newcounter{index}%
\begin{document}
\def\LAST{10}

\noindent%
\setcounter{index}{0}%
\whileboolexpr{test {\ifnumcomp{\theindex}<{\LAST}}}{%
\stepcounter{index}%
\ifboolexpr{test {\ifnumcomp{\theindex}{=}{1}}}%
{NOT\\}%
{\ifboolexpr{test {\ifnumcomp{\theindex}{=}{\LAST}}}%
{NOT\\}%
{\theindex\\}%
}%
}

\newtoggle{flag}
\noindent%
\setcounter{index}{0}%
\togglefalse{flag}%
\whileboolexpr{test {\ifnumcomp{\theindex}<{\LAST}}}{%
\stepcounter{index}%
\ifboolexpr{test {\ifnumcomp{\theindex}{=}{\LAST}}}{%
\togglefalse{flag}}{}%OFF FOR LAST COLUMN
\iftoggle{flag}{\theindex\\}{NOT\\}%
\toggletrue{flag}% ON FOR COLUMNS 2 TO \LAST-1
}%
\end{document}


However, the documentation leads me to believe that I should be able to use a Boolean and or or to accomplish this contingency with a single \ifboolexpr. And yet, I have been unable to construct such a syntax to do so. Is there a way, using the etoolbox syntax, to perform a single "if" check to determine if this is "the first excursion OR the \LAST excursion" through the loop, or alternately, if this is "not the first excursion AND not the \LAST excursion" through the loop?

• And this is specific to an iteration over numerals, not random items, right? – Werner Oct 24 '13 at 2:41
• @Werner Yes, the iteration is a simple incremented index from 1 to a maximum value. – Steven B. Segletes Oct 24 '13 at 2:42

Here's a way to combine conditions:

\documentclass{article}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\newcounter{index}%
\begin{document}
\def\LAST{10}

First: \par
\noindent%
\setcounter{index}{0}%
\whileboolexpr{test {\ifnumcomp{\theindex}<{\LAST}}}{%
\stepcounter{index}%
\ifboolexpr{test {\ifnumcomp{\theindex}{=}{1}}}%
{NOT\ }%
{\ifboolexpr{test {\ifnumcomp{\theindex}{=}{\LAST}}}%
{NOT}%
{\theindex\ }%
}%
}

Second: \par
\newtoggle{flag}
\noindent%
\setcounter{index}{0}%
\togglefalse{flag}%
\whileboolexpr{test {\ifnumcomp{\theindex}<{\LAST}}}{%
\stepcounter{index}%
\ifboolexpr{test {\ifnumcomp{\theindex}{=}{\LAST}}}{%
\togglefalse{flag}}{}%OFF FOR LAST COLUMN
\iftoggle{flag}{\theindex\ }{NOT\ }%
\toggletrue{flag}% ON FOR COLUMNS 2 TO \LAST-1
}%

Third: \par
\noindent%
\setcounter{index}{0}%
\whileboolexpr{test {\ifnumcomp{\value{index}}<{\LAST}}}{%
\stepcounter{index}%
\ifboolexpr{
test {\ifnumgreater{\value{index}}{1}}%
and
test {\ifnumless{\value{index}}{\LAST}}}%
{\theindex\ }%
{NOT\ }%
}
\end{document}


"First" and "Second" matches your original loops, while "Third" uses an and clause to merge two tests.

• That's excellent. For whatever reason, I couldn't get such an approach to work with \ifnumcomp, but \ifnumless and \ifnumgreater do the trick. – Steven B. Segletes Oct 24 '13 at 10:16