Obtain expansion of cref
Is it possible to save the expansion of cref in a macro? My try
documentclass{article}
usepackage{cleveref}
begin{document}
section{This is a section}
label{sec}
edeftemp{expandafteremptycref{sec}}
showtemp
end{document}
fails with some obscure errors.
cross-referencing expansion cleveref
|
show 2 more comments
Is it possible to save the expansion of cref in a macro? My try
documentclass{article}
usepackage{cleveref}
begin{document}
section{This is a section}
label{sec}
edeftemp{expandafteremptycref{sec}}
showtemp
end{document}
fails with some obscure errors.
cross-referencing expansion cleveref
2
To give some background info: can you tell use what you are trying to do with the macro?
– samcarter
Jul 20 '17 at 12:57
1
This can't work, but perhaps there are other methods. They might depend on the intended usage.
– egreg
Jul 20 '17 at 12:57
The main reason is that I have something likesection{Comments on cref{other_section}}and I would like that the bookmark in the PDF is resolved toComments on Section 3.
– gerw
Jul 20 '17 at 12:58
see tex.stackexchange.com/a/138950/36296
– samcarter
Jul 20 '17 at 13:20
or tex.stackexchange.com/questions/235516/…
– samcarter
Jul 20 '17 at 13:20
|
show 2 more comments
Is it possible to save the expansion of cref in a macro? My try
documentclass{article}
usepackage{cleveref}
begin{document}
section{This is a section}
label{sec}
edeftemp{expandafteremptycref{sec}}
showtemp
end{document}
fails with some obscure errors.
cross-referencing expansion cleveref
Is it possible to save the expansion of cref in a macro? My try
documentclass{article}
usepackage{cleveref}
begin{document}
section{This is a section}
label{sec}
edeftemp{expandafteremptycref{sec}}
showtemp
end{document}
fails with some obscure errors.
cross-referencing expansion cleveref
cross-referencing expansion cleveref
edited Sep 17 '17 at 8:29
Christian Hupfer
147k14192384
147k14192384
asked Jul 20 '17 at 12:52
gerw
361110
361110
2
To give some background info: can you tell use what you are trying to do with the macro?
– samcarter
Jul 20 '17 at 12:57
1
This can't work, but perhaps there are other methods. They might depend on the intended usage.
– egreg
Jul 20 '17 at 12:57
The main reason is that I have something likesection{Comments on cref{other_section}}and I would like that the bookmark in the PDF is resolved toComments on Section 3.
– gerw
Jul 20 '17 at 12:58
see tex.stackexchange.com/a/138950/36296
– samcarter
Jul 20 '17 at 13:20
or tex.stackexchange.com/questions/235516/…
– samcarter
Jul 20 '17 at 13:20
|
show 2 more comments
2
To give some background info: can you tell use what you are trying to do with the macro?
– samcarter
Jul 20 '17 at 12:57
1
This can't work, but perhaps there are other methods. They might depend on the intended usage.
– egreg
Jul 20 '17 at 12:57
The main reason is that I have something likesection{Comments on cref{other_section}}and I would like that the bookmark in the PDF is resolved toComments on Section 3.
– gerw
Jul 20 '17 at 12:58
see tex.stackexchange.com/a/138950/36296
– samcarter
Jul 20 '17 at 13:20
or tex.stackexchange.com/questions/235516/…
– samcarter
Jul 20 '17 at 13:20
2
2
To give some background info: can you tell use what you are trying to do with the macro?
– samcarter
Jul 20 '17 at 12:57
To give some background info: can you tell use what you are trying to do with the macro?
– samcarter
Jul 20 '17 at 12:57
1
1
This can't work, but perhaps there are other methods. They might depend on the intended usage.
– egreg
Jul 20 '17 at 12:57
This can't work, but perhaps there are other methods. They might depend on the intended usage.
– egreg
Jul 20 '17 at 12:57
The main reason is that I have something like
section{Comments on cref{other_section}} and I would like that the bookmark in the PDF is resolved to Comments on Section 3.– gerw
Jul 20 '17 at 12:58
The main reason is that I have something like
section{Comments on cref{other_section}} and I would like that the bookmark in the PDF is resolved to Comments on Section 3.– gerw
Jul 20 '17 at 12:58
see tex.stackexchange.com/a/138950/36296
– samcarter
Jul 20 '17 at 13:20
see tex.stackexchange.com/a/138950/36296
– samcarter
Jul 20 '17 at 13:20
or tex.stackexchange.com/questions/235516/…
– samcarter
Jul 20 '17 at 13:20
or tex.stackexchange.com/questions/235516/…
– samcarter
Jul 20 '17 at 13:20
|
show 2 more comments
2 Answers
2
active
oldest
votes
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}
edeftemp{crtcref{sec}}
edefTemp{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.7 (being uploaded to CTAN today (2018/12/28), the crtcref etc. macros are from v0.1 however, v.0.7 contains a small bug fix for crtcref and crtCref, reporting wrong reference numbers.
1
Doesn't work really well with subsections for me. Whereas theCrefwas "Section 2.1.2", thecrtCrefis "Section 2" (usingcrtnumberalso yields 2).
– Norswap
6 hours ago
@Norswap: Oh my, there is another new version ofcleverefapparently that was released one week aftercrossreftools v.0.6-- I will investigate and update accordingly. I am pretty sure that it worked previously!. Thanks for notifying me!
– Christian Hupfer
5 hours ago
@Norswap: Ok, I found a small typo incrtcrefandcrtCref, using the wrong information. Please note, thatcrefitself still reportssectionfor subsection etc. references, as long as there is no propercrefnameetc. setup -- I've uploadedcrossreftools 0.7to CTAN, it should be available soon there and on TeXLive/MikTeX
– Christian Hupfer
2 hours ago
add a comment |
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.
cleverefstores labels with the@crefsuffix, solabel{sec}generates the label namessecandsec@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 (sectionhere,[2]stands for the section number which is2here and, however, I could not figure out the meaning of2yet.
getrefnumberbyrefcountpackage extracts the content stored under the label name, which is more complicated in the case ofcleveref, but using a 'splitter' macro namedsplit@@internalthat leaves the content within the firstonly in the input stream, it is possible to extract the counter type, i.e.sectionhere.
cleverefdefinescref@section@nameandCref@section@nameas macros that holds the singular name ofsectionandSection, similarly for other counters, depending on the arguments tocrefnameandCrefname.
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
defsplit@@internal[#1][#2][#3]#4{#1}%
defsplit@internal#1{%
expandaftersplit@@internal#1%
}
newcommand{Xcref}[1]{%
IfRefUndefinedExpandable{#1@cref}{%
}{%
csname Cref@expandaftersplit@internalgetrefnumber{#1@cref}@nameendcsname getrefnumber{#1}%
}%
}
newcommand{xcref}[1]{%
IfRefUndefinedExpandable{#1@cref}{%
}{%
csname cref@expandaftersplit@internalgetrefnumber{#1@cref}@nameendcsname getrefnumber{#1}%
}%
}
makeatother
begin{document}
edeftmp{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}

add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f381513%2fobtain-expansion-of-cref%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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}
edeftemp{crtcref{sec}}
edefTemp{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.7 (being uploaded to CTAN today (2018/12/28), the crtcref etc. macros are from v0.1 however, v.0.7 contains a small bug fix for crtcref and crtCref, reporting wrong reference numbers.
1
Doesn't work really well with subsections for me. Whereas theCrefwas "Section 2.1.2", thecrtCrefis "Section 2" (usingcrtnumberalso yields 2).
– Norswap
6 hours ago
@Norswap: Oh my, there is another new version ofcleverefapparently that was released one week aftercrossreftools v.0.6-- I will investigate and update accordingly. I am pretty sure that it worked previously!. Thanks for notifying me!
– Christian Hupfer
5 hours ago
@Norswap: Ok, I found a small typo incrtcrefandcrtCref, using the wrong information. Please note, thatcrefitself still reportssectionfor subsection etc. references, as long as there is no propercrefnameetc. setup -- I've uploadedcrossreftools 0.7to CTAN, it should be available soon there and on TeXLive/MikTeX
– Christian Hupfer
2 hours ago
add a comment |
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}
edeftemp{crtcref{sec}}
edefTemp{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.7 (being uploaded to CTAN today (2018/12/28), the crtcref etc. macros are from v0.1 however, v.0.7 contains a small bug fix for crtcref and crtCref, reporting wrong reference numbers.
1
Doesn't work really well with subsections for me. Whereas theCrefwas "Section 2.1.2", thecrtCrefis "Section 2" (usingcrtnumberalso yields 2).
– Norswap
6 hours ago
@Norswap: Oh my, there is another new version ofcleverefapparently that was released one week aftercrossreftools v.0.6-- I will investigate and update accordingly. I am pretty sure that it worked previously!. Thanks for notifying me!
– Christian Hupfer
5 hours ago
@Norswap: Ok, I found a small typo incrtcrefandcrtCref, using the wrong information. Please note, thatcrefitself still reportssectionfor subsection etc. references, as long as there is no propercrefnameetc. setup -- I've uploadedcrossreftools 0.7to CTAN, it should be available soon there and on TeXLive/MikTeX
– Christian Hupfer
2 hours ago
add a comment |
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}
edeftemp{crtcref{sec}}
edefTemp{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.7 (being uploaded to CTAN today (2018/12/28), the crtcref etc. macros are from v0.1 however, v.0.7 contains a small bug fix for crtcref and crtCref, reporting wrong reference numbers.
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}
edeftemp{crtcref{sec}}
edefTemp{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.7 (being uploaded to CTAN today (2018/12/28), the crtcref etc. macros are from v0.1 however, v.0.7 contains a small bug fix for crtcref and crtCref, reporting wrong reference numbers.
edited 42 mins ago
answered Dec 26 '17 at 19:35
Christian Hupfer
147k14192384
147k14192384
1
Doesn't work really well with subsections for me. Whereas theCrefwas "Section 2.1.2", thecrtCrefis "Section 2" (usingcrtnumberalso yields 2).
– Norswap
6 hours ago
@Norswap: Oh my, there is another new version ofcleverefapparently that was released one week aftercrossreftools v.0.6-- I will investigate and update accordingly. I am pretty sure that it worked previously!. Thanks for notifying me!
– Christian Hupfer
5 hours ago
@Norswap: Ok, I found a small typo incrtcrefandcrtCref, using the wrong information. Please note, thatcrefitself still reportssectionfor subsection etc. references, as long as there is no propercrefnameetc. setup -- I've uploadedcrossreftools 0.7to CTAN, it should be available soon there and on TeXLive/MikTeX
– Christian Hupfer
2 hours ago
add a comment |
1
Doesn't work really well with subsections for me. Whereas theCrefwas "Section 2.1.2", thecrtCrefis "Section 2" (usingcrtnumberalso yields 2).
– Norswap
6 hours ago
@Norswap: Oh my, there is another new version ofcleverefapparently that was released one week aftercrossreftools v.0.6-- I will investigate and update accordingly. I am pretty sure that it worked previously!. Thanks for notifying me!
– Christian Hupfer
5 hours ago
@Norswap: Ok, I found a small typo incrtcrefandcrtCref, using the wrong information. Please note, thatcrefitself still reportssectionfor subsection etc. references, as long as there is no propercrefnameetc. setup -- I've uploadedcrossreftools 0.7to CTAN, it should be available soon there and on TeXLive/MikTeX
– Christian Hupfer
2 hours ago
1
1
Doesn't work really well with subsections for me. Whereas the
Cref was "Section 2.1.2", the crtCref is "Section 2" (using crtnumberalso yields 2).– Norswap
6 hours ago
Doesn't work really well with subsections for me. Whereas the
Cref was "Section 2.1.2", the crtCref is "Section 2" (using crtnumberalso yields 2).– Norswap
6 hours ago
@Norswap: Oh my, there is another new version of
cleveref apparently that was released one week after crossreftools v.0.6 -- I will investigate and update accordingly. I am pretty sure that it worked previously!. Thanks for notifying me!– Christian Hupfer
5 hours ago
@Norswap: Oh my, there is another new version of
cleveref apparently that was released one week after crossreftools v.0.6 -- I will investigate and update accordingly. I am pretty sure that it worked previously!. Thanks for notifying me!– Christian Hupfer
5 hours ago
@Norswap: Ok, I found a small typo in
crtcref and crtCref, using the wrong information. Please note, that cref itself still reports section for subsection etc. references, as long as there is no proper crefname etc. setup -- I've uploaded crossreftools 0.7 to CTAN, it should be available soon there and on TeXLive/MikTeX– Christian Hupfer
2 hours ago
@Norswap: Ok, I found a small typo in
crtcref and crtCref, using the wrong information. Please note, that cref itself still reports section for subsection etc. references, as long as there is no proper crefname etc. setup -- I've uploaded crossreftools 0.7 to CTAN, it should be available soon there and on TeXLive/MikTeX– Christian Hupfer
2 hours ago
add a comment |
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.
cleverefstores labels with the@crefsuffix, solabel{sec}generates the label namessecandsec@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 (sectionhere,[2]stands for the section number which is2here and, however, I could not figure out the meaning of2yet.
getrefnumberbyrefcountpackage extracts the content stored under the label name, which is more complicated in the case ofcleveref, but using a 'splitter' macro namedsplit@@internalthat leaves the content within the firstonly in the input stream, it is possible to extract the counter type, i.e.sectionhere.
cleverefdefinescref@section@nameandCref@section@nameas macros that holds the singular name ofsectionandSection, similarly for other counters, depending on the arguments tocrefnameandCrefname.
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
defsplit@@internal[#1][#2][#3]#4{#1}%
defsplit@internal#1{%
expandaftersplit@@internal#1%
}
newcommand{Xcref}[1]{%
IfRefUndefinedExpandable{#1@cref}{%
}{%
csname Cref@expandaftersplit@internalgetrefnumber{#1@cref}@nameendcsname getrefnumber{#1}%
}%
}
newcommand{xcref}[1]{%
IfRefUndefinedExpandable{#1@cref}{%
}{%
csname cref@expandaftersplit@internalgetrefnumber{#1@cref}@nameendcsname getrefnumber{#1}%
}%
}
makeatother
begin{document}
edeftmp{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}

add a comment |
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.
cleverefstores labels with the@crefsuffix, solabel{sec}generates the label namessecandsec@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 (sectionhere,[2]stands for the section number which is2here and, however, I could not figure out the meaning of2yet.
getrefnumberbyrefcountpackage extracts the content stored under the label name, which is more complicated in the case ofcleveref, but using a 'splitter' macro namedsplit@@internalthat leaves the content within the firstonly in the input stream, it is possible to extract the counter type, i.e.sectionhere.
cleverefdefinescref@section@nameandCref@section@nameas macros that holds the singular name ofsectionandSection, similarly for other counters, depending on the arguments tocrefnameandCrefname.
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
defsplit@@internal[#1][#2][#3]#4{#1}%
defsplit@internal#1{%
expandaftersplit@@internal#1%
}
newcommand{Xcref}[1]{%
IfRefUndefinedExpandable{#1@cref}{%
}{%
csname Cref@expandaftersplit@internalgetrefnumber{#1@cref}@nameendcsname getrefnumber{#1}%
}%
}
newcommand{xcref}[1]{%
IfRefUndefinedExpandable{#1@cref}{%
}{%
csname cref@expandaftersplit@internalgetrefnumber{#1@cref}@nameendcsname getrefnumber{#1}%
}%
}
makeatother
begin{document}
edeftmp{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}

add a comment |
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.
cleverefstores labels with the@crefsuffix, solabel{sec}generates the label namessecandsec@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 (sectionhere,[2]stands for the section number which is2here and, however, I could not figure out the meaning of2yet.
getrefnumberbyrefcountpackage extracts the content stored under the label name, which is more complicated in the case ofcleveref, but using a 'splitter' macro namedsplit@@internalthat leaves the content within the firstonly in the input stream, it is possible to extract the counter type, i.e.sectionhere.
cleverefdefinescref@section@nameandCref@section@nameas macros that holds the singular name ofsectionandSection, similarly for other counters, depending on the arguments tocrefnameandCrefname.
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
defsplit@@internal[#1][#2][#3]#4{#1}%
defsplit@internal#1{%
expandaftersplit@@internal#1%
}
newcommand{Xcref}[1]{%
IfRefUndefinedExpandable{#1@cref}{%
}{%
csname Cref@expandaftersplit@internalgetrefnumber{#1@cref}@nameendcsname getrefnumber{#1}%
}%
}
newcommand{xcref}[1]{%
IfRefUndefinedExpandable{#1@cref}{%
}{%
csname cref@expandaftersplit@internalgetrefnumber{#1@cref}@nameendcsname getrefnumber{#1}%
}%
}
makeatother
begin{document}
edeftmp{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}

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.
cleverefstores labels with the@crefsuffix, solabel{sec}generates the label namessecandsec@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 (sectionhere,[2]stands for the section number which is2here and, however, I could not figure out the meaning of2yet.
getrefnumberbyrefcountpackage extracts the content stored under the label name, which is more complicated in the case ofcleveref, but using a 'splitter' macro namedsplit@@internalthat leaves the content within the firstonly in the input stream, it is possible to extract the counter type, i.e.sectionhere.
cleverefdefinescref@section@nameandCref@section@nameas macros that holds the singular name ofsectionandSection, similarly for other counters, depending on the arguments tocrefnameandCrefname.
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
defsplit@@internal[#1][#2][#3]#4{#1}%
defsplit@internal#1{%
expandaftersplit@@internal#1%
}
newcommand{Xcref}[1]{%
IfRefUndefinedExpandable{#1@cref}{%
}{%
csname Cref@expandaftersplit@internalgetrefnumber{#1@cref}@nameendcsname getrefnumber{#1}%
}%
}
newcommand{xcref}[1]{%
IfRefUndefinedExpandable{#1@cref}{%
}{%
csname cref@expandaftersplit@internalgetrefnumber{#1@cref}@nameendcsname getrefnumber{#1}%
}%
}
makeatother
begin{document}
edeftmp{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}

edited Sep 17 '17 at 6:33
answered Sep 16 '17 at 22:06
Christian Hupfer
147k14192384
147k14192384
add a comment |
add a comment |
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f381513%2fobtain-expansion-of-cref%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
To give some background info: can you tell use what you are trying to do with the macro?
– samcarter
Jul 20 '17 at 12:57
1
This can't work, but perhaps there are other methods. They might depend on the intended usage.
– egreg
Jul 20 '17 at 12:57
The main reason is that I have something like
section{Comments on cref{other_section}}and I would like that the bookmark in the PDF is resolved toComments on Section 3.– gerw
Jul 20 '17 at 12:58
see tex.stackexchange.com/a/138950/36296
– samcarter
Jul 20 '17 at 13:20
or tex.stackexchange.com/questions/235516/…
– samcarter
Jul 20 '17 at 13:20