9

I have an appendix (section) at the end of Chapter 1 that looks like

Appendix 1.A Baz

However, as you can see in the image below, the TOC doesn't display the appendix counter properly (it overlaps) Appendix

What I want to achieve in the TOC is

1 Foo
  1.1 Bar.......
  Appendix 1.A Baz...... 

Here is a MWE:

\documentclass{book}

\usepackage{etoolbox}

\newcommand*{\secappendix}[1]{%
  \setcounter{section}{0}%
  \renewcommand*{\thesection}{\appendixname\space\thechapter.\Alph{section}}%
  \section{#1}\renewcommand*{\thesection}{\thechapter.\Alph{section}}%
}
\pretocmd{\chapter}{\renewcommand*{\thesection}{\thechapter.\arabic{section}}}{}{}

\begin{document}

\frontmatter
\tableofcontents

\mainmatter

\chapter{Foo}
\section{Bar}

\secappendix{Baz}
\end{document}

EDIT: The above MWE has further problems when using the hyperref package. The hyperlink from the TOC to the appendix heading (wrongly) takes you the first section and the hyperlink to the equation in the appendix takes you to the equation in the first section. I tried the solution suggested in this answer but it doesn't seem to work (in fact I think it doesn't work in the corresponding MWE there). Here is a new MWE including the hyperref package.

\documentclass{book}

\usepackage{etoolbox}
\usepackage{lipsum}

\usepackage{amsmath}
\numberwithin{equation}{section}

\newcommand*{\secappendix}[1]{%
  \setcounter{section}{0}%
  \renewcommand*{\thesection}{\appendixname\space\thechapter.\Alph{section}}%
  \section{#1}\renewcommand*{\thesection}{\thechapter.\Alph{section}}%
}
\preto{\chapter}{\renewcommand*{\thesection}{\thechapter.\arabic{section}}}

\usepackage[colorlinks=true]{hyperref}

\begin{document}

\frontmatter
\tableofcontents

\mainmatter

\chapter{Foo}
\section{Bar}
\begin{equation}
\label{eq:1}
  1 + 1 = 2
\end{equation}
\lipsum
\secappendix{Baz}
\begin{equation}
\label{eq:2}
  2 + 2 = 4
\end{equation}

Hyperlink to \ref{eq:2}
\end{document}

EDIT 2: Changing the command to

\newcommand*{\secappendix}[1]{%
  \setcounter{section}{0}%
  \renewcommand*{\thesection}{\appendixname\space\thechapter.\Alph{section}}%
  \renewcommand*{\theHsection}{\thechapter.\Alph{section}}%
  \section{#1}\renewcommand*{\thesection}{\thechapter.\Alph{section}}%
}

seems to fix the hyperref problem but the TOC issue remains.

Edit 3: Since I've added a bounty I think I should explain the motivation behind this question. I don't want the appendix of a chapter to be at the same level as the chapter since the appendix is (just) a part of the chapter. Therefore I want to treat appendices as sections. I also want to signal that the appendices are in fact appendices. What I want to achieve is something like this

1 Chapter title
  1.1 Section title
      1.1.1 Subsection title
  Appendix 1.A Appendix title
      1.A.1 Appendix subsection title
4
  • Have you considered using e.g. A.1 Appendix title or 1.A Appendix title without the preceding 'Appendix'? I ask just because it looks a little odd (to my eyes) that 'Section title' from 1.1 does not align with 'Appendix title' of 1.A even though they are at the same level.
    – cfr
    Jan 2, 2014 at 0:22
  • @cfr yes I have considered that. However, how does the reader know that 1.A Title is in fact an appendix and not just a section? Generally appendices have some sort of sign (with the word appendix) stating that they are indeed appendices.
    – petobens
    Jan 2, 2014 at 3:32
  • It just looks odd to me but I provided an answer which seems to do what you want, as far as I can tell. I just thought I'd ask. You could always incorporate 'Appendix' into the actual title i.e. '1.A Appendix: Appendix title' but perhaps there is really no perfect way of arranging this!
    – cfr
    Jan 2, 2014 at 3:47
  • @petobens No offense, but it also looks odd to me :) The way I have seen it most times (even in a book on scientific writing) was: Appendix or List of appendices (header), followed by A.1, A.2, A.3,..., B.1, B.2,... But if you prefer your structure, that's fine. In the end you have to like it and be happy with it.
    – TomM
    Jan 3, 2014 at 23:14

3 Answers 3

3
+50

This solution does not use any package (except the OP already has), but adding a \addtocontents{toc}{...} to the author-defined command \secappendix.

The following command taking 3 parameters is used to set l@section. The default value for numwidth is 2.3em instead of 7em here. One could adjust to suit one's need.

\@dottedtocline{<level>}{<indentation>}{<numwidth>}}

