Here is an idea:
\newcommand{\startappendix}{%
\pagebreak%
\addcontentsline{toc}{chapter}{Appendices}%
\fancyhead[L]{{\bfseries Appendices}}%
\setcounter{section}{0}%
\renewcommand{\thesection}{\Alph{section}}%
}
\newenvironment{newappendix}[2]%
{\gdef\headername{#1}%
\phantomsection%
\refstepcounter{section}%
\addcontentsline{toc}{section}{Appendix \thesection~- #1}%
\label{app:#2}}
{\fancyhead[R]{Appendix \thesection~- \headername}
\newpage}
Add these two commands in your preamble. The first one should be used when you want to start your appendices. The second one is to use each time you want to change the name of the current appendix. The first argument is the name of the appendix and the second is what will be added the the app:
prefix for the label. You can then call the appendix with the \ref{app:<your second argument>}
wherever you want in your code.
Example
Here is a short example of the code and its output
\documentclass{report}
\usepackage[demo]{graphicx}
\usepackage{hyperref}
\usepackage{fancyhdr}
\pagestyle{fancy}
\newcommand{\startappendix}{%
\pagebreak%
\addcontentsline{toc}{chapter}{Appendices}%
\fancyhead[L]{{\bfseries Appendices}}%
\setcounter{section}{0}%
\renewcommand{\thesection}{\Alph{section}}%
}
\newenvironment{newappendix}[2]%
{\gdef\headername{#1}%
\phantomsection%
\refstepcounter{section}%
\addcontentsline{toc}{section}{Appendix \thesection~- #1}%
\label{app:#2}}
{\fancyhead[R]{Appendix \thesection~- \headername}
\newpage}
\begin{document}
\tableofcontents
\chapter{One}
\section{One-one}
\startappendix
\begin{newappendix}{First}{1}
\includegraphics[width=12cm,height=15cm]{test}
\end{newappendix}
\begin{newappendix}{Second}{2}
\includegraphics[width=12cm,height=15cm]{test}
\end{newappendix}
\end{document}
EDIT Another way to go I found thanks to egreg for the second command:
\fancyhead[R]{Appendix \rightmark}
\newcommand{\newappendix}[2]{%
\clearpage%
\phantomsection%
\refstepcounter{section}%
\addcontentsline{toc}{section}{Appendix \thesection~- #1}%
\markright{\thesection \ -- #1}%
\label{app:#2}
}
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
Appendices should have a title. If you insist in not giving a title other than “Appendix A”, then you can add
to your preamble and do
Complete example