Language dependent conjunctions in cleveref

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

\documentclass{article}

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

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

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

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



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

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
\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}


Just provide a default and then use \renewcommand:

\documentclass{article}

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

\providecommand\crefpairconjunction{ and }

\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
• @JohnDorian Maybe one loads packages that already define it; who knows? – egreg Mar 27 '18 at 8:47
• @JohnDorian: \providecommand acts like \newcommand if the command isn't there already, otherwise it does nothing – user31729 Mar 27 '18 at 8:47