2

Question

How can I cite a figure, section, or equation from an external document, but reference it using the same formatting as I use in my document via the cleveref package, regardless of how cleveref reference style was set up?

Example

Suppose I wish to refer to an equation (say, their ninth equation) in some other document. Suppose cleveref was set up so that \Cref{eqn:myEqn} in the text below resolved to something looking like Equation~(1). In this case, I could simply use Equation~(9) to refer to the external equation, as done below.

\documentclass{report}
\usepackage{cleveref}

\begin{document}

My equation is:
\begin{equation} \label{eqn:myEqn}
    f(x) = a x^2 + bx + c \text{.}
\end{equation}

My \Cref{eqn:myEqn} is equivalent to Equation~(9) of Smith et al. (2018). 

\end{document}

The problem is that if cleveref was later set up differently so that \Cref{eqn:myEqn} resolved to Eqn.~1, or (1), or Equation~1 (etc.), then anywhere I hard-coded Equation~(9) would need to be changed to match the new reference style.

Is there a macro in cleveref that can do this for me? I imagine I'd need to tell it what kind of reference it was (equation, figure, section, etc.) and what number or letter it should use to refer to it. If there was, perhaps it could be used like the following:

\documentclass{report}
\usepackage{cleveref}

\begin{document}

My equation is:
\begin{equation} \label{eqn:myEqn}
    f(x) = a x^2 + bx + c \text{.}
\end{equation}

My \Cref{eqn:myEqn} is equivalent to \CrefFormatExternal[eqn]{9} of Smith et al. (2018). 

\end{document}
  • The cleveref documentation does not say anything about this. Are both files using cleveref? Of course, using number as label is not really useful. Your example is not compilable, anyway – user31729 Feb 9 '18 at 21:50
  • 1
    The other document is a published manuscript, so I don't have access to it other than the PDF it was published in. I'm not hoping to somehow link two files using cleveref. I know I want to cite an equation in an external document, and I know what number the other document refers to that equation by. – jvriesem Feb 9 '18 at 21:53
  • Off-topic: You have asked some questions and some of them has answers, but you did not accept them. Please consider to go through your list of questions and accept the answers that are most valuable to you in order to show appreciation of the work done by users to help you -- anything other is exploitation of them – user31729 Feb 12 '18 at 6:40
1

My possible solution stores the current value of the counter (equation), then sets the counter (equation) to the 2nd argument value decreased by one, uses \refstepcounter and an automatically generated label. This is referenced by checking whether r@X is defined or not. X is the autogenerated label name. Of course, a weakness of this approach is, that every time the macro \CrefFormatExternal macro is called for the same value, it will generate such a label.

The starred version suppresses hyperlinks, the \crefFormatExternal is available as well.

\documentclass{report}
\usepackage{mathtools}

\usepackage{xparse}
\usepackage{hyperref}
\usepackage{cleveref}


\makeatletter

\newif\if@hyperrefpackageloaded

\AtBeginDocument{%
  \@ifpackageloaded{hyperref}{%
    \@hyperrefpackageloadedtrue
  }{}
}    

\newcounter{tmpcntr}
\newcounter{helpcntr}


\NewDocumentCommand{\CrefFormatExternal}{sO{equation}m}{%
  \setcounter{tmpcntr}{\value{#2}}%
  \stepcounter{helpcntr}%
  \setcounter{#2}{\numexpr#3-1}%
  \refstepcounter{#2}\label{cleverrefexternalref:\number\value{helpcntr}}%
  \@ifundefined{r@cleverrefexternalref:\number\value{helpcntr}}{%
  }{%
    \IfBooleanTF{#1}{%
      \Cref*{cleverrefexternalref:\number\value{helpcntr}}%
    }{%
      \Cref{cleverrefexternalref:\number\value{helpcntr}}%
    }%
  }%
  \setcounter{#2}{\value{tmpcntr}}%
}




\NewDocumentCommand{\crefFormatExternal}{sO{equation}m}{%
  \setcounter{tmpcntr}{\value{#2}}%
  \stepcounter{helpcntr}%
  \setcounter{#2}{\numexpr#3-1}%
  \refstepcounter{#2}\label{cleverrefexternalref:\number\value{helpcntr}}%
  \@ifundefined{r@cleverrefexternalref:\number\value{helpcntr}}{%
  }{%
    \IfBooleanTF{#1}{%
      \cref*{cleverrefexternalref:\number\value{helpcntr}}%
    }{%
      \cref{cleverrefexternalref:\number\value{helpcntr}}%
    }%
  }%
  \setcounter{#2}{\value{tmpcntr}}%
}

\makeatother


\begin{document}

My equation is:
\begin{equation} \label{eqn:myEqn}
    f(x) = a x^{2} + bx + c \text{.} 
\end{equation}

My \Cref{eqn:myEqn} is equivalent to \CrefFormatExternal*{9} of Smith et al. (2018). 


\begin{equation} \label{eqn:otherEqn}
    x_{1/2} =  \dfrac{ - b\pm \sqrt{ b^{2} - 4 ac}}{2a}
\end{equation}


\end{document}

enter image description here

  • Where does the hyperlink link to in the non-starred version? – Lack Sep 8 '18 at 6:54
0

It's possible to use cleveref's internal commands to format a reference without any counters involved. The commands of the form cref@<type>@format, where <type> is the counter name (such as equation, section, or figure), seem to do what we want.

For example, cref@figure@format{foo}{}{} produces "fig. foo", or whatever is produced by the user-defined \crefformat for figures. Note:

  • The two empty arguments are normally used to mark the beginning and end of the hyperlinked part of the reference (see documentation of \crefformat), but I assume no hyperlink is desired in this case.
  • Because this internal macro has @ in the name, we can't use it in a document except after \makeatletter.

The following example defines a helper macro, \crefcustom, which invokes the internal command. \Crefcustom is the capitalized version.

\documentclass{report}
\usepackage{cleveref}

\makeatletter
\newcommand {\crefcustom}[2]{\csname cref@#1@format\endcsname{#2}{}{}}
\newcommand {\Crefcustom}[2]{\csname Cref@#1@format\endcsname{#2}{}{}}
\makeatother

\begin{document}

My equation is:
\begin{equation} \label{eqn:myEqn}
    f(x) = a x^2 + bx + c.
\end{equation}

My \cref{eqn:myEqn} is equivalent to \crefcustom{equation}{9} of Smith et al. (2018). 

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.