8

I'm using scrartcl and am trying to move the appendices in the toc one level up so that they are formatted as if they where a subsection instead of a section.

I've adapted my solution from this question and my current MWE (min.tex) looks like this:

\documentclass{scrartcl}

\usepackage[page]{appendix}  % better appendices: \begin{appendices}
\usepackage{etoolbox}           % needed for code below (\AtBeginEnvironment)
\AtBeginEnvironment{appendices}{%
    \addappheadtotoc             % add Appendices to toc
    \makeatletter
    \addtocontents{toc}{%        % move appendix sections one level up in toc
        \begingroup              % make sure we reset section level redefinition after appendices
        \let\protect\l@section\protect\l@subsection
        \let\protect\l@subsection\protect\l@subsubsection}}
\AtEndEnvironment{appendices}{%
    \addtocontents{toc}{\endgroup}}  % reset section level redefinition

\begin{document}
\tableofcontents

\begin{appendices}
\section{Variablen}
\section{Tabellen}
\end{appendices}

\end{document}

When compiling with lualatex the second time to get the table of contents, I get the following error:

) (./min.toc

! LaTeX Error: Something's wrong--perhaps a missing \item.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...

l.3 ...tsline {section}{\numberline {A}Variablen}{1}

Looking at min.toc, it is clear why. There are spourious spaces added in the commands

\begingroup \let \l @section\l @subsection \let \l @subsection\l @subsubsection

which should read

\begingroup \let \l@section\l@subsection \let \l@subsection\l@subsubsection

When I add the \let commands just after \begin{appendices} it works fine. So my suspicion is, that \AtBeginEnvironment is adding those spaces. I'd rather like to keep this in the preamble and not put it in the main document. Is there a way to fix this?

Update

The formatting problem described below is fixed imho quite more elegantly in the accepted answer than my edit here. I suggest to use that. But I'll leave my fix for the record.


The solution from the answer below and from the above question with the fix in the comments by cgnieder has inconsistency problems with the "Appendices" heading of the appendix package. To adapt it to the formatting of sections used in scrartcl, the command \@sec@pppage has to be renwed (modified from the appendix package):

\makeatletter
\renewcommand{\@sec@pppage}{%
  \par
  \addvspace{4ex}%
  \@afterindentfalse
  {\parindent \z@ \raggedright
   \interlinepenalty \@M
   % the following line was changed from the original
   \usekomafont{disposition} \usekomafont{section} \appendixpagename%
   \markboth{}{}\par}%
  \if@dotoc@pp
    \addappheadtotoc
  \fi
  \nobreak
  \vskip 3ex
  \@afterheading
}
\makeatother

This code might be a bit overkill, but it works. Unfortunately, my LaTeX programming skills are not quite where they could be. Maybe someone has a better solution?

justibus
  • 220
  • I am not sure, but adding \begingroup inside an argument without \endgroup does not look very healthy. And you mean 'pushing' down the appendix, since for scrartcl appendix entries are sections, subsections is just one level below –  Aug 21 '14 at 14:19
  • 2
    Welcome to TeX.sx! :) You just need a \makeatletter before \AtBeginEnvironment and a \makeatother after all the patching is done – cgnieder Aug 21 '14 at 14:23
  • @ChristianHupfer: I do close the group in \AtEndEnvironment. If I don't do it at the end of the appendices environment, the bibliography or any other section that comes after is also pushed down. – justibus Aug 21 '14 at 14:30
  • @cgnieder: That works nicely! I have to read up on those two commands. Could you convert is to an answer so I can accept it? – justibus Aug 21 '14 at 14:33
  • @justibus: Changed the solution –  Aug 22 '14 at 06:09
  • @justibus: There's no need in fiddling about the KOMA - Script internal setup of environments/commands. –  Aug 22 '14 at 07:03

1 Answers1

4

A solution without \AtBeginEnvironment and grouping, by slight redefinition of appendices environment and letting temporarily \section being the same as \subsection

Edit

There is already a subappendices environment by the package appendix. Its perhaps better to use that environment instead of hacking the appendices environment. However, this solution shows both ways.

The fact, that \begin{appendices} introduces a strange,\part- like heading for the appendix is due to the[page]option. Removing that and adding a more appropiate\section*` like heading cures this.

\documentclass{scrartcl}

\usepackage{appendix}  % better appendices: \begin{appendices}
\usepackage{etoolbox}           % needed for code below (\AtBeginEnvironment)
\usepackage{letltxmacro}%


\LetLtxMacro\StandardAppendixBegin\appendices
\LetLtxMacro\StandardAppendixEnd\endappendices%

\renewenvironment{appendices}{%
  \setcounter{subsection}{0}%
  \section*{\appendixname}
  \addcontentsline{toc}{section}{\appendixname}%
  \StandardAppendixBegin%
  \renewcommand{\thesubsection}{\Alph{subsection}}
  \LetLtxMacro\section\subsection%
}{\StandardAppendixEnd\setcounter{subsection}{0}}%

%% Adding the automatic heading for subappendices%
\AtBeginEnvironment{subappendices}{%
  \section*{\appendixname}%
  \addcontentsline{toc}{section}{\appendixname}%
  \renewcommand{\setthesubsection}{\Alph{subsection}}%
}%

\begin{document}
\tableofcontents

\section{Real Section}

\subsection{A real subsection}


\begin{appendices}%
\subsection{Variablen -- changed appendices environment}
\subsection{Tabellen -- changed appendices environment}
\end{appendices}%



\begin{subappendices}
\subsection{Variablen -- subappendices environment}
\subsection{Tabellen -- subappendices environment}
\end{subappendices}

\section{Another real section}

\end{document}

enter image description here

  • This also works. The only problem I see is, that the formatting of the appendix titles are a bit inconsistent. "Appendices" is formatted as a \part and the appendices themselves as subsections. How can I change the formatting of "Appendices" to the section formatting? – justibus Aug 21 '14 at 18:36
  • I found an answer to my comment above. See updated question. – justibus Aug 21 '14 at 19:47
  • I rather think it is the appendices environment –  Aug 21 '14 at 19:48
  • The updated answer is perfect! Thank you. One note: To also put "Appendix" into the page headings, put \pagestyle{myheadings} \markright{\appendixname} into the redefinition of the appendices and subappendices environments. – justibus Aug 22 '14 at 08:14