2

I built a custom language-switch macro for my document. It seems to be working fine in some cases, but not in others. How to make this work in any case, e.g. just output the content of the first or second bracket depending on my LANGUAGE flag?

Here is the minimal working example:

\documentclass[11pt,a4paper]{article}
\usepackage{xifthen}

\newcommand{\LANGUAGE}{DE}              % or EN

% language selector, the command \LANGUAGE needs to be set before
\newcommand{\deen}[2]{\ifthenelse{\equal{\LANGUAGE}{DE}}{#1}{\ifthenelse{\equal{\LANGUAGE}{EN}}{#2}{NO LANGUAGE}}}

% content
\begin{document}
\section{Some title}
\deen{first entry}{second entry}                        % this one works
\section{Another title}
%\subsection{\deen{First language}{Second language}}    % this one fails
\end{document}

With the current setting (LANGUAGE=DE), the document looks like this:

1 Some title first entry 2 Another title

Changing the flag to LANGUAGE=EN does the trick in the first case (the line "first entry" will be changed to "second entry"), but when including it in e.g. \subsection{} it fails with the following error:

! Undefined control sequence.
<argument> \equal
{\LANGUAGE }{DE}
l.14 ...on{\deen{First language}{Second language}}

Any help is appreciated!

leosh
  • 235
  • 1
  • 2
  • 8
  • Welcome to TeX.SX! Can you please add a minimal example starting from \documentclass up to \end{document}? – egreg Dec 10 '17 at 22:59
  • I'm wondering if this is related. –  Dec 10 '17 at 23:05
  • \chapter{\deen{Kapitel}{Chapter}}{} What kind of document class are you using or what is the {} supposed to do? –  Dec 10 '17 at 23:27
  • Do you know about the babel package? –  Dec 11 '17 at 00:50
  • @Andrew yes I know about babel, but I'm not sure how that helps me here... – leosh Dec 11 '17 at 18:07
  • @marmot thanks for the link, but this is not related. The code given in the answer by Joseph Wright (last code example) has the same issue as my own code - it works when used as plain-text, but not when used as arguments to e.g. \section{}. – leosh Dec 11 '17 at 18:13

2 Answers2

1

As defined, \deen is fragile, so it must either be prefixed by \protect in a moving argument (such as a sectional title) or be defined with \DeclareRobustCommand.

\documentclass[11pt,a4paper]{article}
\usepackage{xifthen}

\newcommand{\LANGUAGE}{DE}              % or EN

% language selector, the command \LANGUAGE needs to be set before
\DeclareRobustCommand{\deen}[2]{%
  \ifthenelse{\equal{\LANGUAGE}{DE}}{#1}{%
    \ifthenelse{\equal{\LANGUAGE}{EN}}{#2}{NO LANGUAGE}}}

% content
\begin{document}
\section{Some title}
\deen{first entry}{second entry}                        % this one works
\section{Another title}
\subsection{\deen{First language}{Second language}}    % this one fails
\end{document}

enter image description here

egreg
  • 1,121,712
0

I am not sure why your code is not working but perhaps you should take an easier approach an just define a new boolean with

\newif\ifGerman% false by default

You use with code like \ifGermam <true code>\else<false code>\fi and you can change the value of the toggle using \Germantrue and \Germanfalse. So, with this in place you can define\deen` as follows:

\newcommand{\deen}[2]{\ifGerman #1\else#2\fi}

Plugging this into an extended version of the MWE in the question gives the output:

enter image description here

Here is the full code:

\documentclass[11pt,a4paper]{article}
\newif\ifGerman% false by default

% language selector, the command \LANGUAGE needs to be set before
\newcommand{\deen}[2]{\ifGerman #1\else#2\fi}

% content
\begin{document}

  First with \verb+\Germanfalse+
  \section{Some title}
  \deen{first entry}{second entry}                        % this one works
  \section{Another title}
  \subsection{\deen{First language}{Second language}}    % this one also works

  Now with \verb+\Germantrue+

  \Germantrue
  \section{Some title}
  \deen{first entry}{second entry}                        % this one works
  \section{Another title}
  \subsection{\deen{First language}{Second language}}    % this one works too!

\end{document}