2

For my thesis I use the memoir class and the \include command to include all the chapters. Unfortunately, I used identical figure and table labels in some chapters. Therefore, the labelling is totally off.

I found this solution to my problem:

Reusing same labels across different chapters

Following Werner's initial advice and his last command (May 30 '16 at 14:19), I managed to fix the figure and tables labels. But now the equation labels are off (i.e., equation ??) is displayed whenever I refer to an equation).

Here is part of my code:

\documentclass[11pt,twoside,openright]{memoir}

\AtBeginDocument{% ...if you're using hyperref
  \let\oldlabel\label% Copy original version of \label
  \let\oldref\ref% Copy original version of \ref
}

\newcommand{\addlabelprefix}[1]{%
  \renewcommand{\label}[1]{\oldlabel{#1-##1}}% Update \label
  \renewcommand{\ref}[1]{\oldref{#1-##1}}% Update \ref
}
\newcommand{\removelabelprefix}{%
  \renewcommand{\label}{\oldlabel}% Restore \label
  \renewcommand{\ref}{\oldref}% Restore \ref
}

\addlabelprefix{a}
\include{chapter1} 

Any advice on how to fix this issue is welcome!

  • 1
    Please show us your version of the code, with memoir – user31729 Jan 2 '17 at 15:03
  • \documentclass[11pt,twoside,openright]{memoir} – Helen Steingröver Jan 2 '17 at 15:23
  • No, the document ... – user31729 Jan 2 '17 at 15:23
  • The document is only a fragment yet – user31729 Jan 2 '17 at 15:26
  • Please post a compilable document, with \documentclass, \begin{document} ...\end{document}, not only fragments! – user31729 Jan 2 '17 at 15:28
2

I used the frame of Werner's answer above, but apply a different method:

  • Each time a \chapter starts, a auxiliary counter is increased and a \@currentprefix is defined, as chapter::\number\value{auxchapter}
  • This \@currentprefix serves in renewed versions of \label and \ref as \@currentprefix::#1 (hidden in \generatemultilabel.

References in and from \include{foochapter} will work too then.

Additionally, I defined a macro to make references across chapters with the relevant chapter number and usual label.

The current approach works for book as well as for memoir.

It will fail if cleveref is used, due to the optional argument of \label introduced in cleveref.


\documentclass[11pt,twoside,openright]{memoir}
\usepackage{xpatch}
\usepackage{hyperref}

\begin{filecontents}{externalchapter.tex}
\chapter{Yet another nice chapter}\label{foochapter}
See chapter~\ref{foochapter}.

\begin{figure}
\caption{Foo figure}\label{foofigure}
\end{figure}

\begin{table}
\caption{Foo table}\label{footable}
\end{table}

\begin{equation}
  E=mc^{2} \label{einstein}
\end{equation}

See \ref{einstein} or \ref{footable} or \ref{foofigure}
\end{filecontents}

\newcounter{auxchapter}


\makeatletter
\newcommand{\generatemultilabel}[1]{%
  \@currentprefix::#1%
}

\AtBeginDocument{%
  \let\latex@@label\label
  \let\latex@@ref\ref

  \renewcommand{\label}[1]{%
    \latex@@label{\generatemultilabel{#1}}%
  }
  \renewcommand{\ref}[1]{%
    \latex@@ref{\generatemultilabel{#1}}%
  }

  \xpretocmd{\chapter}{%
    \stepcounter{auxchapter}%
    \edef\@currentprefix{chapter::\number\value{auxchapter}}
  }{\typeout{Success}}{\typeout{Failure}}

  \newcommand{\crosschapterref}[2]{%
    \begingroup
    \edef\@currentprefix{chapter::#1}%
    \ref{#2}%
    \endgroup
  }
}
\makeatother





\begin{document}

% Hypothetical first paper

\chapter{A chapter}\label{foochapter}
See chapter~\ref{foochapter}.

\begin{figure}
\caption{Foo figure}\label{foofigure}
\end{figure}

\begin{table}
\caption{Foo table}\label{footable}
\end{table}

\begin{equation}
  E=mc^{2} \label{einstein}
\end{equation}

See \ref{einstein} or \ref{footable} or \ref{foofigure}

% Hypothetical second paper

\chapter{Another chapter}\label{foochapter}
See chapter~\ref{foochapter}.

\begin{figure}
\caption{Foo figure}\label{foofigure}
\end{figure}

\begin{table}
\caption{Foo table}\label{footable}
\end{table}

\begin{equation}
  E=mc^{2} \label{einstein}
\end{equation}

See \ref{einstein} or \ref{footable} or \ref{foofigure}

\include{externalchapter}

But in \crosschapterref{1}{einstein} we saw

\end{document}‎

enter image description here

|improve this answer|||||
  • 1
    This worked well for me after some modifications. Added \renewcommand{\ltx@label}[1]{\latex@@label{\generatemultilabel{#1}}} for it to work with amsmath. Added \edef\@currentprefix{} at the start to make it work before the first \chapter. – Forss Feb 20 '18 at 17:58
  • @Forss: Yes, but the question was not for amsmath – user31729 Feb 20 '18 at 18:05

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.