Obtain expansion of cref












6














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.










share|improve this question




















  • 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 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










  • or tex.stackexchange.com/questions/235516/…
    – samcarter
    Jul 20 '17 at 13:20
















6














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.










share|improve this question




















  • 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 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










  • or tex.stackexchange.com/questions/235516/…
    – samcarter
    Jul 20 '17 at 13:20














6












6








6


0





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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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










  • or tex.stackexchange.com/questions/235516/…
    – samcarter
    Jul 20 '17 at 13:20














  • 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 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










  • 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










2 Answers
2






active

oldest

votes


















6














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.



enter image description here



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.






share|improve this answer



















  • 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










  • @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



















5














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.





  1. cleveref stores labels with the @cref suffix, so label{sec}generates the label names sec and sec@cref.


  2. 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 is 2 here and, however, I could not figure out the meaning of 2 yet.



  3. getrefnumber by refcount package extracts the content stored under the label name, which is more complicated in the case of cleveref, 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.



  4. cleveref defines cref@section@name and Cref@section@name as macros that holds the singular name of section and Section, 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
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}


enter image description here






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    6














    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.



    enter image description here



    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.






    share|improve this answer



















    • 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










    • @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
















    6














    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.



    enter image description here



    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.






    share|improve this answer



















    • 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










    • @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














    6












    6








    6






    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.



    enter image description here



    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.






    share|improve this answer














    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.



    enter image description here



    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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 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: 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














    • 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










    • @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








    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











    5














    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.





    1. cleveref stores labels with the @cref suffix, so label{sec}generates the label names sec and sec@cref.


    2. 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 is 2 here and, however, I could not figure out the meaning of 2 yet.



    3. getrefnumber by refcount package extracts the content stored under the label name, which is more complicated in the case of cleveref, 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.



    4. cleveref defines cref@section@name and Cref@section@name as macros that holds the singular name of section and Section, 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
    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}


    enter image description here






    share|improve this answer




























      5














      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.





      1. cleveref stores labels with the @cref suffix, so label{sec}generates the label names sec and sec@cref.


      2. 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 is 2 here and, however, I could not figure out the meaning of 2 yet.



      3. getrefnumber by refcount package extracts the content stored under the label name, which is more complicated in the case of cleveref, 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.



      4. cleveref defines cref@section@name and Cref@section@name as macros that holds the singular name of section and Section, 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
      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}


      enter image description here






      share|improve this answer


























        5












        5








        5






        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.





        1. cleveref stores labels with the @cref suffix, so label{sec}generates the label names sec and sec@cref.


        2. 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 is 2 here and, however, I could not figure out the meaning of 2 yet.



        3. getrefnumber by refcount package extracts the content stored under the label name, which is more complicated in the case of cleveref, 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.



        4. cleveref defines cref@section@name and Cref@section@name as macros that holds the singular name of section and Section, 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
        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}


        enter image description here






        share|improve this answer














        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.





        1. cleveref stores labels with the @cref suffix, so label{sec}generates the label names sec and sec@cref.


        2. 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 is 2 here and, however, I could not figure out the meaning of 2 yet.



        3. getrefnumber by refcount package extracts the content stored under the label name, which is more complicated in the case of cleveref, 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.



        4. cleveref defines cref@section@name and Cref@section@name as macros that holds the singular name of section and Section, 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
        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}


        enter image description here







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Sep 17 '17 at 6:33

























        answered Sep 16 '17 at 22:06









        Christian Hupfer

        147k14192384




        147k14192384






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Accessing regular linux commands in Huawei's Dopra Linux

            Can't connect RFCOMM socket: Host is down

            Kernel panic - not syncing: Fatal Exception in Interrupt