# Automatically add prefix to references from another document?

I'm using xr to reference equations in supplemental material to a paper. I'd like equations from the supplement referenced in the paper to automatically be prefixed with "S", so that Eq.~\ref{eqn:first} appears in the text as Eq. S1. Ideally this would be true in the supplemental document as well. How do I do this?

Here is a solution combining the xr and prettyref packages. The downside is that it forces you to use \prettyref instead of \ref, but you might benefit from the kind of automation provided by the prettyref package anyway :)

\documentclass{article}

\usepackage{xr}
\externaldocument[S]{myotherfile}

\usepackage{prettyref}
\newrefformat{Seq}{Eq.~S\ref{#1}}

\begin{document}
You can find more about \prettyref{Seq:myequation} in my other paper.
\end{document}


Contents of myotherfile.tex:

\documentclass{article}
\begin{document}
$$a=b \label{eq:myequation}$$
\end{document}


Output of the main file:

• Thanks! If I also had \begin{algorithm} ... \label{alg:myalg} \end{algorithm} in myotherfile.tex and wanted to reference that, could I do \prettyref{Seq:myalg}, or do I have to preface everything with eq: in myotherfile.tex? – David P May 6 '13 at 22:05
• You have to prefix all references coming from myotherfile.tex by an S. You could add \newrefformat{Salg}{Algorithm~S\ref{#1}} in the main file and then use \prettyref{Salg:myagl}. – jub0bs May 6 '13 at 22:06

Old post, but I was trying to accomplish this today and none of these solutions were scratching my itch.

There are some good answers in this thread, but none are perfect. Especially when you specifically stipulated that you wanted to prepend "S" to the equations in the supplemental document (not just in the referencing document).

The most robust way to do this is to focus on prepending the "S" first, and then figure out how to cross-reference it. To do that, add this to your preamble in your supplemental document ("supplemental.tex"):

% Define chapter number
\newcommand{\snum}{S1}

% Prepend supplemental chapter to equation list
\renewcommand{\theequation}{\snum.\arabic{equation}}


Now add equations to the document as usual -- the S1 will be added to all the equation numbers. (I specify S1 using a macro to demonstrate this will work with prefixes generated from enumerated lists and such.)

Adding an equation and referencing it from the same document will include the S1 with all references.

\documentclass{article}

\usepackage{amsmath,amssymb}
\usepackage{xr}

% Define chapter number
\newcommand{\snum}{S1}

% Prepend supplemental chapter to equation list
\renewcommand{\theequation}{\snum.\arabic{equation}}

\begin{document}

% Equation to be referenced
\begin{eqnarray}
\label{eq:myequation}
a=b
\end{eqnarray}

% Reference the equation as you normally would
Hello, the equation is \ref{eq:myequation}

\end{document}


Compiling that will output the following:

Now, to reference this equation in another document, just use the standard xr approach in your preamble:

\usepackage{xr}
\externaldocument{supplemental}


Reference the equation as usual:

\documentclass{article}

\usepackage{amsmath,amssymb}

\usepackage{xr}
\externaldocument{supplemental}

\begin{document}

I'm cross-referencing to Equation \ref{eq:myequation}!

\end{document}


This will output:

I think this solution is ideal because the document containing the referenced equation is the authority on how to render (cross)references. You could move an equation from the main text to a supplemental file without worrying about reformatting any of your references (as long as the label name is conserved).

I imagine similar approaches could work for any referenced object if you figure out the equivalent to

\renewcommand{\theequation}{\schap.\arabic{equation}}


for figures, tables, etc.

Shout out to this guy for figuring out the equation prepend trick back in '08.

\newcount\suppl
\suppl0
\def\Ref#1{\ifnum\suppl<1 \ref{#1} \else S\ref{#1}\fi}


And at the beginning of the supplement you should write \suppl1. Of course, you should write Eq.~\Ref{eqn:first} instead of Eq.~\ref{eqn:first}.

Your need seems to be similar to the purpose of the package cleveref. I personally think these references here and there so simple and good for testing and feeling.

For example:

\documentclass[11pt]{article}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{cleveref}
\crefname{equation}{Eq. S}{equations}

\begin{document}
\begin{align}
y&=a_1x+b_1\label{eqn:1}
\end{align}
\noindent
Standard equation reference (\textbackslash ref): \ref{eqn:1}\\
Cleveref equation reference (\textbackslash cref): \cref{eqn:1}
\begin{figure}[ht]\centering\rule{0.5\linewidth}{0.1\linewidth}\caption{First figure}\label{fig:1}\end{figure}
$$\begin{array}{l} \displaystyle \int 1 = x + C\\ \displaystyle \int x = \frac{x^2}{2} + C \\ \displaystyle \int x^2 = \frac{x^3}{3} + C \end{array} \label{eq:xdef}$$
\noindent
Standard figure reference (\textbackslash ref): \ref{fig:1}\\
Cleveref figure reference (\textbackslash cref): \cref{fig:1}\\
And here is the reference using cleveref: \cref{eq:xdef}
\end{document}


The output compiled this example is as below:

• There are two forms of a reference instruction \cref and \Cref for adding lowercase and capital case respectively.
• I customized the prefix Equation into Eq. S in the preamble section.