Menu

HowTo_AppendixToC

How to add your own directories to the appendix

The basic procedure for splitting a directory was already presented in “How to create a custom table of contents for parts (\part).” For a complete solution with a separate table of contents, a separate list of figures, and a separate list of tables at the beginning of the appendix, you can expand on this solution and modify it slightly.

As mentioned in the post above, since KOMA-Script v3.23, the auxiliary file used for the table of contents is specified via the \ext@toc macro. For the list of figures, this has always been defined via \ext@figure, and for the list of tables via \ext@table. These macros can also be redefined within the document.

The tocbasic package, which is used by all KOMA-Script classes, also offers the option to define new tables of contents resp. list of floats using \DeclareNewTOC. By default, these have the owner or category set to float. This setting is also used for the list of figures and the list of tables. For the lists of floating environments in the appendix, you can simply retain this setting:

\DeclareNewTOC[%
  name=\listoflofentryname,
  listname={Figures in the Appendix},% Title of the list
]{alof}% File extension (a=appendix, lof=list of figures)
\DeclareNewTOC[%
  name=\listoflotentryname,
  listname={Tables in the Appendix},% Title of the list
]{alot}% File extension (a=appendix, lot=list of tables)

Incidentally, the name option setting only has an effect if the class option listof=entryprefix is used, which I strongly advise against for typographical reasons alone.

For the new table of contents, however, a different owner should be selected. The standard table of contents generated by \tableofcontents uses the owner ToC. Therefore, we’ll use that here as well:

\DeclareNewTOC[%
  owner=ToC,% Same setting as for \tableofcontents
  listname={Appendix Contents},% Title of the table of contents
]{atoc}% File extension (a=appendix, toc=table of contents)

If you’d rather not have the directories in the appendix respond to class options or other settings that depend on the owner or category, you can, of course, specify a separate category—for example, using owner=appendix.

Incidentally, the file extensions in the examples above were chosen arbitrarily. The comments simply explain my reasoning behind them.

Now these new directories can be used. To switch between them, it makes sense to include another command:

\newcommand*{\useappendixtocs}{%
  \renewcommand*{\ext@toc}{atoc}%
  \scr@ifundefinedorrelax{hypersetup}{}{% so that it works even without hyperref
    \hypersetup{bookmarkstype=atoc}%
  }%
  \renewcommand*{\ext@figure}{alof}%
  \renewcommand*{\ext@table}{alot}%
}

The special feature here is \hypersetup{bookmarkstype=atoc}. The hyperref package—or rather, its recommended extension bookmark, which is loaded by the KOMA-Script classes anyway—normally uses the entries in the auxiliary file with the toc extension for bookmarks (aka outlines, aka PDF table of contents, aka everyone-calls-it-something-different). As soon as we switch to the atoc extension, bookmarks are no longer generated for the entries in the appendix table of contents (links in the appendix table of contents still exist, of course). However, using the bookmarkstype option, you can instruct hyperref to use the entries in the auxiliary file with the specified extension for the bookmarks. So if you don’t want the entries in the appendix table of contents to appear in the bookmarks, simply delete these additional lines.

It’s certainly also a good idea to provide a way back to the main table of contents. For example, you’d probably prefer to list a bibliography or an index in the main table of contents—if at all—rather than in the appendix.

\newcommand*{\usestandardtocs}{%
  \renewcommand*{\ext@toc}{toc}%
  \scr@ifundefinedorrelax{hypersetup}{}{% so that it works even without hyperref
    \hypersetup{bookmarkstype=toc}%
  }%
  \renewcommand*{\ext@figure}{lof}%
  \renewcommand*{\ext@table}{lot}%
}

Here, too, in addition to simply changing the file extensions, the impact on hyperref was also taken into account.

I mentioned the listof=entryprefix option earlier. This also enables the listof=flat option for KOMA-Script classes. Unfortunately, via \if@dynlist, this applies not only to the directories with owner float resp. in the float category, but to all directories. For the table of contents, \if@tocleft is then explicitly used instead. As a result, with the previous code, listof=flat is also used for atoc. However, this can be easily fixed:

\AfterTOCHead[atoc]{%
  \let\if@dynlist\if@tocleft
}

Here, the setting for the main table of contents is now also applied to atoc, i.e., the table of contents for the appendix. Of course, you can also use \@dynlistfalse or \@dynlisttrue instead to explicitly disable or enable the flat display.

So now you can already switch back and forth between the main tables of contents and the appendix tables of contents and output the new tables directly. The format in which the tables are output can be specified at any time using the listof option (only for lists of floating environments) or via \setuptoc and \unsettoc. For more details on the option and the two commands, refer to the KOMA-Script manual. At this point, it is also recommended that you review the KOMA-Script commands used above in the manual. They are easy to find using the index.

Before I show a complete example using the code above, it’s worth noting that all of this works without the appendixtoc package and, of course, without the tocstyle package, which has never been officially supported. In principle, the use of other appendix packages may also affect the functionality of this solution. Of course, the solution also requires that you do not write directly into the auxiliary files using, for example, \addcontentsline{toc}{…} or \addcontentsline{lot}{…}. For floating environments, you should instead use, for example, \addcontentsline{\ext@table}{…}. For entries in the table of contents, you can simply use the commands \addparttocentry, \addchaptertocentry, etc.—which are documented in the manual—instead of \addcontentline{\ext@toc}{…} with the KOMA-Script classes.

