# cleveref: configure symbol §/§§ for all sectioning types once time

I want to configure the section symbol § (singular and plural form §§) indistinctly for all types of sections (part, chapter, section, subsection, paragraph and so on). I use the cleveref package, but I don't want this type of customization:

\crefname{part}{\S}{\S\S}
\crefname{chapter}{\S}{\S\S}
\crefname{section}{\S}{\S\S}
\crefname{subsection}{\S}{\S\S}
...


How can I configure § and §§ for all calls to \cref, irrespective of the type of reference?

I've been searching in the cleveref documentation but I haven't found/understood how I can do this.

-
What's wrong in that code? – egreg Nov 6 '12 at 12:30
Ugly, unpleasant, repetitous and in general an horror in terms of code readability and design. – Peregring-lk Nov 6 '12 at 12:33
Not uglier than using §. :) Seriously, it's not clear why the package should provide commands that very few people would need. Should it provide also a command for various sets of sectioning commands? – egreg Nov 6 '12 at 12:34
Hehe, that is right, but I'm writting now a formal document and for differente reasons I want to use that ugly symbol. – Peregring-lk Nov 6 '12 at 12:38

\makeatletter
\newcommand{\crefnames}[3]{%
\@for\next:=#1\do{%
\expandafter\crefname\expandafter{\next}{#2}{#3}%
}%
}
\makeatother

\crefnames{part,chapter,section}{\S}{\S\S}


You can use any set of sectional commands; this is sufficient for lower levels. If you want to use different symbols for \paragraph and \subparagraph, just do

\crefnames{part,chapter,section}{\S}{\S\S}
\crefnames{paragraph}{\P}{\P\P}


Complete example drawn from lockstep's:

\documentclass{book}

\usepackage{cleveref}

\makeatletter
\newcommand{\crefnames}[3]{%
\@for\next:=#1\do{%
\expandafter\crefname\expandafter{\next}{#2}{#3}%
}%
}
\makeatother

\setcounter{secnumdepth}{100}

\crefnames{part,chapter,section}{\S}{\S\S}
\crefnames{paragraph,subparagraph}{\P}{\P\P}

\begin{document}

\chapter{foo}\label{sec:f}

\section{foobar}\label{sec:fb}

\subsection{foobargnu}\label{sec:fbg}

\subsubsection{foobargnugnat}\label{sec:fbgg}

\subsubsection{foobargnugnat2}\label{sec:fbgg2}

These are references to \cref{sec:f}, \cref{sec:fb}, \cref{sec:fbg}, and \cref{sec:fbgg,sec:fbgg2}.

\paragraph{par}\label{par:p}

\subparagraph{subpar}\label{par:sp}

\subparagraph{subpar2}\label{par:sp2}

References to paragraphs: \cref{par:p}, \cref{par:sp,par:sp2}.

\end{document}


In order to avoid problems with spaces in the list of sectional units, you can use xparse:

