A heading command like \section{A}
writes a line into the .toc
file that looks like this:
\contentsline {section}{\numberline {1.1}A}{2}
when this is read back in \contentsline{section}
simply expands to \l@section
and that is then the command that picks up the remaining arguments and produces the toc entry. Most often (but not always) this command is defined as a special instance of \@dottedcontentsline
, e.g., in the book class you find:
\newcommand*\l@section{\@dottedtocline{1}{1.5em}{2.3em}}
So to get rid of the dots in some cases there are different approaches possible and the depend on how things are defined and which entries you have to change in reality.
Plan A
Assuming that your toc uses \@dottedtocline
then to get rid of the dots we can redefine \@dotsep
to have a very high number. Assuming further, that we only want to change the sections at the end of the toc and we do not need to have dots appearing later on again. Then the following in front of the appendix in the source document will do the trick:
\makeatletter
\addtocontents{toc}{\def\string\@dotsep{100}}
\makeatother
This will write \def\@dotsep{100}
into the toc file and from that point on the dots are gone.
If you need them back at a later point issue
\makeatletter
\addtocontents{toc}{\def\string\@dotsep{4.5}} % 4.5 might be a different value in your class
\makeatother
Plan B
Make the heading commands in the appendix area or whenever you want a different toc style not write \contentsline{section}
but, say, \contentsline{appsection}
and then define \l@appsection
to format the toc entry according to the desired style.
For this one would need to look for the places in the heading commands that issue \addcontentsline
and change that code.
Fairly elaborate but much more general.
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
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.
** 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}
Best Answer
An alternative solution.