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}
Add the following to your preamble:
\addtolength{\cftchapternumwidth}{.5em}
\makeatletter
\renewcommand*{\l@appendix}[2]{%
\renewcommand{\chapternumberlinehook}[1]{\def\@cftbsnum{Appendix\ }}%
\l@chapapp{#1}{#2}{}}
\g@addto@macro\appendix{\renewcommand{\printchaptername}{\normalfont\bfseries Appendix}}
\makeatother
The first length adjustment pushes the ToC titles over by another .5em. This is because the class hard-codes the width of the "chapter number", which actually consists of Chapter<space><num>
. And, since Chapter<space><num>
is typically longer than Appendix<space><letter>
, we need a little more space.
The second adjustment is made to the way the appendix entries are handled in the ToC. In a rather crude way (similar to the way the class has been written), we adjust the content b
efore the s
ectional num
ber to be Appendix
.
Finally, we append a new chapter name printing mechanism to the \appendix
macro.
Here is a complete minimal example:
\documentclass[final]{ukthesis}
%you must include these 2 packages.
\usepackage{hyperref}
\usepackage{memhfixc}
\addtolength{\cftchapternumwidth}{.5em}
\makeatletter
\renewcommand*{\l@appendix}[2]{%
\renewcommand{\chapternumberlinehook}[1]{\def\@cftbsnum{Appendix\ }}%
\l@chapapp{#1}{#2}{}}
\g@addto@macro\appendix{\renewcommand{\printchaptername}{\normalfont\bfseries Appendix}}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\tableofcontents
\chapter{Introduction}
\section{General Introduction}
In \textbf{Appendix \ref{app:A}}, alongside CAD diagrams.
\appendix
\chapter{Detector Frame Drawings}\label{app:A}
The following pages show drawings
\end{document}
Best Answer
As xport noted, you probably have an old (very old) version of TeX. Newer version don't have this problem. The best solution is to update; you probably have a lot of other since-corrected misfeatures in addition to this one. If you can't update, try the following: