TransWikia.com

Using a command with ifthenelse for setting a section title

TeX - LaTeX Asked on November 1, 2021

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.

One Answer

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}

Answered by Ulrike Fischer on November 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP