# If command in LaTex

I am writing a project for a course and I want to make each Part to have a different color image on the side. But to do this I have to make a loop (that's what I think) to recognize the Part and and make the corresponding programming. I found this online, but it doesn't return me anything

\documentclass{book}

\usepackage{ifthen}

\begin{document}
\newcommand{\PartRecognizer}[1]
{
\ifthenelse{\equal{#1}{1}}{Part 1}{}
\ifthenelse{\equal{#1}{2}}{Part 2}{}
}
\PartRecognizer{\thepart}

\end{document}


Any ideas?

• Welcome to TeX.SX!! It is better to post a full minimal working example that starts with a \documentclass command, has a minimal preamble and then \begin{document}...\end{document}. Unless the problem is a compilation error, the code should compile and be as small as possible to demonstrate your problem. This makes it much easier for people to help you --- and much more likely that they will! – Andrew Mar 5 '16 at 2:24
• Presumably \thepart is 0 right now because your document doesn't have any \part{}s yet. – cfr Mar 5 '16 at 4:17
• No even when i put in in a part doesn't show nothing – Jon Snow Mar 5 '16 at 4:19
• I found it :/ how stupit.... The part counts I,II,III,IV... and not 1,2,3 :P – Jon Snow Mar 5 '16 at 4:45
• Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – JP-Ellis Nov 14 '16 at 0:32

The default \parts enumeration in book.cls is set using \Roman{part}. That is,

\renewcommand \thepart {\@Roman\c@part}


However, it's better to not test against the representation of the counter, but against the counter itself. To that end, we'll condition on what \value{part} looks like.

Finally, since your macro seems solely dedicated to condition on the part, its seems superfluous to pass the part number as an argument.

\documentclass{book}

\newcommand{\PartRecognizer}{%
Part
\ifcase\value{part}%
0%
\or 1%
\or 2%
\else ?%
\fi
}

\begin{document}

\part{First part}

\PartRecognizer

\end{document}


Note that the part counter is incremented when you call \part. So, like most counters, its starts with a default of 0.

I've used a "case-like" conditioning above, as you'll step the part counter in single increments, possibly starting from 0 - something that \ifcase works well with.

The \equal test in \ifthenelse compares strings, but you want to compare numbers. So

\documentclass{book}

\usepackage{ifthen}

\newcommand{\PartRecognizer}{%
\ifthenelse{\value{part}=1}{Part 1}{}%
\ifthenelse{\value{part}=2}{Part 2}{}%
}

\begin{document}

\PartRecognizer

\end{document}


Please, note the % comment characters that avoid unwanted spurious space in the output.

This works, but has several defects, because you can't use it for something like

\newcommand{\PartRecognizer}{%
\ifthenelse{\value{part}=1}{duck}{}%
}


and then

\includegraphics{\PartRecognizer}


in the place where you want the graphics. This would require more awkward code.

Here's an easier implementation, where adding image names is straightforward. The first part of the code is meant to add the image to the part starting page.

\documentclass{book}
\usepackage{graphicx,etoolbox,xparse}

\makeatletter
\preto\@endpart{%
\vfil
\begin{center}
\includegraphics[height=.5\textheight]{\partimage}
\end{center}
}
\makeatother

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\partimage}{}
{
\int_case:nn { \value{part} }
{
{1}{duck}
% add here file names for the following parts
}
}
\ExplSyntaxOff

\begin{document}

\part{Ducks}

\part{Monsters}

\end{document}


• ooh cookies! :) – Paulo Cereda Mar 5 '16 at 10:02