3

I want to add language dependent conjunctions to cleveref. I think that

\documentclass{article}

\usepackage[ngerman]{babel}
\usepackage{cleveref}

\addto\captionsngerman{%
    \crefname{section}{Abschnitt}{Abschnitte}%
    \newcommand{\crefpairconjunction}{ und }%
}


\begin{document}
    \section{title}\label{1}
    \section{title}\label{2}

    \cref{1,2}
\end{document}

should work (and the output is indeed correct). However I get the error Command \crefpairconjunction already defined. \select@language{ngerman}. If I change \newcommand to \renewcommand, the error becomes \crefpairconjunction undefined. \select@language{ngerman}.

What am I doing wrong?

  • The code inside of \addtocaptions is called multiple times, so at the 2nd usage \crefpairconjunction is defined already – user31729 Mar 27 '18 at 8:34
2

The code that is added to the German captions is not added just once, but called multiple times, so after the first usage it would be alright, but in the next calls, \crefpairconjunction is already defined and can't be defined again, other than using \renewcommand.

It is possible to see the multiple call of the added code with \typeout{foo} after or before \crefname... -- you will see 3 'foo's in on the terminal etc. screen.

One possibility is to define a global \crefpairconjunction that is empty or has some default value in the beginning and then redefine it to be expand to und in the \addto\captions.... usage or check for the existence with \@ifundefined.

\documentclass{article}

\usepackage[ngerman]{babel}
\usepackage{cleveref}


\makeatletter
\addto\captionsngerman{%
  \crefname{section}{Abschnitt}{Abschnitte}%
  \@ifundefined{crefpairconjunction}{\newcommand{\crefpairconjunction}{ und }}{}%
}
\makeatother


\begin{document}
    \section{title}\label{1}
    \section{title}\label{2}

    \cref{1,2}
\end{document}
1

Just provide a default and then use \renewcommand:

\documentclass{article}

\usepackage[ngerman]{babel}
\usepackage{cleveref}

\providecommand\crefpairconjunction{ and }

\addto\captionsngerman{%
  \crefname{section}{Abschnitt}{Abschnitte}%
  \renewcommand{\crefpairconjunction}{ und }%
}


\begin{document}

\section{title}\label{1}

\section{title}\label{2}

\cref{1,2}

\end{document}
  • Why \providecommand? – John Dorian Mar 27 '18 at 8:46
  • 1
    @JohnDorian Maybe one loads packages that already define it; who knows? – egreg Mar 27 '18 at 8:47
  • 1
    @JohnDorian: \providecommand acts like \newcommand if the command isn't there already, otherwise it does nothing – user31729 Mar 27 '18 at 8:47

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.