Here is a complete example:

\documentclass[listof=totoc,index=totoc]{scrbook}[2021/04/30]
\usepackage{iftex}
\iftutex
  \usepackage{fontspec}
\else
  \usepackage[T1]{fontenc}
  \usepackage{lmodern}
\fi

\usepackage{makeidx}\makeindex% just as an example

\usepackage{mwe}
\usepackage{hyperref}% To demonstrate the implications/necessity of hyperref.
\usepackage{bookmark}% Because the hyperref has been significantly improved.


\DeclareNewTOC[%
  owner=ToC,% same setting as for \tableofcontents
  listname={Contents of the Appendix},% title of the directory
]{atoc}% file name extension (a=appendix, toc=table of contents)
\DeclareNewTOC[%
  name=\listoflofentryname,% irrelevant in this example
  listname={Figures of the Appendix},% title of the directory
]{alof}% Dateierweiterung (a=appendix, lof=list of figures)
\DeclareNewTOC[%
  name=\listoflotentryname,% irrelevant in this example
  listname={Tables of the Appendix},% title of the directory
]{alot}% Dateierweiterung (a=appendix, lot=list of tables)

\makeatletter
\newcommand*{\useappendixtocs}{%
  \renewcommand*{\ext@toc}{atoc}%
  \scr@ifundefinedorrelax{hypersetup}{}{% so that it works even without hyperref
    \hypersetup{bookmarkstype=atoc}%
  }%
  \renewcommand*{\ext@figure}{alof}%
  \renewcommand*{\ext@table}{alot}%
}
\newcommand*{\usestandardtocs}{%
  \renewcommand*{\ext@toc}{toc}%
  \scr@ifundefinedorrelax{hypersetup}{}{% so that it works even without hyperref
    \hypersetup{bookmarkstype=toc}%
  }%
  \renewcommand*{\ext@figure}{lof}%
  \renewcommand*{\ext@table}{lot}%
}
\AfterTOCHead[atoc]{%
  \let\if@dynlist\if@tocleft% or \@dynlistfalse or \@dynlisttrue
}
\makeatother

\AddToHook{\KOMAClassName/appendix}{% Make everything in \appendix automatic:
  \addpart{\appendixname}% - Entry in the main table of contents
  \useappendixtocs% - Switch to appendix directories
  \listofatocs% - Display the table of contents for the appendix
  \listofalofs% - Display a list of figures for the appendix
  \listofalots% - Display a list of tables for the appendix
}

\begin{document}
\tableofcontents
\listoffigures
\listoftables
\blinddocument

\begin{figure}[!h]
\centering
\rule{5cm}{2cm}
\caption{test-fig-main}
\end{figure}

\begin{table}[h]
\caption{test-tab-main\index{main}}
\noindent
\centering
\begin{tabular}{|c|c|}
\hline
a & b\tabularnewline
\hline\hline
1 & 2\tabularnewline
\hline
\end{tabular}
\end{table}

\appendix
\blinddocument

\begin{figure}[!h]
\centering
\rule{5cm}{2cm}
\caption{test-fig-appendix}
\end{figure}

\begin{table}[h]
\caption{test-tab-appendix}
\noindent
\centering
\begin{tabular}{|c|c|}
\hline
a & b\tabularnewline
\hline\hline
1 & 2\tabularnewline
\hline
\end{tabular}
\end{table}

\usestandardtocs
\bookmarksetup{startatroot}% see the bookmark manual
\printindex% just as an example

\end{document}

Incidentally, in the example, makeidx was used intentionally rather than imakeidx. For one thing, imakeidx does not respond to the KOMA-Script option index=totoc. Instead, you would have to use \makeindex[intoc]. At the same time, imakeidx always writes the table of contents entry using \addcontentsline{toc}{…}. So the entry for the index would not end up in the appendix list anyway. Consequently, you wouldn’t need \usestandardtocs either. However, I wanted to demonstrate the purpose of this command, which becomes clear if you try commenting it out in the example above and running two new LaTeX compiles. You don’t need to run MakeIndex again, though, if the index has already been generated for the unchanged example.

Incidentally, for the standard classes, simply loading tocbasic would not be sufficient to use the code above. For one thing, these classes always hard-code entries into the auxiliary file toc. Furthermore, within \@chapter, they hard-code entries into lof and lot instead of correctly using \ext@figure and \ext@table there. For these classes, it would therefore be necessary to patch \addtocontents. This is, of course, possible and, in combination with certain packages, can even be useful when using a KOMA-Script class. Such a patch might look like this (untested):

\ExplSyntaxOn
\NewCommandCopy{\appendixtoc_saved_addtocontents:nn}{\addtocontents}
\RenewDocumentCommand{\addtocontents}{m+m}{%
  \exp_args:Ne \appendixtoc_saved_addtocontents:nn
    {
      \str_case:enF {#1}
        {
          {toc} {\ext@toc}
          {lot} {\ext@table}
          {lof} {\ext@figure}
        } {#1}
    } {#2}
}
\ExplSyntaxOff

Related

Wiki (English): HowTo_PartToC
Wiki (English): HowTo_ToCConfiguration