Obtain expansion of \cref
In order to leave the first answer unmixed with a much shorter way I provide a second solution with the \crtcref
and \crtCref
macros from my crossreftools
package:
\documentclass{article}
\usepackage{cleveref}
\usepackage{crossreftools}
\begin{document}
\section{This is a section}
\label{sec}
\edef\temp{\crtcref{sec}}
\edef\Temp{\crtCref{sec}}
Content of reference: \temp\ or \Temp
\end{document}
Note that \cref
or \Cref
are not expandable, whereas \crtcref
and \crtCref
are expandable, hyperlinked versions however are not.
Current version of crossreftools
is 0.9 (being uploaded to CTAN today (2019/01/03), the \crtcref
etc. macros are from v0.1 however, v.0.9 contains a small bug fix for \crtcref
and \crtCref
, reporting wrong reference numbers, v.0.8. an extra macro 'crtcrefcountervalue
.
Neither \cref
nor \cref*
nor their uppercase variants are expandable, so storing them in a macro with \edef
will not work.
This is a by-pass solution.
cleveref
stores labels with the@cref
suffix, so\label{sec}
generates the label namessec
andsec@cref
.The label for
othersec
(see code below) is\newlabel{othersec@cref}{{[section][2][]2}{1}}
where
{[section][2][]2}
is the relevant information about the counter type (section
here,[2]
stands for the section number which is2
here and, however, I could not figure out the meaning of[]2
yet.\getrefnumber
byrefcount
package extracts the content stored under the label name, which is more complicated in the case ofcleveref
, but using a 'splitter' macro named\split@@internal
that leaves the content within the first[]
only in the input stream, it is possible to extract the counter type, i.e.section
here.cleveref
defines\cref@section@name
and\Cref@section@name
as macros that holds the singular name ofsection
andSection
, similarly for other counters, depending on the arguments to\crefname
and\Crefname
.
Please note the wrapping {}
pair around the cleveref
label -- it is possible to use this with \split@internal
and \split@@internal
.
The macros \xcref
and \Xcref
are not wrappers for \cref
or \Cref
but a bypass.
I have repeated the the section value in the output of \xcref
and \Xcref
with \getrefnumber{#1}
where the usual label is evaluated.
The code for \xcref
and \Xcref
checks for the existence of the label name as well, in order to provide 'nothing' if the label has not been defined yet -- this is important for the first compilation run of a document or after deletion of the .aux
file.
Please note: The approach here might fail when \label
is used with an optional argument which has been introduced by cleveref
-- I have not tested this yet.
\documentclass{article}
\usepackage{hyperref}
\usepackage{cleveref}
\makeatletter
\def\split@@internal[#1][#2][#3]#4{#1}%
\def\split@internal#1{%
\expandafter\split@@internal#1%
}
\newcommand{\Xcref}[1]{%
\IfRefUndefinedExpandable{#1@cref}{%
}{%
\csname Cref@\expandafter\split@internal\getrefnumber{#1@cref}@name\endcsname\ \getrefnumber{#1}%
}%
}
\newcommand{\xcref}[1]{%
\IfRefUndefinedExpandable{#1@cref}{%
}{%
\csname cref@\expandafter\split@internal\getrefnumber{#1@cref}@name\endcsname\ \getrefnumber{#1}%
}%
}
\makeatother
\begin{document}
\edef\tmp{\Xcref{othersec}}
\section{This is a section and is followed by \Xcref{othersec}}
\label{sec}
\section{Other section}\label{othersec}
or use \tmp
\end{document}