How can I get cleveref to surround references with parentheses automatically?












3















I use cleveref for in-document references, and find it to be clever. However, I typically want to refer to figures (for example) within parentheses. What I do now is this



usepackage{cleveref}
...
X exhibits Y (cref{fig:foo}).


but what I would like to do is something like



X exhibits Y cref{fig:foo}. % Parentheses omitted in code but not in output


Ideally I would have some way to omit parentheses in output when needed.










share|improve this question







New contributor




August Janse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    A quick (not good) solution: Redefining cref on itself.

    – JouleV
    16 hours ago











  • Welcome to the site! :)

    – cmhughes
    15 hours ago











  • @JouleV Nice suggestion. It looks to me like this is the cleanest solution, as I reason in my comment to @cmhughes' answer. It also leaves the original cref for parenthesis-less refs. Is there any good reason against?

    – August Janse
    15 hours ago






  • 1





    @AugustJanse If you want, this is the solution: makeatletter letmy@commandcref renewcommandcref[1]{[my@command{#1}]} makeatother

    – JouleV
    14 hours ago
















3















I use cleveref for in-document references, and find it to be clever. However, I typically want to refer to figures (for example) within parentheses. What I do now is this



usepackage{cleveref}
...
X exhibits Y (cref{fig:foo}).


but what I would like to do is something like



X exhibits Y cref{fig:foo}. % Parentheses omitted in code but not in output


Ideally I would have some way to omit parentheses in output when needed.










share|improve this question







New contributor




August Janse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    A quick (not good) solution: Redefining cref on itself.

    – JouleV
    16 hours ago











  • Welcome to the site! :)

    – cmhughes
    15 hours ago











  • @JouleV Nice suggestion. It looks to me like this is the cleanest solution, as I reason in my comment to @cmhughes' answer. It also leaves the original cref for parenthesis-less refs. Is there any good reason against?

    – August Janse
    15 hours ago






  • 1





    @AugustJanse If you want, this is the solution: makeatletter letmy@commandcref renewcommandcref[1]{[my@command{#1}]} makeatother

    – JouleV
    14 hours ago














3












3








3








I use cleveref for in-document references, and find it to be clever. However, I typically want to refer to figures (for example) within parentheses. What I do now is this



usepackage{cleveref}
...
X exhibits Y (cref{fig:foo}).


but what I would like to do is something like



X exhibits Y cref{fig:foo}. % Parentheses omitted in code but not in output


Ideally I would have some way to omit parentheses in output when needed.










share|improve this question







New contributor




August Janse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I use cleveref for in-document references, and find it to be clever. However, I typically want to refer to figures (for example) within parentheses. What I do now is this



usepackage{cleveref}
...
X exhibits Y (cref{fig:foo}).


but what I would like to do is something like



X exhibits Y cref{fig:foo}. % Parentheses omitted in code but not in output


Ideally I would have some way to omit parentheses in output when needed.







cleveref parentheses






share|improve this question







New contributor




August Janse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




August Janse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




August Janse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 16 hours ago









August JanseAugust Janse

1164




1164




New contributor




August Janse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





August Janse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






August Janse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1





    A quick (not good) solution: Redefining cref on itself.

    – JouleV
    16 hours ago











  • Welcome to the site! :)

    – cmhughes
    15 hours ago











  • @JouleV Nice suggestion. It looks to me like this is the cleanest solution, as I reason in my comment to @cmhughes' answer. It also leaves the original cref for parenthesis-less refs. Is there any good reason against?

    – August Janse
    15 hours ago






  • 1





    @AugustJanse If you want, this is the solution: makeatletter letmy@commandcref renewcommandcref[1]{[my@command{#1}]} makeatother

    – JouleV
    14 hours ago














  • 1





    A quick (not good) solution: Redefining cref on itself.

    – JouleV
    16 hours ago











  • Welcome to the site! :)

    – cmhughes
    15 hours ago











  • @JouleV Nice suggestion. It looks to me like this is the cleanest solution, as I reason in my comment to @cmhughes' answer. It also leaves the original cref for parenthesis-less refs. Is there any good reason against?

    – August Janse
    15 hours ago






  • 1





    @AugustJanse If you want, this is the solution: makeatletter letmy@commandcref renewcommandcref[1]{[my@command{#1}]} makeatother

    – JouleV
    14 hours ago








1




1





A quick (not good) solution: Redefining cref on itself.

– JouleV
16 hours ago





A quick (not good) solution: Redefining cref on itself.

– JouleV
16 hours ago













Welcome to the site! :)

– cmhughes
15 hours ago





Welcome to the site! :)

– cmhughes
15 hours ago













@JouleV Nice suggestion. It looks to me like this is the cleanest solution, as I reason in my comment to @cmhughes' answer. It also leaves the original cref for parenthesis-less refs. Is there any good reason against?

– August Janse
15 hours ago





@JouleV Nice suggestion. It looks to me like this is the cleanest solution, as I reason in my comment to @cmhughes' answer. It also leaves the original cref for parenthesis-less refs. Is there any good reason against?

– August Janse
15 hours ago




1




1





@AugustJanse If you want, this is the solution: makeatletter letmy@commandcref renewcommandcref[1]{[my@command{#1}]} makeatother

– JouleV
14 hours ago





@AugustJanse If you want, this is the solution: makeatletter letmy@commandcref renewcommandcref[1]{[my@command{#1}]} makeatother

– JouleV
14 hours ago










2 Answers
2






active

oldest

votes


















5














Referencing Section 8.2.1 of the cleveref documention,




Cross-reference formats for single cross-references are defined or
redefined using the crefformat and Crefformat commands, which are
used by the cref and Cref commands respectively. These take two
arguments: the cross-reference type, and the formatting code:
crefformat{type}{format}




For your purposes, we can use, for example:



crefformat{figure}{(Figure~#2#1#3)}
Crefformat{figure}{Figure~#2#1#3}


Note that I've used crefformat for use with the cref command (not beginning of sentence), and Crefformat for use with the Cref command (beginning of sentence).



This gives



enter image description here



A complete MWE follows



% arara: pdflatex
% arara: pdflatex
documentclass{article}
usepackage{cleveref}
crefformat{figure}{(Figure~#2#1#3)}
Crefformat{figure}{Figure~#2#1#3}
begin{document}

begin{figure}[!h]
rule{3mm}{2mm}
caption{August}
label{fig:foo}
end{figure}

X exhibits Y cref{fig:foo}.

Cref{fig:foo} is a cross reference at the beginning of a sentence
end{document}





share|improve this answer
























  • Thank you! This certainly is a solution, but it's a bit unwieldy. I would need to crefformat every type of object (tables for example) and rewrite a few sentences to have the ref at the beginning, or alternatively reset the format before and after. I was hopeful for crefdefaultlabelformat, but it looks like that only affects the label and not the name. Reading the part of the documentation you referred to, it looks like there's no better solution using cleveref.

    – August Janse
    15 hours ago



















3














I would not modify the workings of cref and friends directly. Instead, I would create a new macro called pcref -- short for "parenthetic cref", I suppose -- as follows:



newcommandpcref[1]{(cref{#1})}


As you can probably guess, pcref places round parentheses around the output of cref. No need for lots of separate crefformat directives. This approach preserves access to the standard definition of cref, should the need to do so arise.



For the following screenshot, I loaded the hyperref package and specified the cleveref option nameinlink, in order to make visible what is, and is not, produced by cref.



enter image description here



documentclass{article}
usepackage[colorlinks,allcolors=blue]{hyperref} % optional
usepackage[noabbrev,nameinlink]{cleveref}
newcommandpcref[1]{(cref{#1})}
begin{document}
begin{figure}[t!] caption{foo}label{fig:foo}end{figure}
begin{figure}[h!] caption{bar}label{fig:bar}end{figure}
begin{table}[h!] caption{foo}label{tab:foo}end{table}

dots pcref{fig:foo}, pcref{fig:foo,fig:bar}, pcref{tab:foo,fig:bar}, dots

bigskip vs.

bigskip
dots cref{fig:foo}, cref{fig:foo,fig:bar}, cref{tab:foo,fig:bar}, dots

end{document}





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


    }
    });






    August Janse is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f484093%2fhow-can-i-get-cleveref-to-surround-references-with-parentheses-automatically%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









    5














    Referencing Section 8.2.1 of the cleveref documention,




    Cross-reference formats for single cross-references are defined or
    redefined using the crefformat and Crefformat commands, which are
    used by the cref and Cref commands respectively. These take two
    arguments: the cross-reference type, and the formatting code:
    crefformat{type}{format}




    For your purposes, we can use, for example:



    crefformat{figure}{(Figure~#2#1#3)}
    Crefformat{figure}{Figure~#2#1#3}


    Note that I've used crefformat for use with the cref command (not beginning of sentence), and Crefformat for use with the Cref command (beginning of sentence).



    This gives



    enter image description here



    A complete MWE follows



    % arara: pdflatex
    % arara: pdflatex
    documentclass{article}
    usepackage{cleveref}
    crefformat{figure}{(Figure~#2#1#3)}
    Crefformat{figure}{Figure~#2#1#3}
    begin{document}

    begin{figure}[!h]
    rule{3mm}{2mm}
    caption{August}
    label{fig:foo}
    end{figure}

    X exhibits Y cref{fig:foo}.

    Cref{fig:foo} is a cross reference at the beginning of a sentence
    end{document}





    share|improve this answer
























    • Thank you! This certainly is a solution, but it's a bit unwieldy. I would need to crefformat every type of object (tables for example) and rewrite a few sentences to have the ref at the beginning, or alternatively reset the format before and after. I was hopeful for crefdefaultlabelformat, but it looks like that only affects the label and not the name. Reading the part of the documentation you referred to, it looks like there's no better solution using cleveref.

      – August Janse
      15 hours ago
















    5














    Referencing Section 8.2.1 of the cleveref documention,




    Cross-reference formats for single cross-references are defined or
    redefined using the crefformat and Crefformat commands, which are
    used by the cref and Cref commands respectively. These take two
    arguments: the cross-reference type, and the formatting code:
    crefformat{type}{format}




    For your purposes, we can use, for example:



    crefformat{figure}{(Figure~#2#1#3)}
    Crefformat{figure}{Figure~#2#1#3}


    Note that I've used crefformat for use with the cref command (not beginning of sentence), and Crefformat for use with the Cref command (beginning of sentence).



    This gives



    enter image description here



    A complete MWE follows



    % arara: pdflatex
    % arara: pdflatex
    documentclass{article}
    usepackage{cleveref}
    crefformat{figure}{(Figure~#2#1#3)}
    Crefformat{figure}{Figure~#2#1#3}
    begin{document}

    begin{figure}[!h]
    rule{3mm}{2mm}
    caption{August}
    label{fig:foo}
    end{figure}

    X exhibits Y cref{fig:foo}.

    Cref{fig:foo} is a cross reference at the beginning of a sentence
    end{document}





    share|improve this answer
























    • Thank you! This certainly is a solution, but it's a bit unwieldy. I would need to crefformat every type of object (tables for example) and rewrite a few sentences to have the ref at the beginning, or alternatively reset the format before and after. I was hopeful for crefdefaultlabelformat, but it looks like that only affects the label and not the name. Reading the part of the documentation you referred to, it looks like there's no better solution using cleveref.

      – August Janse
      15 hours ago














    5












    5








    5







    Referencing Section 8.2.1 of the cleveref documention,




    Cross-reference formats for single cross-references are defined or
    redefined using the crefformat and Crefformat commands, which are
    used by the cref and Cref commands respectively. These take two
    arguments: the cross-reference type, and the formatting code:
    crefformat{type}{format}




    For your purposes, we can use, for example:



    crefformat{figure}{(Figure~#2#1#3)}
    Crefformat{figure}{Figure~#2#1#3}


    Note that I've used crefformat for use with the cref command (not beginning of sentence), and Crefformat for use with the Cref command (beginning of sentence).



    This gives



    enter image description here



    A complete MWE follows



    % arara: pdflatex
    % arara: pdflatex
    documentclass{article}
    usepackage{cleveref}
    crefformat{figure}{(Figure~#2#1#3)}
    Crefformat{figure}{Figure~#2#1#3}
    begin{document}

    begin{figure}[!h]
    rule{3mm}{2mm}
    caption{August}
    label{fig:foo}
    end{figure}

    X exhibits Y cref{fig:foo}.

    Cref{fig:foo} is a cross reference at the beginning of a sentence
    end{document}





    share|improve this answer













    Referencing Section 8.2.1 of the cleveref documention,




    Cross-reference formats for single cross-references are defined or
    redefined using the crefformat and Crefformat commands, which are
    used by the cref and Cref commands respectively. These take two
    arguments: the cross-reference type, and the formatting code:
    crefformat{type}{format}




    For your purposes, we can use, for example:



    crefformat{figure}{(Figure~#2#1#3)}
    Crefformat{figure}{Figure~#2#1#3}


    Note that I've used crefformat for use with the cref command (not beginning of sentence), and Crefformat for use with the Cref command (beginning of sentence).



    This gives



    enter image description here



    A complete MWE follows



    % arara: pdflatex
    % arara: pdflatex
    documentclass{article}
    usepackage{cleveref}
    crefformat{figure}{(Figure~#2#1#3)}
    Crefformat{figure}{Figure~#2#1#3}
    begin{document}

    begin{figure}[!h]
    rule{3mm}{2mm}
    caption{August}
    label{fig:foo}
    end{figure}

    X exhibits Y cref{fig:foo}.

    Cref{fig:foo} is a cross reference at the beginning of a sentence
    end{document}






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 16 hours ago









    cmhughescmhughes

    79.7k17201300




    79.7k17201300













    • Thank you! This certainly is a solution, but it's a bit unwieldy. I would need to crefformat every type of object (tables for example) and rewrite a few sentences to have the ref at the beginning, or alternatively reset the format before and after. I was hopeful for crefdefaultlabelformat, but it looks like that only affects the label and not the name. Reading the part of the documentation you referred to, it looks like there's no better solution using cleveref.

      – August Janse
      15 hours ago



















    • Thank you! This certainly is a solution, but it's a bit unwieldy. I would need to crefformat every type of object (tables for example) and rewrite a few sentences to have the ref at the beginning, or alternatively reset the format before and after. I was hopeful for crefdefaultlabelformat, but it looks like that only affects the label and not the name. Reading the part of the documentation you referred to, it looks like there's no better solution using cleveref.

      – August Janse
      15 hours ago

















    Thank you! This certainly is a solution, but it's a bit unwieldy. I would need to crefformat every type of object (tables for example) and rewrite a few sentences to have the ref at the beginning, or alternatively reset the format before and after. I was hopeful for crefdefaultlabelformat, but it looks like that only affects the label and not the name. Reading the part of the documentation you referred to, it looks like there's no better solution using cleveref.

    – August Janse
    15 hours ago





    Thank you! This certainly is a solution, but it's a bit unwieldy. I would need to crefformat every type of object (tables for example) and rewrite a few sentences to have the ref at the beginning, or alternatively reset the format before and after. I was hopeful for crefdefaultlabelformat, but it looks like that only affects the label and not the name. Reading the part of the documentation you referred to, it looks like there's no better solution using cleveref.

    – August Janse
    15 hours ago











    3














    I would not modify the workings of cref and friends directly. Instead, I would create a new macro called pcref -- short for "parenthetic cref", I suppose -- as follows:



    newcommandpcref[1]{(cref{#1})}


    As you can probably guess, pcref places round parentheses around the output of cref. No need for lots of separate crefformat directives. This approach preserves access to the standard definition of cref, should the need to do so arise.



    For the following screenshot, I loaded the hyperref package and specified the cleveref option nameinlink, in order to make visible what is, and is not, produced by cref.



    enter image description here



    documentclass{article}
    usepackage[colorlinks,allcolors=blue]{hyperref} % optional
    usepackage[noabbrev,nameinlink]{cleveref}
    newcommandpcref[1]{(cref{#1})}
    begin{document}
    begin{figure}[t!] caption{foo}label{fig:foo}end{figure}
    begin{figure}[h!] caption{bar}label{fig:bar}end{figure}
    begin{table}[h!] caption{foo}label{tab:foo}end{table}

    dots pcref{fig:foo}, pcref{fig:foo,fig:bar}, pcref{tab:foo,fig:bar}, dots

    bigskip vs.

    bigskip
    dots cref{fig:foo}, cref{fig:foo,fig:bar}, cref{tab:foo,fig:bar}, dots

    end{document}





    share|improve this answer




























      3














      I would not modify the workings of cref and friends directly. Instead, I would create a new macro called pcref -- short for "parenthetic cref", I suppose -- as follows:



      newcommandpcref[1]{(cref{#1})}


      As you can probably guess, pcref places round parentheses around the output of cref. No need for lots of separate crefformat directives. This approach preserves access to the standard definition of cref, should the need to do so arise.



      For the following screenshot, I loaded the hyperref package and specified the cleveref option nameinlink, in order to make visible what is, and is not, produced by cref.



      enter image description here



      documentclass{article}
      usepackage[colorlinks,allcolors=blue]{hyperref} % optional
      usepackage[noabbrev,nameinlink]{cleveref}
      newcommandpcref[1]{(cref{#1})}
      begin{document}
      begin{figure}[t!] caption{foo}label{fig:foo}end{figure}
      begin{figure}[h!] caption{bar}label{fig:bar}end{figure}
      begin{table}[h!] caption{foo}label{tab:foo}end{table}

      dots pcref{fig:foo}, pcref{fig:foo,fig:bar}, pcref{tab:foo,fig:bar}, dots

      bigskip vs.

      bigskip
      dots cref{fig:foo}, cref{fig:foo,fig:bar}, cref{tab:foo,fig:bar}, dots

      end{document}





      share|improve this answer


























        3












        3








        3







        I would not modify the workings of cref and friends directly. Instead, I would create a new macro called pcref -- short for "parenthetic cref", I suppose -- as follows:



        newcommandpcref[1]{(cref{#1})}


        As you can probably guess, pcref places round parentheses around the output of cref. No need for lots of separate crefformat directives. This approach preserves access to the standard definition of cref, should the need to do so arise.



        For the following screenshot, I loaded the hyperref package and specified the cleveref option nameinlink, in order to make visible what is, and is not, produced by cref.



        enter image description here



        documentclass{article}
        usepackage[colorlinks,allcolors=blue]{hyperref} % optional
        usepackage[noabbrev,nameinlink]{cleveref}
        newcommandpcref[1]{(cref{#1})}
        begin{document}
        begin{figure}[t!] caption{foo}label{fig:foo}end{figure}
        begin{figure}[h!] caption{bar}label{fig:bar}end{figure}
        begin{table}[h!] caption{foo}label{tab:foo}end{table}

        dots pcref{fig:foo}, pcref{fig:foo,fig:bar}, pcref{tab:foo,fig:bar}, dots

        bigskip vs.

        bigskip
        dots cref{fig:foo}, cref{fig:foo,fig:bar}, cref{tab:foo,fig:bar}, dots

        end{document}





        share|improve this answer













        I would not modify the workings of cref and friends directly. Instead, I would create a new macro called pcref -- short for "parenthetic cref", I suppose -- as follows:



        newcommandpcref[1]{(cref{#1})}


        As you can probably guess, pcref places round parentheses around the output of cref. No need for lots of separate crefformat directives. This approach preserves access to the standard definition of cref, should the need to do so arise.



        For the following screenshot, I loaded the hyperref package and specified the cleveref option nameinlink, in order to make visible what is, and is not, produced by cref.



        enter image description here



        documentclass{article}
        usepackage[colorlinks,allcolors=blue]{hyperref} % optional
        usepackage[noabbrev,nameinlink]{cleveref}
        newcommandpcref[1]{(cref{#1})}
        begin{document}
        begin{figure}[t!] caption{foo}label{fig:foo}end{figure}
        begin{figure}[h!] caption{bar}label{fig:bar}end{figure}
        begin{table}[h!] caption{foo}label{tab:foo}end{table}

        dots pcref{fig:foo}, pcref{fig:foo,fig:bar}, pcref{tab:foo,fig:bar}, dots

        bigskip vs.

        bigskip
        dots cref{fig:foo}, cref{fig:foo,fig:bar}, cref{tab:foo,fig:bar}, dots

        end{document}






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 13 hours ago









        MicoMico

        286k32390779




        286k32390779






















            August Janse is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            August Janse is a new contributor. Be nice, and check out our Code of Conduct.













            August Janse is a new contributor. Be nice, and check out our Code of Conduct.












            August Janse is a new contributor. Be nice, and check out our Code of Conduct.
















            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f484093%2fhow-can-i-get-cleveref-to-surround-references-with-parentheses-automatically%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

            How to label and detect the document text images

            Vallis Paradisi

            Tabula Rosettana