7

I want to change the style of the crossed reference (chapter, section, figure, equation, ...) generated by using cleveref package, for example, put parentheses around the referenced item and use boldface for the numbers.

\documentclass[14pt,a4paper]{extreport}
\usepackage[left=2.00cm, right=1.50cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage{hyperref}  
\hypersetup{colorlinks=true,citecolor=red, linkcolor=blue}
\usepackage{cleveref}
\usepackage{tcolorbox}
\begin{document}
\chapter{First  Chapter}\label{ch:chapter 1}
\section{Section One}\label{sec:section one}
\lipsum[1]
\section{Section Two}\label{sec:section two}
\begin{align}
r =& \sqrt{x^2 + y^2}\label{eq:equation 1}\\[2ex]
z =& \sqrt[3]{1 - 2r^2}\label{eq:equation 2}
\end{align}
see \cref{sec:section one} in \cref{ch:chapter 1}\\
\Cref{eq:equation 1} is the governing equations, \cref{eq:equation 2} is the second equation.
\end{document}
9
  • Should just the number of the item being cross-referenced be typeset in bold, or should the surrounding parentheses be typeset in bold as well? Please advise.
    – Mico
    Commented Aug 20, 2016 at 8:00
  • @ChristianHupfer I think \bfseries has no effect. I compiled with and without it and I saw no difference in the format.
    – Diaa
    Commented Aug 20, 2016 at 8:15
  • @DiaaAbidou: I see bold reference 'numbers', I could have placed the parentheses inside as well, to make them bold too, but that doesn't look nice, in my point of view
    – user31729
    Commented Aug 20, 2016 at 8:16
  • @ChristianHupfer can you spot the difference here imgur.com/a/uuY1W ? it is very subtle, if existed.
    – Diaa
    Commented Aug 20, 2016 at 8:22
  • @DiaaAbidou: Sorry, I had a different version: I meant: \crefdefaultlabelformat{({#2\bfseries#1#3})}, I used the other comment above from memory, not by copy and paste. #2 and #3 belong to hyperref parts and disable the \bfseries then.
    – user31729
    Commented Aug 20, 2016 at 8:27

2 Answers 2

7

If the numbers and the surrounding parentheses should be typeset in bold, add the instructions

\crefdefaultlabelformat{#2\bfseries\upshape(#1)#3}          
\creflabelformat{equation}{#2\bfseries\upshape(#1)#3}

after loading cleveref.

If only the numbers but not the surrounding parentheses should be typeset in bold, issue the instructions

\crefdefaultlabelformat{#2(\textbf{\textup{#1}})#3}
\creflabelformat{equation}{#2(\textbf{\textup{#1}})#3}

Observe the use of \upshape/\textup to make sure that the number (and, if required, the parentheses) are always typeset in the upright font shape.

A full MWE (minimum working example) that renders both the numbers and the surrounding parentheses in bold:

enter image description here

\documentclass[14pt,a4paper]{extreport}
\usepackage[left=2cm, right=1.5cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amsfonts,amssymb,graphicx,xcolor}

\usepackage{hyperref}  
\hypersetup{colorlinks=true,linkcolor=blue}

\usepackage{cleveref}
\crefdefaultlabelformat{#2\bfseries\upshape(#1)#3}
\creflabelformat{equation}{#2\bfseries\upshape(#1)#3}

\usepackage{tcolorbox}

\begin{document}
\chapter{First  Chapter}\label{ch:chapter 1}
\section{Section One}\label{sec:section one}
\section{Section Two}\label{sec:section two}
\begin{align}
r =& \sqrt{x^2 + y^2}\label{eq:equation 1}\\[2ex]
z =& \sqrt[3]{1 - 2r^2}\label{eq:equation 2}
\end{align}

See \cref{sec:section one} in \cref{ch:chapter 1}.

\Cref{eq:equation 1} is the governing equation, and  \cref{eq:equation 2} is the second equation.
\end{document}

Addendum to address the OP's follow-up comment: Use the instructions

\crefdefaultlabelformat{(#2\textbf{\textup{#1}}#3)}
\creflabelformat{equation}{(#2\textbf{\textup{#1}}#3)}

if only the cross-referenced numbers, but not the surrounding parentheses, should be rendered in bold and be made into hyperlink targets (and hence be rendered in blue, given the hyperref settings in force).

8
  • Perhaps you should add a normal \ref{ch:chapter 1} next to a \cref{...} to show the difference (especially the font change) ...
    – user31729
    Commented Aug 20, 2016 at 8:38
  • @DiaaAbidou - The screenshot you've produced uses a sans-serif font (which one??) that's not used in the OP's example. What does the screenshot look like if the default fonts are used?
    – Mico
    Commented Aug 20, 2016 at 8:44
  • @DiaaAbidou - If you copied and pasted the OP's code into ShareLaTeX, why would you expect to get the results from my code? I certainly wouldn't...
    – Mico
    Commented Aug 20, 2016 at 8:55
  • @Mico, I copied his main code while changing the format by your bold commands.
    – Diaa
    Commented Aug 20, 2016 at 8:57
  • 1
    @DiaaAbidou - Your ShareLaTeX setup appears to be somewhat non-standard. I have no idea which font family you use; it may be the case that the particular sans-serif font that you use cannot produce bold material. At any rate, the screenshot I have posted demonstrates that my code does render the cross-referenced numbers in bold.
    – Mico
    Commented Aug 20, 2016 at 9:02
5

For the parentheses, put this in preamble \crefdefaultlabelformat{#2(#1)#3} where #1 stands for the cited item as stated in cleveref documentation

\documentclass[14pt,a4paper]{extreport}
\usepackage[left=2.00cm, right=1.50cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{lipsum}
\usepackage{hyperref}  
\hypersetup{colorlinks=true,citecolor=red, linkcolor=blue}
\usepackage{cleveref}

\crefdefaultlabelformat{#2(#1)#3}


\begin{document}
\chapter{First  Chapter}\label{ch:chapter 1}
\section{Section One}\label{sec:section one}
\lipsum[1]
\section{Section Two}\label{sec:section two}
\begin{align}
r =& \sqrt{x^2 + y^2}\label{eq:equation 1}\\[2ex]
z =& \sqrt[3]{1 - 2r^2}\label{eq:equation 2}
\end{align}
see \cref{sec:section one} in \cref{ch:chapter 1}\\
\Cref{eq:equation 1} is the governing equations, \cref{eq:equation 2} is the second equation.
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .