简体   繁体   中英

How to make 'appendix' appear in toc in Latex?

How to make word 'appendix' appear in the table of contents? Right now toc looks like this:

1 ......
2 ......
.
.
A .....
B .....

I would like it to be:

1 ......
2 ......
.
.
Appendix A .....
Appendix B .....

My latex source file structure is like this:

\\begin{document}
\\tableofcontents
\\include{...}
\\include{...}
\\appendix
\\include{...}
\\include{...}
\\end{document}

There's a couple of ways to solve this problem; unfortunately, I've only got a hack for you at this stage. One problem is that if we redefine the section number "A" to include the word "Appendix", it messes up the formatting of the table of contents. So instead, I've just defined a new sectioning command that prints the section without a number and inserts "Appendix X" manually.

Kind of ugly, but at least it works without having to change any markup :)

\documentclass{article}

\makeatletter
\newcommand\appendix@section[1]{%
  \refstepcounter{section}%
  \orig@section*{Appendix \@Alph\c@section: #1}%
  \addcontentsline{toc}{section}{Appendix \@Alph\c@section: #1}%
}
\let\orig@section\section
\g@addto@macro\appendix{\let\section\appendix@section}
\makeatother

\begin{document}

\tableofcontents

\section{goo}
\label{a} 
This is sec~\ref{a}

\section{har}
\label{b}
This is sec~\ref{b}

\appendix
\section{ji}
\label{c} 
This is app~\ref{c}
\subsection{me}
does this look right?

\end{document}

This is probably most easily achieved by using the appendix package , or the memoir class .

If you don't want to use a prepackaged solution, you'll have to hack the sectioning commands. When I needed to do this for my dissertation, I cloned the report class, and edited until I made the margins lady happy. What you're looking for is the definition of the \\addcontentsline macro.

For my thesis, I did the following:

\appendix
\addcontentsline{toc}{section}{Appendix~\ref{app:scripts}: Training Scripts}
\section*{Sample Training Scripts}
\label{app:scripts}
Blah blah appendix content blah blah blah.

Explanation: I manually added a line to the TOC so I would have "Appendix X:..." show up in my TOC. Then I excluded the actual section command from the TOC by using an asterisk.

The appendix package is really good and simple solution. My answer can be helpful for who wants to change chapters numbering style, for example, with using cyrillic alphabet or roman digits. The appendices numbering style is hardcoded in the \\@resets@pp command (I looked in sources here http://hal.in2p3.fr/docs/00/31/90/21/TEX/appendix.sty ). I solved it by simple redefining this command to my own. Just add this code into your preamble:

\makeatletter

    \renewcommand{\@resets@pp}{\par
        \@ppsavesec
        \stepcounter{@pps}
        \setcounter{section}{0}

        \if@chapter@pp
            \setcounter{chapter}{0}
            \renewcommand\@chapapp{\appendixname}
            \gdef\thechapter{\Asbuk{chapter}} % changed
        \else
            \setcounter{subsection}{0}
            \gdef\thechapter{\Asbuk{section}} % changed
        \fi

        \if@pphyper
            \if@chapter@pp
                \renewcommand{\theHchapter}{\theH@pps.\Asbuk{chapter}} % changed
            \else
                \renewcommand{\theHsection}{\theH@pps.\Asbuk{section}} % changed
            \fi

            \def\Hy@chapapp{\appendixname}%
        \fi
    \restoreapp
}

\makeatother

As a result,

Appendix A
Appendix B
Appendix C
...

will change to

Appendix A
Appendix Б
Appendix В
... etc

I'm not a latex expert, and I can't guarantee this code won't break something else.

Based on @Will Robertson's answer, the code below defines the same thing but for chapter and also fixes the fact that chapter* does not add to the header when using the fancyhdr package.

With this in the preable all issues are resolved.

\makeatletter
\newcommand\appendix@chapter[1]{%
    \refstepcounter{chapter}%
    \def\app@ct{Appendix \@Alph\c@chapter: #1}
    \orig@chapter*{\app@ct}%
    \markboth{\MakeUppercase{\app@ct}}{\MakeUppercase{\app@ct}}
    \addcontentsline{toc}{chapter}{\app@ct}%
}
\let\orig@chapter\chapter
\g@addto@macro\appendix{\let\chapter\appendix@chapter}
\makeatother

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM