9

Let's say I have this:

\documentclass{article}
\usepackage{cleveref}
\begin{document}
\section{Section One}
\label{a:sec1}
\cref{a:sec1}

\section{Section Two}
\label{b:sec2}
\cref{b:sec2}
\end{document}

Cleveref will now reference "Section 1" and "Section 2" because the label comes after a section. But what I need is that it references the label depending on a: and b:, where I define the displayed name. For example: All labels having an a: should be referenced as "Car X" and all labels having a b: should be referenced as "Truck X" even though they are both a section.

Note: I do not want to change the counter, only the word that is displayed when referencing depending on the label's "namespace"!

Is cleveref able to do this alone or do I have to build some custom solution (probably with prettyref)?

  • 1
    well, you should define a \carsection and \trucksection instead. That's of course solvable as well, and I would consider it more standard. – yo' Sep 13 '14 at 14:51
14

Rather than give special meaning to items based on the labels' "prefixes" (a:, b:, ...), I'd use a feature that's already provided by the cleveref package: use the optional argument of the \label command to override an item's default cross-referencing name.

In the following example, I first inform cleveref about the singular and plural forms of items called "car" and "truck", and then use the optional arguments of the \label commands to inform cleveref that the corresponding sections' "names" are "car" and "truck" rather than "section". (The hyperref package is loaded and the option nameinlink is specified merely to make the output of cleveref immediately visible.)

enter image description here

\documentclass{article}
\usepackage[colorlinks=true]{hyperref}
\usepackage[nameinlink]{cleveref}    
% provide singular and plural names of the categories "car" and "truck"
\crefname{truck}{truck}{trucks}
\crefname{car}{car}{cars}

\begin{document}
\section{Section One}\label[car]{sec:1}
\section{Section Two}\label[truck]{sec:2}
\section{Section Three}
Cross-references to \cref{sec:1,sec:2}.
\end{document}
3

Based on my answer at new command for including a custom string into \cref, one can change the labels of cleveref (and also work with hyperref). In this answer, I extend that to look at the first character of the label to help decide what the reference should look like. Here, I create a new macro, \crefX to do the job, but it could be made to replace the default \cref, if one really wanted it so (I don't recommend that, however):

\documentclass{article}
\usepackage{hyperref}
\usepackage{cleveref}

\newcommand{\crefX}[1]{\crefformat{section}{%
  \crefXhelper#1\relax~##2##1##3}%
  \cref{#1}\crefformat{section}{section~##2##1##3}}

\def\crefXhelper#1#2\relax{\if#1aCar\else\if#1bTruck\else Section\fi\fi}

\begin{document}


\section{Section One (a label)}
\label{a:sec1}
This section \crefX{a:sec1}

\section{Section Two (b label)}
\label{b:sec2}
This section \crefX{b:sec2} and prior section, \crefX{a:sec1}.

\section{Section Three (c label)}
\label{c:sec3}
This section \crefX{c:sec3} and prior section, \crefX{b:sec2}
and first section, \crefX{a:sec1}.
\end{document}

enter image description here

If you really wanted it to say "Car X" without the actual number, then

\newcommand{\crefX}[1]{\crefformat{section}{%
  \crefXhelper#1\relax~##2X##3}%
  \cref{#1}\crefformat{section}{section~##2##1##3}}

could work.

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.