How can I get cleveref to surround references with parentheses automatically?
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
New contributor
add a comment |
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
New contributor
1
A quick (not good) solution: Redefiningcref
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 originalcref
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
add a comment |
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
New contributor
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
cleveref parentheses
New contributor
New contributor
New contributor
asked 16 hours ago
August JanseAugust Janse
1164
1164
New contributor
New contributor
1
A quick (not good) solution: Redefiningcref
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 originalcref
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
add a comment |
1
A quick (not good) solution: Redefiningcref
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 originalcref
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
add a comment |
2 Answers
2
active
oldest
votes
Referencing Section 8.2.1 of the cleveref
documention,
Cross-reference formats for single cross-references are defined or
redefined using thecrefformat
andCrefformat
commands, which are
used by thecref
andCref
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
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}
Thank you! This certainly is a solution, but it's a bit unwieldy. I would need tocrefformat
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 forcrefdefaultlabelformat
, 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 usingcleveref
.
– August Janse
15 hours ago
add a comment |
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
.
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}
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
});
}
});
August Janse is a new contributor. Be nice, and check out our Code of Conduct.
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%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
Referencing Section 8.2.1 of the cleveref
documention,
Cross-reference formats for single cross-references are defined or
redefined using thecrefformat
andCrefformat
commands, which are
used by thecref
andCref
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
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}
Thank you! This certainly is a solution, but it's a bit unwieldy. I would need tocrefformat
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 forcrefdefaultlabelformat
, 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 usingcleveref
.
– August Janse
15 hours ago
add a comment |
Referencing Section 8.2.1 of the cleveref
documention,
Cross-reference formats for single cross-references are defined or
redefined using thecrefformat
andCrefformat
commands, which are
used by thecref
andCref
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
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}
Thank you! This certainly is a solution, but it's a bit unwieldy. I would need tocrefformat
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 forcrefdefaultlabelformat
, 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 usingcleveref
.
– August Janse
15 hours ago
add a comment |
Referencing Section 8.2.1 of the cleveref
documention,
Cross-reference formats for single cross-references are defined or
redefined using thecrefformat
andCrefformat
commands, which are
used by thecref
andCref
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
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}
Referencing Section 8.2.1 of the cleveref
documention,
Cross-reference formats for single cross-references are defined or
redefined using thecrefformat
andCrefformat
commands, which are
used by thecref
andCref
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
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}
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 tocrefformat
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 forcrefdefaultlabelformat
, 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 usingcleveref
.
– August Janse
15 hours ago
add a comment |
Thank you! This certainly is a solution, but it's a bit unwieldy. I would need tocrefformat
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 forcrefdefaultlabelformat
, 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 usingcleveref
.
– 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
add a comment |
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
.
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}
add a comment |
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
.
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}
add a comment |
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
.
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}
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
.
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}
answered 13 hours ago
MicoMico
286k32390779
286k32390779
add a comment |
add a comment |
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.
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.
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%2f484093%2fhow-can-i-get-cleveref-to-surround-references-with-parentheses-automatically%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
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