5

This was somewhat similar to what I am looking for, but not exactly: How to make appendices behave like sections in ToC

I would like for my appendices to do the following:

  1. One chapter in the ToC for all appendices
  2. Appendices behave like sections and are labeled A, B, C, etc.
  3. \ref command inserts the appendix label, not a number
  4. Appendices have their name and label in the page header, no need for chapter, section or other headings.

The resulting ToC would look like this:

Some Chapter 1
    Some section 1
Some Chapter 2
    Some section 2
Appendices
    Appendix A - Some name
    Appendix B - Some name 2
    etc.

Currently I can only reproduce this behaviour by adding the content lines manually, using phantom sections to get the page numbers correct and manual naming for the ToC lines and headers. This leads to a problem that my labels and references use numbers instead of the letters defined for the counter. With the command below I would have to define the header for each appendix manually.

\refstepcounter{AppCounter}
\phantomsection\addcontentsline{toc}{section}{Appendix \Alph{AppCounter} - Some name}
\includegraphics ...
\label{app:appA}

The other problem with using appendix package is that I could not figure out how to have appendix labels without chapters/sections. And chapter/section commands add the text to the page, which is something I have no use for.

saikon
  • 73
  • First to get the "Appendices" entry in the toc you should add \addcontentsline{toc}{chapter}{Appendices} in your .tex file just before the first appendix. – Ludovic C. Jul 18 '13 at 13:17
  • How should the appendices heading (not headers) look like in your document? Do they appear using the formatting for chapters? – Gonzalo Medina Jul 18 '13 at 13:24
  • @LudovicC. I used that command when adding everything manually. Now I am looking for something that would do the additions to ToC somewhat automatically. – saikon Jul 18 '13 at 13:30
  • @GonzaloMedina The same formatting used for chapters is not exactly what I am looking for. The format now has only the chapter name in the top left corner and the page number on top right. I would like it to have the appendix name in there also. And I have manually added the page number to the bottom of the page. (\fancyhead[R]{Appendix \Alph{AppCounter} - Some name} and \fancyfoot[C]{\thepage}) – saikon Jul 18 '13 at 13:33
  • As I said, I am not asking about headers. I am asking about the headings (possible label + number + title. as in "Chapter A The title of the Appendix"). Do you use \chapter to produce the headings? – Gonzalo Medina Jul 18 '13 at 13:40
  • @GonzaloMedina Oh, sorry, I didn't read your message carefully enough. Currently I am not using anything to produce the appendice headings, everything is set up manually. What I would like is to have a chapter called "Appendices" which appears in the ToC. Then all of the separate appendix-titles would be sections numbered with A, B, C, etc.

    So the idea is basically that I want to have an separate appendix appear with a command that sets the headers and ToC entries.

    – saikon Jul 18 '13 at 13:54
  • How do you want the "Appendix" heading and the appendices headings to appear in your document (not in the toc)? Should that be like chapter and sections headings or do you want anything special? – Ludovic C. Jul 18 '13 at 14:05
  • @LudovicC. I don't want chapter/section commands to add any headings to the page itself when working with appendices. The problem with those commands now is that the heading itself takes over space from the page and not enough space is left for the content (the appendix) itself. That is why I want the headings to be in the headers and leave to whole page for the content. – saikon Jul 18 '13 at 14:14

2 Answers2

3

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}

Table of contents

First appendix

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}
}
Ludovic C.
  • 8,888
  • Thank you @Ludovic. Tweaked your code a little bit, but this really worked like a charm. Had to fiddle a bit with it to get the hyperref links working correctly. Added my result to this question also. – saikon Jul 19 '13 at 07:19
2

Modified the solution a little bit sent by @Ludovic.

  • Added a title page for appendices using \appendixpage
  • Removed the label from the enviroment since Sublime Text & LaTeXTools cannot parse it from there
  • Reordered the elements a little bit to get hyperref links working
  • Used the name commands from appendix package to easily change the values used (for different languages)
  • The margin settings I added were just specific for my needs

Here is the solution I ended up using:

    \newcommand{\startappendix}{%
    \renewcommand{\appendixtocname}{Appendices}%
    \renewcommand{\appendixname}{Appendix}%
    \renewcommand{\appendixpagename}{Appendices}%
    \cleardoublepage%
    \appendixpage%
    \fancyhead{}%
    \fancyfoot{}%
    \newgeometry{margin=2cm}%
    \renewcommand{\headrulewidth}{0pt}%
    \setlength{\headheight}{15pt}%
    \fancyhead[L]{{\bfseries \appendixtocname}}%
    \fancyfoot[C]{\thepage}%
    \pagestyle{fancy}%
    \setcounter{section}{0}%
    \renewcommand{\thesection}{\Alph{section}}%
    }

    \newenvironment{newappendix}[1]%
    {\gdef\headername{#1}%
    \refstepcounter{section}%
    \phantomsection\addcontentsline{toc}{section}{\appendixname \thesection~- \headername}%
    }
    {\fancyhead[R]{\appendixname~\thesection~- \headername}%
    \cleardoublepage}
saikon
  • 73