0

I use appendix package to generate appendices. It generates the desired heading in the text, but generates the entries of appendices in the TOC exactly the same way it generates entries for chapters. However, I want that to be different. In particular, I want the appendix entry to be

enter image description here

(instead of setting \cftchappresnum to \chaptername, I want to set it to all caps 'appendix'). However, it does not seem that I am able to do it for appendices that use the syntax

\begin{appendices}

    \chapter{}

\end{appendices}

With the syntax above, I simply get arabic numbering with the word 'Chapter' being used instead of Appendix.

Thus, I use \chapter* to avoid printing the a chapter entry in the TOC altogether but then add a custom code to generate the output I want as follows

\AtBeginEnvironment{appendices}{\renewcommand{\thesection}{\arabic{appendix}.\arabic{section}.}}
\newcommand{\appendixtitle}{}
\newcommand{\appendixtitleFull}{\chaptername\space\Alph{appendix}.\space\uppercase{\appendixtitle}}
\newlength{\widthAppendixName}
\setlength{\widthAppendixName}{\widthof{\chaptername \Alph{appendix}.~~} - \cftsecindent}
\makeatletter
    \AtBeginEnvironment{appendices}{
        \preto{\chapter}{
            \addtocounter{appendix}{1}
            \setcounter{section}{0}
            \setcounter{figure}{0}
            \setcounter{equation}{0}
            \addtocounter{appendix}{1}
                \addtocontents{toc}{\protect
         \contentsline{chapter}{\numberline{\chaptername~\Alph{appendix}.}%
                        \cftchapaftersnumb\hspace{\widthAppendixName}\uppercase{\appendixtitle}%
                                        }{\thepage}{\@currentHref}
                                }
                    }
                                }
\makeatother

However, with the code above, I get the wrong page number and the wrong hyperlink destination. Is there a way to get the output I want while using the normal mechanism of generating appendices (i.e., with appendices environment and \chapter command)?

The answer in Wrong page number for appendix in table of contents is not suitable here because I am using appendices package instead of the general \appendix command. The answer in Table of contents and appendices does not show how to make the appendices entries different from chapters while maintaining correct page documentation in the TOC.

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

\usepackage[x11names]{xcolor}
\usepackage{calc}
\usepackage{caption}
\usepackage{graphicx}
\usepackage{etoolbox}
\usepackage{tocloft}

    \usepackage{tocloft}
% BEGIN_FOLD

    % Labels widths
    \setlength{\cftchapnumwidth}{\widthof{\chaptername .~~}}
    \newlength{\widthcftsecnumwidth}
    \setlength{\widthcftsecnumwidth}{\widthof{1.2.3~}}
    \setlength{\cftsecnumwidth}{\widthcftsecnumwidth}

    % Labels indentations
    \newlength{\widthSpace}
    \setlength{\widthSpace}{\widthof{~}}
    \setlength{\cftchapindent}{0mm}
    \setlength{\cftsecindent}{\widthof{\chaptername 1.~} - 0.5\widthSpace - \widthcftsecnumwidth}
    \setlength{\cftsubsecindent}{\cftsecindent + \widthof{\cftsecaftersnumb} + \cftsecnumwidth}

    \newcommand{\tocchapfont}{}
    \renewcommand{\cftchapfont}{\tocchapfont}   

% END_FOLD



\usepackage{appendix}

        \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\uppercase{\appendixtitle}}
            \newlength{\widthAppendixName}
            \setlength{\widthAppendixName}{\widthof{\chaptername \Alph{appendix}.~~} - \cftsecindent}
            \makeatletter
            \AtBeginEnvironment{appendices}{
                \preto{\chapter}{
                    \setcounter{section}{0}
                    \addtocounter{appendix}{1}
                        \addtocontents{toc}{\protect
                             \contentsline{chapter}{\numberline{\chaptername~\Alph{appendix}.}%
                                \cftchapaftersnumb\hspace{\widthAppendixName}\uppercase{\appendixtitle}%
                                                }{\thepage}{\@currentHref}
                                        }
                            }
                                        }
            \makeatother


\usepackage{hyperref}
\hypersetup{citecolor=DodgerBlue3, citebordercolor=DodgerBlue3, urlcolor=Blue1, colorlinks=true}

\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


\chapter{subappendices}

\sampleEquationFloats

\newpage

\section{Section 1}

\begin{subappendices}

    \section{Appendix 1.A}

        \sampleEquationFloats

    \section{Appendix 1.B}

        \sampleEquationFloats

    \section{Appendix 1.C}

\end{subappendices}

\section{Section 2}

\newpage

\sampleEquationFloats

\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}

\chapter{New Chapter}

\section{New Section}

\begin{subappendices}

    \section{Appendix A}

        \sampleEquationFloats

    \section{Appendix B}

        \sampleEquationFloats

    \section{Appendix C}

\end{subappendices}

\end{document} 
  • What happens if you laod appendix with option [toc]? – Bernard Jul 3 at 20:01
  • It just inserts a new line called 'appendices' before the appendices entries. The entries of appendices in the TOC are still the same – Al-Motasem Aldaoudeyeh Jul 3 at 20:04
  • The \contentsline entry has to be issued after the \chapter has started. The best way to accomplish that automatically is to modify the star form of \chapter to add the \contentsline with proper timing as done for the unstarred form. Some possibly useful information appears with the question Creating unnumbered chapters/sections (plus adding them to the ToC and/or header) – barbara beeton Jul 3 at 20:28
  • @barbarabeeton. I thought that this is really what I need to do, but there is no command I am aware of that could execute an arbitrary command right after another command has finished its task – Al-Motasem Aldaoudeyeh Jul 3 at 20:31
  • @Al-MotasemAldaoudeyeh -- amsbook does add the titles of starred elements to the TOC automatically, so you could look at how that is done. The definitions aren't identical to those of book, so can't just be copied, but the idea for the method should be clear, and thus adaptable. – barbara beeton Jul 3 at 20:34

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.