\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\crefnames}{ m m m }
{
\clist_map_inline:nn { #1 } { \crefname{##1}{#2}{#3} }
}
\ExplSyntaxOff


The more complex way you use in your self-answer can be written

\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\crefnames}{ m m m }
{
\clist_map_inline:nn { #1 }
{
\crefformat{##1}{#2####2####1####3}
\crefmultiformat{##1}
{#3####2####1####3}
{and~####2####1####3}
{, ####2####1####3}
{, and~####2####1####3}
}
}
\ExplSyntaxOff

-
+1. BTW, a nice way to suggest that this kind of formatting is "subpar". ;-) – lockstep Nov 6 '12 at 12:53
Thank you very much. You are just geniusses. – Peregring-lk Nov 6 '12 at 13:09
@egreg Perhaps you could communicate your macro to Toby Cubbit in order to improve his package. Perhaps you are right and very few people needs this type of macros, but it is really a comfortable macro. – Peregring-lk Nov 6 '12 at 13:18

The cleveref package contains no built-in way to redefine the cross-reference format of all sectioning levels with a "single-stroke" macro. However, and quoting from section 7.1.2 of the manual,

[f]or convenience, if they have not been otherwise customised by the end of the preamble, the cross-reference name (and label format) for subsection is by default inherited from that of section, and that of subsubsection is inherited from subsection (which might itself have been inherited from section). Similarly for subappendix, subsubappendix and subsubsubappendix, and also for enumii, enumiii, enumiv and enumv, which inherit from enumi. Finally, subfigure and subtable inherit from figure and table, respectively.

\documentclass{book}

\usepackage{cleveref}

\crefname{chapter}{\S}{\S\S}
\crefname{section}{\S}{\S\S}

\setcounter{secnumdepth}{3}

\begin{document}

\chapter{foo}\label{sec:f}

\section{foobar}\label{sec:fb}

\subsection{foobargnu}\label{sec:fbg}

\subsubsection{foobargnugnat}\label{sec:fbgg}

These are references to \cref{sec:f}, \cref{sec:fb}, \cref{sec:fbg}, and \cref{sec:fbgg}.

\end{document}


-
Very useful your comments about the "heritage" of names. Thank you. – Peregring-lk Nov 6 '12 at 13:19
@Peregring-lk You may also thank me by upvoting my answer. :-) – lockstep Nov 6 '12 at 13:20
@Peregring-lk It's the upward-pointing pen nib, not the checkmark. ;-) – lockstep Nov 6 '12 at 13:41
@lockstep: How do you know who is upvoting (or not)? – AlexG Nov 6 '12 at 13:54
@Peregring-lk You can only accept one answer (by clicking on the checkmark), but you can upvote as many questions/answers as you want (by clicking on the upward-pointing pen nib). You'll even earn badges for it. :-) – lockstep Nov 6 '12 at 14:02
show 4 more comments

I believe it's customary not to leave a space between the section symbol generated by \S (plural form \S\S) and the associated number (or numbers). To achieve this outcome using cleveref's cross-referencing commands, it's necessary to use the "low-level" controls \crefformat and \crefmultiformat; relying on the "high-level" control \crefname doesn't quite get you there, I think. The following MWE is a modified form of lockstep's example:

\documentclass{book}
\usepackage{cleveref}

\crefformat{chapter}{\S#2#1#3}
\crefmultiformat{chapter}{\S\S#2#1#3}{and~#2#1#3}{, #2#1#3}{, and~#2#1#3}

\crefformat{section}{\S#2#1#3}
\crefmultiformat{section}{\S\S#2#1#3}{and~#2#1#3}{, #2#1#3}{, and~#2#1#3}

\setcounter{secnumdepth}{3}
\begin{document}
\chapter{foo}\label{sec:f}
\section{foobar}\label{sec:fb}
\subsection{foobargnu}\label{sec:fbg}
\subsubsection{foobargnugnat}\label{sec:fbgg}

These are cross-references to \cref{sec:f} and to \cref{sec:fb,sec:fbg,sec:fbgg}.
\end{document}

-
 You have reason with the spaces. I didn't realize this. – Peregring-lk Nov 6 '12 at 13:41

Mixing the contributions of @Mico, @lockstep and @egreg, this is my actual integrated customization:

\newcommand{\crefnames}[3]{%
\@for\next:=#1\do{%
\expandafter\crefformat\expandafter{\next}{#2##2##1##3}
\expandafter\crefmultiformat\expandafter{\next}
{#3##2##1##3}{and~##2##1##3}{, ##2##1##3}{, and~##2##1##3}
}%
}

\setcounter{secnumdepth}{100}

\crefnames{part,section}{\S}{\S\S}
\crefnames{paragraph}{\P}{\P\P}


Finally, a questions about this: if I put a space in the crefnames list, only the first element is taken: for example, in \crefnames{part, section}{\S}{\S\S} only part is configurated. Why?

-
 \@for doesn't trim spaces around the various parts of the list. I'll add a way to cope with this to my answer. – egreg Nov 6 '12 at 13:55