5

I am using cleveref to cross-reference everything. Sometimes I write the reference inline like eq.(1). That is fine. But when it is enclosed in parenthesis it turns out like (eq.(2)). Which is not so nice.

Is it possible to somehow toggle the use of parenthesis in cleveref?

Here's an MWE:


\documentclass{article}
\usepackage{cleveref}
\begin{document}
 \begin{equation}\label{one}
 12+2=34
 \end{equation}
 \begin{equation}\label{two}
 1+2=4
 \end{equation}
 if \cref{one} is correct than (\cref{two})!
\end{document}  

1
  • 1
    Please add an example document showing exactly how are you obtaining those references. Aug 27, 2015 at 18:55

2 Answers 2

5

Declare a “variable” format:

\documentclass{article}
\usepackage{amsmath}
\usepackage{cleveref}

\crefformat{equation}{\eqA eq.~\eqB #2#1#3)}
\newcommand{\eqA}{}
\newcommand{\eqB}{(}
\DeclareRobustCommand{\pcref}[1]{%
  \begingroup
  \renewcommand{\eqA}{(}\renewcommand{\eqB}{}%
  \cref{#1}%
  \endgroup
}

\begin{document}

\begin{equation}
1+1=2 \label{true}
\end{equation}

Here's \cref{true}.

That's all \pcref{true}.

Again \cref{true}

\end{document}

enter image description here

3
  • Thankyou. Now I just have to go do this for all of my section, chapter, table, figure references as well :P Aug 27, 2015 at 20:04
  • Is it possible to make this take an optional argument, if I would like to add something like (figure 13a)? I mean adding the a manually without using subcaptions, if the letter is hardcoded in the figure. My TeX skills are unfortunately not up to par here Aug 27, 2015 at 20:24
  • @niclasericsson Sorry, but this site doesn't work like this. Please, open a new question and specify what precisely you'd like to get.
    – egreg
    Aug 27, 2015 at 20:27
0

Here's a slightly modified version of egreg's answer. This works for multiple references and also for all kinds of references.

\documentclass[11pt]{article}

\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink]{cleveref}

\newcommand{\eqlabelleft}{(}
\newcommand{\eqlabelright}{)}
\newcommand{\pcref}[1]{%
\begingroup%
\renewcommand{\eqlabelleft}{}%
\renewcommand{\eqlabelright}{}%
(\cref{#1})%
\endgroup%
}

\creflabelformat{equation}{\eqlabelleft#2#1#3\eqlabelright}

\begin{document}
\begin{equation}
    y = mx + c
    \label{eq:1}
\end{equation}
\begin{equation}
    x^2 + y^2 = 1
    \label{eq:2}
\end{equation}
\cref{eq:1,eq:2}\\
\pcref{eq:1,eq:2}\\
\cref{eq:1,eq:2}
\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 .