There are several ways to do that. I propose two solutions, one using xifthen
, the other using expl3
. It would also be doable with ifthen
and functions from datatool
.
Note that for tests on \B
, I used string comparison because the contents is not always a number, contrary to \No
. I replaced one 2222
with 22222
in your logical expression (looks like a mistake in your question?).
I implemented the logical tests as you wrote them, but they could be simplified (the \No
= 5 test could be factored out).
Using xifthen
\RequirePackage{filecontents}
\begin{filecontents*}{test.csv}
Acol, Bcol, NoCol
Ax,Bxxxx,1
Ay,By,3
A1,B22,2
A2,B44,4
A3,11111,5
Ax,B,1
Ay,By,3
A1,B22,2
A2,B44,4
A3,22222,5
Ax,Bxxxx,1
Ay,By,3
A1,B22,2
A2,B44,4
A3,33333,5
\end{filecontents*}
\documentclass{article}
\usepackage{xifthen}
\usepackage{datatool}
\DTLloaddb{mydata}{test.csv}
\newcommand{\inserpageA}{%
\subsubsection*{AAA}
\newpage
}
\newcommand{\inserpageB}{%
\subsubsection*{BBB}
\newpage
}
\begin{document}
\DTLforeach*{mydata}{\A=Acol, \B=Bcol, \No=NoCol}%
{%
\ifthenelse{\(\cnttest{\No}{=}{5}\AND \equal{\B}{11111}\)\OR
\(\cnttest{\No}{=}{5}\AND \equal{\B}{22222}\)}%
{\inserpageA}%
{\ifthenelse{\cnttest{\No}{=}{5}\AND
\NOT\equal{\B}{11111}\AND
\NOT\equal{\B}{22222}}%
{\inserpageB}%
{\No\hspace{1cm}\B \hspace{5cm}\A\par}%
}%
}%
\end{document}
Using expl3
Maybe you'll find the expl3
way more readable. Contrary to xifthen
, boolean expressions for l3prg
(the expl3
module for these things) use standard precedence rules for &&
and ||
(logical AND and logical OR, respectively), which is nice.
Note that spaces are ignored between \ExplSyntaxOn
and \ExplSyntaxOff
. If you need a space there, use ~
or \space
. ~
may be ignored as usual after a control word, for instance the ~
in \B ~
is ignored, but \B \space
will insert a space token when \space
is expanded (it is a macro expanding to an explicit space token).
\RequirePackage{filecontents}
\begin{filecontents*}{test.csv}
Acol, Bcol, NoCol
Ax,Bxxxx,1
Ay,By,3
A1,B22,2
A2,B44,4
A3,11111,5
Ax,B,1
Ay,By,3
A1,B22,2
A2,B44,4
A3,22222,5
Ax,Bxxxx,1
Ay,By,3
A1,B22,2
A2,B44,4
A3,33333,5
\end{filecontents*}
\documentclass{article}
\usepackage{xparse}
\usepackage{datatool}
\DTLloaddb{mydata}{test.csv}
\ExplSyntaxOn
\NewDocumentCommand \lfortiProcessOneRow { }
{
\bool_if:nTF
{ % The parentheses below are not necessary, because && has higher
% priority than || in expl3 (l3prg) boolean expressions.
( \int_compare_p:nNn { \No } = { 5 } && \str_if_eq_p:Vn \B {11111} ) ||
( \int_compare_p:nNn { \No } = { 5 } && \str_if_eq_p:Vn \B {22222} )
}
{ \inserpageA }
{
\bool_if:nTF
{ \int_compare_p:nNn { \No } = { 5 } &&
! \str_if_eq_p:Vn \B {11111} &&
! \str_if_eq_p:Vn \B {22222}
}
{ \inserpageB }
{ \No \hspace{1cm} \B \hspace{5cm} \A \par }
}
}
\ExplSyntaxOff
\newcommand{\inserpageA}{%
\subsubsection*{AAA}
\newpage
}
\newcommand{\inserpageB}{%
\subsubsection*{BBB}
\newpage
}
\begin{document}
\DTLforeach*{mydata}{\A=Acol, \B=Bcol, \No=NoCol}{\lfortiProcessOneRow}
\end{document}
Output of both examples