# How do I add a prefix to references?

I'm writing up solutions to a book. The book already has all its contents labeled as Figure 1.5, Equation 2.13 etc. I'd like to label my own figures and equations without suffering any collisions with the main text. For example Figure S-1.5 or Equation S-2.13 might do. Is there a way to do this?

• If you use plain \ref (so no cleveref, nameref, autoref etc) then you could just do \newcommand{\myref}[1]{S-\ref{#1}}. Jun 7, 2018 at 9:05
• But that only relabels the occurrence of Figure 1.5 to Figure S-1.5 in the text, doesn't it? The figure itself will still be titled Figure 1.5. Jun 7, 2018 at 9:14
• You're right, I didn't think of that - maybe tex.stackexchange.com/questions/305302/… can help you? Jun 7, 2018 at 9:17
• What do you mean by "my own figures and equations"?
– Werner
Jun 7, 2018 at 18:34
• My solutions contain figures and equations. And sometimes I want to reference an equation in the main text, sometimes I want to reference a figure in my own solutions. I don't want to use the same referencing scheme for both. In case it wasn't clear: I'm not the author of the book. Jun 7, 2018 at 18:38

The number that appears in both figure captions and references is determined by the macro\thefigure. It is effectively defined as \arabic{figure} if you are using article, and as \ifnum\value{chapter}>0 \thechapter.\fi\arabic{figure} if you are using book or report. The macro \theequation does the same thing for equations.

You can thus accomplish what you want by prepending S- to the definitions of \thefigure and \theequation, like this:

\documentclass{article}

\usepackage{amsmath} %% <- for \eqref, optional

\let\theequationWithoutS\theequation %% <- store old definition
\renewcommand\theequation{S-\theequationWithoutS}
\let\thefigureWithoutS\thefigure %% <- store old definition
\renewcommand\thefigure{S-\thefigureWithoutS}

%% Alternative:
% \usepackage{etoolbox}
% \pretocmd{\theequation}{S-}{}{}
% \pretocmd{\thefigure}{S-}{}{}

\begin{document}

$$\label{myeq} \int_{-\pi/2}^{\pi/2} \frac1{1+\cos(x)^{\sin(x)}} \, \mathrm{d}x = \frac\pi2$$

\begin{figure}
\centering
\rule{2cm}{2cm} %% black box
\caption{\label{myfig}This is a figure.}
\end{figure}

This sentence contains references to Figure~\ref{myfig} and Equation~\eqref{myeq}.

\end{document}