[Tex/LaTex] Cleveref reference name depending on label instead of counter

cleverefcross-referencinglabelssectioning

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)?

Best Answer

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}
Related Question