0

In the question Use xifthen: how set \cnttest not equal number, I learnt how to perform if-then-else tests where the condition compares two integers. Now, I would like to do the same, but with string comparisons instead of integer comparisons. For example, I would like to test values stored in a macro \No where the values could be X, Y, Z, etc.

My minimal code:

\RequirePackage{filecontents}
\begin{filecontents*}{test.csv}
Acol, Bcol, NoCol
A0,0000,0
Ax,Bx,1
Ay,By,2
A1,B22,3
A2,B44,4
A3,29,5
A3,29,99
A3,449,100
AX,500,X
AY,500,Y
AZ,600,Z
\end{filecontents*}

\documentclass{article}
\usepackage[a5paper,landscape]{geometry}
\usepackage{ifthen}
\usepackage{datatool}
\DTLloaddb{mydata}{test.csv}

\newcommand{\inserpageX}{%
  \subsubsection*{Page X}
  \newpage
}
\newcommand{\inserpageY}{%
  \subsubsection*{Page Y}
  \newpage
}
\newcommand{\inserpageZ}{%
  \subsubsection*{Page Z}
  \newpage
}

\begin{document}

\DTLforeach*{mydata}{\A=Acol, \B=Bcol, \No=NoCol}%
{%

\ifthenelse{\No=0}% X
    {\newpage 
    \inserpageX}%
     {
     \ifthenelse{\No=1}% Y
                 {\newpage \inserpageY}%
{
     \ifthenelse{\No=100}% Z
                 {\newpage \inserpageZ}%
               {

     {\No \hspace{1cm}\B \hspace{5cm}\A\par}
    }
 }
}
}

\end{document}

How can I distinguish between the three cases \No = X, \No = Y and \No = Z that occur in the sample test.csv file?

Thanks in advance.

  • 1
    Alternative approach: If you define a command for every number in NoCol work with csnames and the number. Then it's just one line without ifthenelses (or only one test if the csname is defined and execute the general else otherwise)… – TeXnician Aug 25 '19 at 6:09
  • 1
    @latexforti I find the question not very clear: 1) The NoCol column of the provided data file contains only numbers, not X, Y, etc. 2) You mention xifthen but are actually using ifthen (these are two different packages!). ifthen has a string equality test that is \equal{string1}{string2}, but it would probably be wise to explain clearly what you want to do in the end, instead of what you think you need to do in order to achieve your goal. – frougon Aug 25 '19 at 6:24
  • @frougon thanks. i have updated with text x,y,z in my filecontent. – latexforti Aug 25 '19 at 8:11
1

So, now you are using the ifthen package. This is not the same as xifthen which I used in my previous answer on the subject, and also egreg in the question you linked to. Beware not to confuse the ifthen and xifthen packages.

With ifthen, you can do string comparisons using the \equal{string1}{string2} test, as shown in your modified example below. The same syntax is valid with xifthen too, but one couldn't be sure without first checking the manuals or code of these packages.

Don't forget % signs after braces ({ or }) found at the end of a line, unless you really want to add a space in the output. I did not put one after the closing brace that precedes \end{document}, because this closing brace (}) is at the very end of a paragraph, and a space at the end of a paragraph is ignored. But you could append a % there too if you were unsure, it wouln't harm in any way. If unsure, finish your lines with % unless you really want a space to be inserted.

Here is the code with your string equality tests. It also works if you replace \usepackage{ifthen} with \usepackage{xifthen}.

\RequirePackage{filecontents}
\begin{filecontents*}{test.csv}
Acol, Bcol, NoCol
A0,0000,0
Ax,Bx,1
Ay,By,2
A1,B22,3
A2,B44,4
A3,29,5
A3,29,99
A3,449,100
AX,500,X
AY,500,Y
AZ,600,Z
\end{filecontents*}

\documentclass{article}
\usepackage{ifthen}
\usepackage{datatool}
\DTLloaddb{mydata}{test.csv}

\newcommand{\inserpageX}{%
  \subsubsection*{Page X}
  \newpage
}
\newcommand{\inserpageY}{%
  \subsubsection*{Page Y}
  \newpage
}
\newcommand{\inserpageZ}{%
  \subsubsection*{Page Z}
  \newpage
}

\begin{document}

\DTLforeach*{mydata}{\A=Acol, \B=Bcol, \No=NoCol}%
{%
  \ifthenelse{\equal{\No}{X}}%
    {\newpage \inserpageX}%
    {%
      \ifthenelse{\equal{\No}{Y}}%
        {\newpage \inserpageY}%
        {%
          \ifthenelse{\equal{\No}{Z}}%
            {\newpage \inserpageZ}%
            {\No \hspace{1cm}\B \hspace{5cm}\A\par}%
        }%
    }%
}

\end{document}

Screenshot

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.