# If clause within a loop

I have a similar issue as the one discussed here If condition controlled with a loop variable.

To summarize my case: when looping through a continous range of numbers, from 1 to 24, I'd like do something special if the number is on this list: 9, 10, 12, 14, ..., 18.

My first try was to nest an \ifnum\value{\x} in {9,10,12,14,...,18} clause into a \foreach \x in {1,...,24}... but I suddenly found worrisome not to know that \ifnum\value{\x} can be followed only by =, >, or <.

Now, if I set \ifnum\value{\x} = {9,10,12,14,...,18} as in the example below, I get only (and weird) outcomes when \x = {9,10,12,14,...,18}.

Every comment is welcome!

Example:

\documentclass[letterpaper,11pt]{scrartcl}

\begin{document}
\foreach \x in {1,...,24}
{
\subsection{Question \x}
\begin{figure}[h]
\centering
\ifnum\value{\x} = {9,10,12,14,...,18} {
\includegraphics[width=\textwidth]{figures/univariate/q\x_a.pdf}
\includegraphics[width=\textwidth]{figures/univariate/q\x_b.pdf}
}
\else {
\includegraphics[width=\textwidth]{figures/univariate/q\x.pdf}
}
\fi
\vspace{-1cm}
\captionsetup{labelformat=empty}
\caption{}
\end{figure}
\clearpage
}

\end{document}

• This is not a compilable document... – user121799 May 5 at 19:00
• there is no loop defined here you just get an undefined command error on \foreach – David Carlisle May 5 at 19:16
• Thanks David. I'm fixing the example. – eagu_999 May 5 at 19:24

Some code I have written can be of help:

\documentclass{article}
\usepackage{xparse}

\usepackage[a6paper]{geometry} % smaller pictures

\ExplSyntaxOn

%%% code for cycles (communication to GuiTmeeting 2016)
\NewDocumentCommand{\newforeach}{ m +m }
{
\tl_clear:N \l__newforeach_type_tl
\keys_set:nn { newforeach }
{
start = 1, step = 1, end = 0, list=, #1
}
\__newforeach_exec:n { #2 }
}

\tl_new:N \l__newforeach_type_tl
\cs_generate_variant:Nn \str_case:nnF { V }

\keys_define:nn { newforeach }
{
type .choice:,
type .value_required:n = true,
type/integers .code:n = \tl_set:Nn \l__newforeach_type_tl { integers },
type/fp       .code:n = \tl_set:Nn \l__newforeach_type_tl { fp },
type/alph     .code:n = \tl_set:Nn \l__newforeach_type_tl { alph },
type/Alph     .code:n = \tl_set:Nn \l__newforeach_type_tl { Alph },
list     .clist_set:N = \l_newforeach_list_clist,
list*    .code:n = \clist_set:NV \l_newforeach_list_clist #1,

start .tl_set:N = \l_newforeach_start_tl,
step  .tl_set:N = \l_newforeach_step_tl,
end   .tl_set:N = \l_newforeach_end_tl,
}

\cs_new_protected:Nn \__newforeach_exec:n
{
\str_case:VnF \l__newforeach_type_tl
{
{integers}{\__newforeach_exec_integers:n { #1 }}
{fp}      {\__newforeach_exec_fp:n       { #1 }}
{alph}    {\__newforeach_exec_alph:n     { #1 }}
{Alph}    {\__newforeach_exec_Alph:n     { #1 }}
}
{\__newforeach_exec_list:n { #1 }}
}

\cs_new_protected:Nn \__newforeach_exec_integers:n
{
\int_step_inline:nnnn
{ \l_newforeach_start_tl }
{ \l_newforeach_step_tl }
{ \l_newforeach_end_tl }
{ #1 }
}
\cs_new_protected:Nn \__newforeach_exec_fp:n
{
\fp_step_inline:nnnn
{ \l_newforeach_start_tl }
{ \l_newforeach_step_tl }
{ \l_newforeach_end_tl }
{ #1 }
}
\cs_new_protected:Nn \__newforeach_exec_list:n
{
\clist_map_inline:Nn \l_newforeach_list_clist { #1 }
}

%%% code for exceptions
\NewDocumentCommand{\ifinclistTF}{mmmm}
{
\clist_if_in:nnTF { #2 } { #1 } { #3 } { #4 }
}

\ExplSyntaxOff

\begin{document}

\newforeach{type=integers,end=24}{%
\subsection{Question #1}
\begin{minipage}{\textwidth}
\centering
\ifinclistTF{#1}{9,10,12,14,16,18}
{
%\includegraphics[width=\textwidth]{figures/univariate/q\x.pdf}
{\Huge q#1}%
}
{%
%\includegraphics[width=\textwidth]{figures/univariate/q\x_a.pdf}
%\includegraphics[width=\textwidth]{figures/univariate/q\x_b.pdf}
{\Huge q#1\_a} {\Huge q#1\_b}%
}
\end{minipage}
\clearpage
}

\end{document}


The main difference is you don't use \x in the loop body, but #1. The \newforeach loops can be of different types, here integers is used. The starting point and the step are, by default, 1, so it's only needed to set the end point.

For the exceptions, I defined a further user level command.

In the code I changed the \includegraphics declarations to just the names, since I don't have your pictures. Just uncomment the \includegraphics lines and remove the ones with \Huge.

The following pictures shows the pages from 7 to 12, where the exceptions for 9, 10 and 12 show up.

• Thank you @egreg! Useful and instructive example! – eagu_999 May 6 at 14:07
\documentclass[letterpaper,11pt]{scrartcl}
\usepackage{pgffor,graphicx}
%\usepackage{caption}
\begin{document}
\foreach \x in {1,...,24}
{
\subsection{Question \x}
\begin{figure}[h]
\centering
\xdef\IsInside{0}
\foreach \y in {9,10,12,14,...,18}
{\ifnum\y=\x
\xdef\IsInside{1}
\fi}
\ifnum\IsInside=1
\includegraphics[width=0.45\textwidth]{example-image-a.pdf}~
\includegraphics[width=0.45\textwidth]{example-image-b.pdf}
\else
\includegraphics[width=\textwidth]{example-image-duck.pdf}
\fi
\caption{}

\end{figure}
\clearpage
}
\end{document}

• Thanks @marmot! I've just seen your reply. I will try to implemet it. – eagu_999 May 5 at 19:31
• Thank you @marmot! It worked very well! – eagu_999 May 6 at 14:08