1

I am working with a slightly modified version of the answer provided by @StevenBSegletes to achieve a custom appendix structure.

(disclaimer: I left out my own modifications here to make the example more minimal, as this issue arises even with the bare original code by Steven. if you deem that I need to show my full set-up, I will be more than happy to do so.)

When you run the MWE below, all of the labelling of the page numbers, equation numbers etc. work as per normal. But under the hood, eq1 and eq2 in the appendices throw up warnings in the log file that look like:

{C:/Users/user/AppData/Local/MiKTeX/2.9/pdftex/config/pdftex.map}]pdfTeX warnin
g (ext4): destination with the same identifier (name{equation.0.2.1}) has been 
already used, duplicate ignored
<to be read again> 
                   \relax 
l.56 \begin{equation}
                     \label{eq1}pdfTeX warning (ext4): destination with the sam
e identifier (name{equation.0.2.1}) has been already used, duplicate ignored
<to be read again> 
                   \relax 

and clicking the hyperlinks from Appendix B (see MWE) brings me to the "first" equation in the main text, not the appropriate ones in Appendix A and B. Presumably because of the above stated warning.

How can I fix Steven's code so that my references will work as expected? I am using cleveref, hypcap, bookmark and hyperref in my actual document.

MWE

\documentclass[12pt]{scrreprt}
\usepackage{hyperref}
\usepackage{cleveref}

% code taken from: https://tex.stackexchange.com/a/132988
\makeatletter
\renewcommand\appendix{\@ifstar{\loneappendix}{\anappendix}}
\newcounter{appndx}
\setcounter{appndx}{0}
\newcommand \loneappendix [1]{
  \clearpage
  \setcounter{appndx}{1}
  \setcounter{figure}{0}
  \renewcommand\thefigure{\Alph{appndx}-\@arabic\c@figure}
  \setcounter{table}{0}
  \renewcommand\thetable{\Alph{appndx}-\arabic{table}}
  \setcounter{equation}{0}
  \renewcommand\theequation {\Alph{appndx}-\arabic{equation}}
  \def\appendixtitle{\appendixname. #1}
  \addcontentsline{toc}{section}\appendixtitle
  \theappendix\appendixtitle
  \setcounter{page}{1}
  \renewcommand\thepage{\Alph{appndx}.\arabic{page}}
}
\newcommand \anappendix[1]{
  \clearpage
  \refstepcounter{appndx}
  \setcounter{equation}{0}
  \setcounter{figure}{0}
  \renewcommand\thefigure{\Alph{appndx}-\@arabic\c@figure}
  \setcounter{table}{0}
  \renewcommand\thetable{\Alph{appndx}-\arabic{table}}
  \renewcommand\theequation {\Alph{appndx}-\arabic{equation}}
  \def\appendixtitle{\appendixname~\Alph{appndx}. #1}
  \addcontentsline{toc}{section}\appendixtitle
  \theappendix\appendixtitle
  \setcounter{page}{1}
  \renewcommand\thepage{\Alph{appndx}.\arabic{page}}
}
\newcommand\theappendix[1]{
  \section*{#1}
}
\makeatother

\begin{document}
\section{First section}
Hello world
\section{Second Section}
Hello world, this is the offending equation.
\begin{equation}
 y =x^2 
\end{equation}

\appendix{First}
I will want to reference this equation later.
\begin{equation}\label{eq1}
 y =x^2 
\end{equation}

\appendix{Second}
\begin{equation}\label{eq2}
y =x^2 
\end{equation}

I want to reference first equation from appendix A: \cref{eq1}.

I also want to reference the first equation from appendix B: \cref{eq2}

But both bring me back to the ``first'' first equation (in the main text).
\end{document}

EDIT:

I think I can solve the problem if I introduce new custom counters for the appendix instead of re-using the equation, figure etc. counters. Then there wouldn't be a clash with the main text hyper-labels. But how can I implement this idea, especially with \refstepcounter?

  • 1
    Option hypertexnames=false should help: \usepackage[hypertexnames=false]{hyperref}. – esdd Nov 16 '18 at 21:29
  • @esdd Care to write up an answer? – Troy Dec 26 '18 at 2:59

Your Answer

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

Browse other questions tagged or ask your own question.