3

I am using appendix package to create appendices but also customize them with etoolbox commands. The TOC page number refers to the correct page of appendices sections but the hyperlink jumps to the wrong place.

\documentclass[11pt,a4paper,oneside]{book}

\usepackage[x11names]{xcolor}
\usepackage{caption}
\usepackage{etoolbox}
\usepackage{graphicx}
\usepackage{hyperref}
\hypersetup{linkcolor=DodgerBlue3, colorlinks=true}
\usepackage{tocloft}
\usepackage{appendix}

    % customizing appendices

        % renew the tags of figures, tables and equations
        \newcounter{appendix}
        \AtBeginEnvironment{appendices}{\renewcommand{\thefigure}{\Alph{appendix}.\arabic{figure}.}}
        \AtBeginEnvironment{appendices}{\renewcommand{\thetable}{\Alph{appendix}.\arabic{table}.}}
        \AtBeginEnvironment{appendices}{\renewcommand{\theequation}{\Alph{appendix}.\arabic{equation}}}
        \AtBeginEnvironment{appendices}{\renewcommand{\chaptername}{APPENDIX}}
        \AtBeginEnvironment{appendices}{\renewcommand{\thesection}{\arabic{appendix}.\arabic{section}.}}

        % Define new way to add appendices to the TOC
        \newcommand{\appendixtitle}{}
        \newcommand{\appendixtitleFull}{\chaptername\space\Alph{appendix}.\space\appendixtitle}
        \AtBeginEnvironment{appendices}{
            \preto{\chapter}{
                \clearpage
                \addtocounter{appendix}{1}
                \setcounter{section}{0}
                \setcounter{figure}{0}
                \setcounter{equation}{0}
                \phantomsection \addcontentsline{toc}{chapter}{\appendixtitleFull}
                        }
                                    }


\newcommand{\sampleEquationFloats}{

\begin{figure}
    \includegraphics[]{example-image-a}
    \caption{Figure Caption}
\end{figure}

\begin{equation}
    E = m c^{2}
\end{equation}

\begin{table}
    \caption{Table Caption}
    \centering
    \begin{tabular}{|c |c|}
        \hline
        Column & Column
        \\
        \hline
    \end{tabular}
\end{table}

}



\begin{document}

\tableofcontents

\begin{appendices}

    \renewcommand{\appendixtitle}{My Title}
    \chapter*{\appendixtitleFull}

        \sampleEquationFloats

        \section{One Section}

        \section{Another Section}

    \renewcommand{\appendixtitle}{My Title}
    \chapter*{\appendixtitleFull}

        \sampleEquationFloats

        \section{One Section}

        \section{Another Section}

    \renewcommand{\appendixtitle}{My Title}
    \chapter*{\appendixtitleFull}

        \sampleEquationFloats

        \section{One Section}

        \section{Another Section}

\end{appendices}

\end{document} 
  • Which hyperlink is incorrect in your case? All hyperlinks from the TOC point to the right place for me. – frougon Jul 7 '19 at 6:44
  • All section hyperlinks after appendix 1 – Al-Motasem Aldaoudeyeh Jul 7 '19 at 6:44
  • Ah, I see. They point to appendix A regardless of which one it is. You could have changed the section titles to make it easier to spot. – frougon Jul 7 '19 at 6:48
  • @frougon. I solved it, but it is not clear for me what changes the definitions of \theHsection and \theHsubsection in my code – Al-Motasem Aldaoudeyeh Jul 7 '19 at 6:55
  • If you take your original MWE and comment out the \setcounter{section}{0} in \preto{\chapter}{...}, all links work... – frougon Jul 7 '19 at 7:00
1

There is a solution here TOC Links to Chapters of Parts lead to Chapters of previous Parts . Using a similar approach, we redefine \theHsection and \theHsubsection as follows

\renewcommand*{\theHsection}{\thesection}
\renewcommand*{\theHsubsection}{\thesubsection}

The full code

\documentclass[11pt,a4paper,oneside]{book}

