5

The method to change the name of the contents while using the babel package
is described here.

Per the answer included in the link, the following code may be used:

\documentclass{article}

\usepackage[american]{babel}

\addto\captionsamerican{% 
  \renewcommand{\contentsname}%
    {newContentsName}%
}

\begin{document}

\tableofcontents

\section{Section}
\subsection{Subsection}

\end{document}

If I wanted to use a variable for the language, I thought I could make the following adjustment:

\documentclass{article}

\newcommand{\xLanguage}{american}          % <-- Added this command

\usepackage[\xLanguage]{babel}             % <-- Implemented here

\addto\csname captions\xLanguage\endcsname{% <-- and here
  \renewcommand{\contentsname}%
    {newContentsName}%
}

\begin{document}

\tableofcontents

\section{Section}
\subsection{Subsection}

\end{document}

But this does not work.
Am I approaching this correctly?

4

\csname captions\xLanguage\encsname must be expanded first, before \addto can work with the 'real' command sequence name, i.e. an \expandafter is needed before \addto.

I.e. captions\xLanguage must be expanded first to yield the content of \xLanguage, american in the O.P. example, such that captionsamerican is usable for `\csname ...\endcsname.

Only after that \addto\captionsamerican{...} is finally available.

This is the same code needed for constructing \newcommand macros etc. from macronames or parameters etc., i.e. \expandafter\newcommand\csname foo\otherfoobar\endcsname{My foo content}, where \otherfoobar contains 'any' token being allowed as a \newcommand - defined macro name.

\documentclass{article}

\newcommand{\xLanguage}{ngerman}          % <-- Added this command

\usepackage[\xLanguage]{babel}             % <-- Implemented here

\expandafter\addto\csname captions\xLanguage\endcsname{% <-- and here
  \renewcommand{\contentsname}%
    {Verzeichnisse der tollen Inhalte}%
}

\begin{document}

\tableofcontents

\section{Section}
\subsection{Subsection}

\end{document}
| improve this answer | |
3

I'm not sure about the usefulness of this approach, but you can use etoolbox:

\documentclass{article}
\usepackage{etoolbox}

\newcommand{\xLanguage}{american}          % <-- Added this command

\usepackage[\xLanguage]{babel}             % <-- Implemented here

\csappto{captions\xLanguage}{% <-- and here
  \renewcommand{\contentsname}%
    {newContentsName}%
}

\begin{document}

\tableofcontents

\section{Section}
\subsection{Subsection}

\end{document}

enter image description here

| improve this answer | |
  • Alternatives informative and are always appreciated. – kando Aug 22 '16 at 22:39

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.