4

I have some custom cross-references that need labels that are not naturally linked to counters. My macros therefore set \@currentlabel directly instead of using \refstepcounter. This works fine but cleveref is not able to understand the type of these references (unless one specifies a custom type for each affected \label).

I have seen questions where redefining \cref@currentlabel managed to get cleveref informed of the right format to use but it is not clear what is the arguments specification that the package is expecting to be used. From bits and pieces, the redefinition seems to follow a scheme similar to

\protected@edef\cref@currentlabel{%
  [type][???][\cref@result]%
  The typeset label}

So my question is: how is one supposed to communicate to cleveref that the current label is a custom one, with custom type mytype? How is \cref@currentlabel supposed to be redefined?

4
  • cleverref always complains about unknown types, i.e. the if you use foo as type, and do not specify the corresponding \crefformat{foo}{#2#1#3} it will choke otherwise your approach should work
    – user31729
    Dec 29 '16 at 15:25
  • I think the crucial point is the macro ` \cref@constructprefix{#1}{\cref@result}`
    – user31729
    Dec 29 '16 at 16:04
  • @ChristianHupfer it's ok if I need to instruct cleveref on how to handle the custom type. My issue is: how to signal which type I want it to use, if I am bypassing \refstepcounter?
    – Bordaigorl
    Dec 29 '16 at 16:19
  • I have no fully working solution yet
    – user31729
    Dec 29 '16 at 16:20
4

cleveref is a very clever package, but hard to understand as it has many windings inside.

The easiest way, in my point of view, is to use \label[type]{labelname} Funny enough, type does not need to be a counter at all, but \cref@currentlabel must be defined as has been done by the O.P. already.

The value in the second [...] inside of the \cref@currentlabel{...} has usually the value of the counter, this is not important here, so I left it empty.

The \crefname and \Crefname macros should be called to inform cleveref about the type name.

\documentclass{article}

\usepackage{blindtext}
\usepackage{hyperref}
\usepackage{cleveref}

\usepackage{xparse}


\makeatletter

\NewDocumentCommand{\arblabel}{O{footext}m+m}{%
  \begingroup
  \cref@constructprefix{#1}{\cref@result}%
  \protected@edef\@currentlabel{%
    #3}%
  \protected@edef\@currentlabelname{#3}%
  \protected@edef\cref@currentlabel{%
    [#1][][\cref@result]%
    #3%
  }
  \label[#1]{#2}%
  \endgroup  
}

\makeatother


\crefname{footext}{some foo text}{some foo texts}
\Crefname{footext}{Some Foo text}{Some foo texts}


\begin{document}

\blindtext[3]
\section{My section} \label{secone}
\blindtext[2]

Here is the link target: \arblabel{foo}{This is a nice foo text}

\blindtext[2]

\cref{foo} or rather like \Cref{foo}?

\end{document}
10
  • Thanks for this. Followup question: is the value of the counter (left empty here) used for sorting? Would this create unwanted effects when using \cref{foo1,foo2}? And what does \cref@result do? And constructprefix?
    – Bordaigorl
    Dec 29 '16 at 17:49
  • @Bordaigorl: I have to check...
    – user31729
    Dec 29 '16 at 17:51
  • I guess you want a \phantomsection too?
    – Bordaigorl
    Dec 29 '16 at 17:54
  • It is a pity that this is not documented, cleveref is such a nice package...
    – Bordaigorl
    Dec 29 '16 at 17:55
  • @Bordaigorl: I am not sure, most likely
    – user31729
    Dec 29 '16 at 17:56

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.