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
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.