0

I want to set the title of a section with a command which includes an \ifthenelse condition:

\documentclass{article}
\usepackage{ifthen}

\newcommand{\region}{Germany}

\newcommand{\getValue}{ \ifthenelse{\equal{\region}{Germany}}{ Kapitel 1 }{ Chapter 1 }
}

\begin{document} \section{\getValue} \end{document}

This however doesn't work and I would like to know why it doesn't work and how to get it working. I receive a lot of errors when I compile the minimal working example which I don't know how to fix. I could very easily solve the problem in the example by the following:

\begin{document}
\ifthenelse{\equal{\region}{Germany}}{
    \section{Kapitel 1}
}{
    \section{Chapter 1}
}   

\end{document}

However this workaround is not an option for me in production as I want to use the getValue command in this case more frequently. I had a look at this question however apart from the title the question doesn't have much in common with this question.

Jonathan
  • 113
  • Given the fact that sections write their contents to the toc file, the latter solution is the most stable one. You should probably write some better get funtions/if constructions instead – daleif Jul 21 '20 at 11:40
  • Ok, thank you. But I also would like to understand why it is not working as I am still a Latex beginner. The first example is working when I make a \getValue command without \ifthenelse e.g. \newcommand{\getValue}{Kapitel 1}. Why is it working in this case but not with \ifthenelse? – Jonathan Jul 21 '20 at 11:53

1 Answers1

0

Using ifthen for languages switches is not a good idea. It doesn't scale well: Imagine that you want to add options for french, italian, russian, japanese ...

But beside this: the content of the argument of \section is used in more than one place, this means that commands that you want to use there should be robust. You can define such robust commands e.g. with \NewDocumentCommand from xparse:

\documentclass{article}
\usepackage{ifthen,xparse}

\newcommand{\region}{Germany}

\NewDocumentCommand{\getValue}{}{% \ifthenelse{\equal{\region}{Germany}}{% Kapitel 1 }{% Chapter 1 }% }

\begin{document} \section{\getValue} \end{document}

Ulrike Fischer
  • 327,261