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)
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
Best Answer
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 fornumwidth
is 2.3em instead of 7em here. One could adjust to suit one's need.If dotted lines in TOC is to remove, use the following in the preamble.
Code
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 afterhyperref
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.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 thebookmark
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 usingbookmark
which causes the redundancy. The objective wanted by OP is achieved and therefore post the full code again to avoid ambiguity.** Full Code:**