\usepackage[x11names]{xcolor}
\usepackage{caption}
\usepackage{etoolbox}
\usepackage{graphicx}
\usepackage{hyperref}
\hypersetup{linkcolor=DodgerBlue3, colorlinks=true}
\usepackage{tocloft}
\usepackage{appendix}

    % customizing appendices

        % rennew the tags of figures, tables and equations
        \AtBeginEnvironment{appendices}{\renewcommand{\thefigure}{\Alph{appendix}.\arabic{figure}.}}
        \AtBeginEnvironment{appendices}{\renewcommand{\thetable}{\Alph{appendix}.\arabic{table}.}}
        \AtBeginEnvironment{appendices}{\renewcommand{\theequation}{\Alph{appendix}.\arabic{equation}}}
        \AtBeginEnvironment{appendices}{\renewcommand{\chaptername}{APPENDIX}}
        \AtBeginEnvironment{appendices}{\renewcommand{\thesection}{\arabic{appendix}.\arabic{section}.}}

        % Define new way to add appendices to the TOC
        \newcounter{appendix}
        \newcommand{\appendixtitle}{}
        \newcommand{\appendixtitleFull}{\chaptername\space\Alph{appendix}.\space\appendixtitle}
        \AtBeginEnvironment{appendices}{
            \preto{\chapter}{
                % Clear the page (to guarantee referring to the correct page)
                \clearpage
                % Add to appendix counter
                \addtocounter{appendix}{1}
                % Redefine hyperlinks for sections and subsections
                \renewcommand*{\theHsection}{\thesection}
                \renewcommand*{\theHsubsection}{\thesubsection}
                % Reset counters
                \setcounter{section}{0}
                \setcounter{subsection}{0}
                \setcounter{figure}{0}
                \setcounter{table}{0}
                \setcounter{equation}{0}
                % Insert an appendix entry in the TOC
                \phantomsection \addcontentsline{toc}{chapter}{\appendixtitleFull}
                        }
                                    }


\newcommand{\sampleEquationFloats}{

\begin{figure}
    \includegraphics[]{example-image-a}
    \caption{Figure Caption}
\end{figure}

\begin{equation}
    E = m c^{2}
\end{equation}

\begin{table}
    \caption{Table Caption}
    \centering
    \begin{tabular}{|c |c|}
        \hline
        Column & Column
        \\
        \hline
    \end{tabular}
\end{table}

}



\begin{document}

\tableofcontents

\begin{appendices}

    \renewcommand{\appendixtitle}{My Title}
    \chapter*{\appendixtitleFull}

        \sampleEquationFloats

        \section{One Section}

        \section{Another Section}

    \renewcommand{\appendixtitle}{My Title}
    \chapter*{\appendixtitleFull}

        \sampleEquationFloats

        \section{One Section}

        \section{Another Section}

    \renewcommand{\appendixtitle}{My Title}
    \chapter*{\appendixtitleFull}

        \sampleEquationFloats

        \section{One Section}

        \section{Another Section}

\end{appendices}

\end{document} 
| improve this answer | |
1

If you simplify the setup and make use of the capabilities of the caption package (which you load anyway), and if you change \addtocounter{appendix}{1} to \refstepcounter{appendix}, the positions of the hypertargets appear to be entirely correct.

Aside: In order to keep the Table of Contents, List of Figures, and List of Tables to look like a bunch of gaudy Christmas trees, I would add the option linktocpage=true to the \hypersetup instruction. That way, the page numbers rather than the ToC entries are made into hyperlinks.

Addendum, inserted after receiving a follow-up comment from the OP: With the setup suggested here, all one needs to do to display "dots" ("periods", aka "full stops") after section, figure, and table numbers in the Table of Contents, the List of Figures, and the List of Tables, respectively, is to provide the following three instructions (best immediately after loading the tocloft package):

\renewcommand\cftsecaftersnum{.}
\renewcommand\cftfigaftersnum{.}
\renewcommand\cfttabaftersnum{.}

enter image description here

\documentclass[11pt,a4paper,oneside]{book}
\usepackage[x11names]{xcolor}
\usepackage{caption,etoolbox,graphicx,appendix}

\usepackage{tocloft} % influence appearance of ToC, LoF, and LoT
\renewcommand\cftsecaftersnum{.}
\renewcommand\cftfigaftersnum{.}
\renewcommand\cfttabaftersnum{.}

\usepackage{hyperref}
\hypersetup{linkcolor=DodgerBlue3, colorlinks, linktocpage}

% Customize tags of sections, figures, tables and equations in 'appendices' env.
\newcounter{appendix}
\renewcommand{\theappendix}{\Alph{appendix}}
\newcommand{\appendixtitle}{}
\newcommand{\appendixtitleFull}{%
  \chaptername\space\Alph{appendix}.\quad\appendixtitle}
\AtBeginEnvironment{appendices}{%
  \renewcommand{\chaptername}{APPENDIX}
  \captionsetup{labelsep=period}
  \counterwithin{figure}{appendix}
  \counterwithin{table}{appendix}
  \counterwithin{equation}{appendix}
  \counterwithin{section}{appendix}  
  \preto{\chapter}{%
    \clearpage
    \refstepcounter{appendix}
    \phantomsection 
    \addcontentsline{toc}{chapter}{\appendixtitleFull}%
  }
}

\newcommand{\sampleEquationFloatsSections}{%
  \begin{figure}[h!]
    \centering
    \includegraphics[]{example-image-a}
    \caption{Figure Caption}
  \end{figure}
  \begin{equation} E = mc^2 \end{equation}
  \begin{table}[h!]
    \caption{Table Caption}
    \centering
    \begin{tabular}{|c|c|}
        \hline Column & Column \\ \hline
    \end{tabular}
  \end{table}
  \section{A Section}
  \section{Another Section}
}

\begin{document}
%% Generate the ToC, LoF, and LoT:
\tableofcontents
\listoffigures
\listoftables

%% Generate three sample appendices, each with 
%% an equation, table, figure, and two sections
\begin{appendices}
    \renewcommand{\appendixtitle}{My Title A}
    \chapter*{\appendixtitleFull}
    \sampleEquationFloatsSections

    \renewcommand{\appendixtitle}{My Title B}
    \chapter*{\appendixtitleFull}
    \sampleEquationFloatsSections

    \renewcommand{\appendixtitle}{My Title C}
    \chapter*{\appendixtitleFull}
    \sampleEquationFloatsSections
\end{appendices}

\end{document} 
| improve this answer | |
  • Thanks for the answer. It is more concise, but my school wants a dot at the end of the labels of Figures and Tables when they are posted in their prefatory lists. This is why I define their labels manually instead of adding a dot instead of using \captionsetup – Al-Motasem Aldaoudeyeh Jul 7 '19 at 8:21
  • @Al-MotasemAldaoudeyeh - Please see the updates to the answer. They show how to make use of the machinery of the tocloft package to insert "dots" after section, figure and table entries in the ToC, the LoF, and the LoT, respectively. An advantage of not hardcoding the dots into these "numbers" is that you don't mess up the ability to cross-reference these items via the usual LaTeX \label-\ref mechanism. – Mico Jul 7 '19 at 9:17
0

First method

You need to declare that your section counter is subordinate to the appendix counter (normally, it is subordinate to chapter in the book class). Doing this with:

\@addtoreset{section}{appendix}

in \AtBeginEnvironment{appendices}{...} is enough because hyperref has already been loaded (see section Subordinate counters in the hyperref manual). This gives the following code:

\documentclass[11pt,a4paper,oneside]{book}

\usepackage[x11names]{xcolor}
\usepackage{caption}
\usepackage{etoolbox}
\usepackage{graphicx}
\usepackage{hyperref}
\hypersetup{linkcolor=DodgerBlue3, colorlinks=true}
\usepackage{tocloft}
\usepackage{appendix}

    % customizing appendices

\makeatletter

        % renew the tags of figures, tables and equations
        \newcounter{appendix}
        \AtBeginEnvironment{appendices}{\renewcommand{\thefigure}{\Alph{appendix}.\arabic{figure}.}}
        \AtBeginEnvironment{appendices}{\renewcommand{\thetable}{\Alph{appendix}.\arabic{table}.}}
        \AtBeginEnvironment{appendices}{\renewcommand{\theequation}{\Alph{appendix}.\arabic{equation}}}
        \AtBeginEnvironment{appendices}{\renewcommand{\chaptername}{APPENDIX}}
        \AtBeginEnvironment{appendices}{\renewcommand{\thesection}{\arabic{appendix}.\arabic{section}.}}

        % Define new way to add appendices to the TOC
        \newcommand{\appendixtitle}{}
        \newcommand{\appendixtitleFull}{\chaptername\space\Alph{appendix}.\space\appendixtitle}
        \AtBeginEnvironment{appendices}{
            \preto{\chapter}{
                \clearpage
                \addtocounter{appendix}{1}
                \setcounter{section}{0}
                \@addtoreset{section}{appendix}
                \setcounter{figure}{0}
                \setcounter{equation}{0}
                \phantomsection
                \addcontentsline{toc}{chapter}{\appendixtitleFull}
                        }
                                    }
\makeatother

\newcommand{\sampleEquationFloats}{

\begin{figure}
    \includegraphics[]{example-image-a}
    \caption{Figure Caption}
\end{figure}

\begin{equation}
    E = m c^{2}
\end{equation}

\begin{table}
    \caption{Table Caption}
    \centering
    \begin{tabular}{|c |c|}
        \hline
        Column & Column
        \\
        \hline
    \end{tabular}
\end{table}

}



\begin{document}

\tableofcontents

\begin{appendices}

    \renewcommand{\appendixtitle}{My Title}
    \chapter*{\appendixtitleFull}

        \sampleEquationFloats

        \section{One Section}

        \section{Another Section}

    \renewcommand{\appendixtitle}{My Title}
    \chapter*{\appendixtitleFull}

        \sampleEquationFloats

        \section{One Section}

        \section{Another Section}

    \renewcommand{\appendixtitle}{My Title}
    \chapter*{\appendixtitleFull}

        \sampleEquationFloats

        \section{One Section}

        \section{Another Section}

\end{appendices}

\end{document}

Second method

Another technique mentioned in the hyperref manual (section Subordinate counters) as well as here consists in redefining \theHsection:

\renewcommand*{\theHsection}{Appendix.\theHappendix.\thesection}

for instance, at the beginning of your \AtBeginEnvironment{appendices}{...} hook. This makes sure hyperref sees unique values of the section counter when it prepares hyperlinks.

| improve this answer | |

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.