If dotted lines in TOC is to remove, use the following in the preamble.

\makeatletter
\renewcommand\@dotsep{450}
\makeatother

enter image description here

enter image description here

Code

\documentclass{book}

\usepackage{etoolbox}
\usepackage{lipsum}

\usepackage{amsmath}
\numberwithin{equation}{section}

\newcommand*{\secappendix}[1]{%
  \setcounter{section}{0}%
  \addtocontents{toc}
  {\string\renewcommand\string\l@section{\string\@dottedtocline{1}{1.5em}{7em}}}
  \renewcommand*{\thesection}{\appendixname\space\thechapter.\Alph{section}}%
  \section{#1}
  \renewcommand*{\thesection}{\thechapter.\Alph{section}}%
  \addtocontents{toc}
  {\string\renewcommand\string\l@section{\string\@dottedtocline{1}{1.5em}{2.3em}}}
}
\preto{\chapter}{\renewcommand*{\thesection}{\thechapter.\arabic{section}}}

\usepackage[colorlinks=true]{hyperref}

\begin{document}

\frontmatter
\tableofcontents

\mainmatter

\chapter{Foo}

Here is a link to \ref{app:baz}.

\section{Bar}
\begin{equation}
\label{eq:1}
  1 + 1 = 2
\end{equation}
\subsection{Subsection title}
\lipsum[1]

\secappendix{Baz}
\label{app:baz}
\begin{equation}
\label{eq:2}
  2 + 2 = 4
\end{equation}
\subsection{Appendix subsection title}
\lipsum[1]
Hyperlink to \ref{eq:2}

\chapter{Chapter 2}
\section{Test}
\subsection{Test subsection title}
\secappendix{TestBaz}
\subsection{TestBaz subsection title}

\end{document}

Edit: The OP wishes to put the author-defined section name into the bookmark as well. To this end, this solution seeks help from bookmark package and please load it after hyperref package to avoid crash. Then add \pdfbookmark[section]{\thesection~#1}{#1}, respectively for <level><bookmark text><internal label>, into the author-defined macro as shown below.

\usepackage[colorlinks=true]{hyperref}
\usepackage{bookmark}                       % <-- here

\newcommand*{\secappendix}[1]{%
  \setcounter{section}{0}%
  \addtocontents{toc}
  {\string\renewcommand\string\l@section{\string\@dottedtocline{1}{1.5em}{7em}}}
  \renewcommand*{\thesection}{\appendixname\space\thechapter.\Alph{section}}%
  \section{#1}
  \pdfbookmark[section]{\thesection~#1}{#1}  %<-- Here
  \renewcommand*{\thesection}{\thechapter.\Alph{section}}%
  \addtocontents{toc}
  {\string\renewcommand\string\l@section{\string\@dottedtocline{1}{1.5em}{2.3em}}}
}

Final Update: It seems that the OP-defined macro will by itself insert a bookmark whenever \section{#1} is called, resulting in repeated bookmark if the bookmark package is used. On the first notice by the OP, an investigation is activated. To remove the redundant bookmark, this solution modifies the OP-defined macro (marked by <-- here) without using bookmark which causes the redundancy. The objective wanted by OP is achieved and therefore post the full code again to avoid ambiguity.

enter image description here

** Full Code:**

\documentclass{book}

\usepackage{etoolbox}
\usepackage{lipsum}
\usepackage{amsmath}
\numberwithin{equation}{section}

\newcommand*{\secappendix}[1]{%
\setcounter{section}{1}                                   %<-- here
\setcounter{subsection}{0}                                %<-- here
\addtocontents{toc}
{\string\renewcommand\string\l@section{\string\@dottedtocline{1}{1.5em}{7em}}}
  \renewcommand*{\thesection}{\appendixname\space\thechapter.\Alph{section}}%
\section*{\thesection~#1}                                 %<-- here
\phantomsection                                           %<-- here
\addcontentsline{toc}{section}{\thesection~#1}            %<-- here
\renewcommand*{\thesection}{\thechapter.\Alph{section}}%
\addtocontents{toc}
{\string\renewcommand\string\l@section{\string\@dottedtocline{1}{1.5em}{2.3em}}}
}
\preto{\chapter}{\renewcommand*{\thesection}{\thechapter.\arabic{section}}}

\usepackage[colorlinks=true]{hyperref}
%\usepackage{bookmark}

\begin{document}

\frontmatter

\tableofcontents

\mainmatter

\chapter{Foo}

Here is a link to \ref{app:baz}.

\section{Bar}
\begin{equation}
\label{eq:1}
  1 + 1 = 2
\end{equation}
\subsection{Subsection title}
\lipsum[1]

\secappendix{Baz}
\label{app:baz}
\begin{equation}
\label{eq:2}
  2 + 2 = 4
\end{equation}
\subsection{Appendix subsection title}
\subsection{Appendix second subsection title}
\lipsum[1]
Hyperlink to \ref{eq:2}

\chapter{Indentation}
\lipsum[1]
\section{Test}
\lipsum[1]
\subsection{Test subsection title}
\lipsum[1]
\secappendix{TestBaz}
\lipsum[1]
\subsection{TestBaz subsection title}
\subsection{TestBaz second subsection title}

\chapter{Bookmark}
\lipsum[1]
\section{Test2}
\lipsum[1]
\subsection{Test2 subsection title}
\lipsum[1]
\secappendix{Test2}
\lipsum[1]
\subsection{Test2 subsection title}
\subsection{Test2 second subsection title}
\end{document}
9
  • Wowww this seems to work great. I have a some questions: why don't you use \makeatletter and \makeatother (as it is done here)? What does \string do? I think that in your answer it should say The default value for numwidth is 7em instead of 2.3em. The value of 7em was chosen arbitrarily? Can you make the bookmarks also show, for instance, Appendix 1.A Baz. Currently they are rendered as Baz. Thanks!
    – petobens
    Jan 3, 2014 at 17:53
  • @petobens -- No need for \makeatletter...\makeatother. \string\cs converts \cs into character string so that the command right after the \string can be written exactly into *.toc file for later formatting procedure. If you remove the \string, such a command is expanded before being written, yielding weird results. As to the bookmark improvement, please see my update.
    – Jesse
    Jan 3, 2014 at 23:13
  • Thanks! I will test it tomorrow and probably award you the bounty! Thanks for all the help.
    – petobens
    Jan 4, 2014 at 3:48
  • @petobens -- Glad I can help.
    – Jesse
    Jan 4, 2014 at 4:06
  • There is a problem with the bookmarks. Now I indeed see Appendix 1.A Baz but also Baz. Can you fix it?
    – petobens
    Jan 5, 2014 at 0:28
1

The problem is that you want the string "Appendix 1.A" the replace the numbering of the section, but there is not enough place for the whole string. You could specify some more room for it, but I think that the result is not what you expect.

\documentclass{book}

\usepackage{etoolbox}


\newcommand*{\secappendix}[1]{%
  \setcounter{section}{0}%
  \renewcommand*{\thesection}{\appendixname\space\thechapter.\Alph{section}}%
  \section{#1}\renewcommand*{\thesection}{\thechapter.\Alph{section}}%
}
\pretocmd{\chapter}{\renewcommand*{\thesection}{\thechapter.\arabic{section}}}{}{}

\makeatletter
 \renewcommand*\l@section{\@dottedtocline{2}{0.6em}{10em}}
\makeatother

\begin{document}

\frontmatter
\tableofcontents

\mainmatter

\chapter{Foo}
\section{Bar}

\secappendix{Baz}
\end{document}

An excellent explantion on how to modify the lengths in the TOC is given here. But I think, the TOC expects the numbering of the section to have always the same lengths or equivalent lengths, for example 1.2 or 1.11.

1
  • Thanks! But your solution is not exactly what I need.
    – petobens
    Dec 6, 2013 at 22:06
1

This is based on answers found here, as well as the documentation for the appendix package.

\documentclass{book}

\usepackage[title,titletoc]{appendix}
\usepackage{lipsum}

\usepackage{amsmath}
\numberwithin{equation}{section}

\usepackage[colorlinks=true]{hyperref}

\begin{document}

\frontmatter
\tableofcontents

\mainmatter

\chapter{Foo}

Here is a link to \ref{app:baz}.

\section{Bar}

  \begin{equation}
  \label{eq:1}
    1 + 1 = 2
  \end{equation}
  \lipsum

\subsection{Fu}

  \lipsum[71]

\begin{subappendices}
\section{Baz}\label{app:baz}

  \begin{equation}
  \label{eq:2}
    2 + 2 = 4
  \end{equation}

  Hyperlink to \ref{eq:1} and \ref{eq:2}

\subsection{Badd}

  \lipsum[5-6]

\end{subappendices}

\end{document}

Exhibit 1:

TOC

Exhibit 2:

Hyperlink to appendix

Exhibit 3:

Hyperlinks to equations

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .