8

My current document is processed like this:

1. Some chapter
1.1. Some section
1.1.1. Some subsection

Appendix A.
Some appendix

What I want to have:

1 Some chapter
1.1 Some section
1.1.1 Some subsection

Appendix A: Some appendix

So I want the last dots of the numbering removed for the main part. For the appendix, I want a colon instead of the dot and the title in the same line.

A small example, including all packages used:

\documentclass[appendixprefix=true,11pt,a4paper]{scrreprt}
%The packages used in my document
\usepackage{listings}
\usepackage{mathtools}
\usepackage{graphicx}
\usepackage{enumitem}
\usepackage{chngcntr}
\usepackage[usenames,dvipsnames]{color}
%Appendix definition (Write "Appendix A")
\makeatletter
\newcommand\appendix@numberline[1]{\appendixname\ #1: }
\g@addto@macro\appendix{%
  \addtocontents{toc}{
    \let\protect\numberline\protect\appendix@numberline}%
}
\makeatother

\begin{document}
\chapter{First chapter}
\section{First section}
\subsection{First subsection}
\appendix
\chapter{Last chapter}
\end{document}

How can I make the colon and the title in the same line. And how do I remove the last dots of the titles?


Symbol1's answer doesn't work. Error message:

Undefined control sequence. \@makechapterhead ...mdepth >\m@ne \if@mainmatter
\huge \bfseries \@chapapp ... l.108 \chapter{Definition of the Problem}
  • I'm not sure how \autodot works, but \def\autodot{} solves part of the question. – Manuel Sep 29 '14 at 20:44
  • Yes, it does. Now I only need the Appendix title in the same line as the "Appendix A". – s3lph Sep 29 '14 at 20:47
  • 1
    numbers=noenddot removes the dots in a simpler way, btw – Tobias Kienzler Nov 30 '15 at 15:55
9
+50

Here's how you can obtain the output you're after in the KOMA-script classes:

enter image description here

\documentclass[appendixprefix=true]{scrreprt}

\usepackage{etoolbox}
\makeatletter
\g@addto@macro{\appendix}{%
  \patchcmd{\@@makechapterhead}% <cmd>
    {\endgraf\nobreak\vskip.5\baselineskip}% <search>
    {\hspace*{-.5em}:\space}% <replace>
    {}{}% <success><failure>
  \patchcmd{\@chapter}% <cmd>
    {\addchaptertocentry{\thechapter}}% <search>
    {\addchaptertocentry{Appendix~\thechapter:}}% <replace>
    {}{}% <success><failure>
  \addtocontents{toc}{%
    \protect\patchcmd{\protect\l@chapter}% <cmd>
      {1.5em}% <search>
      {6.5em}% <replace>
      {}{}}% <success><failure>
}
\renewcommand{\autodot}{}% Remove all end-of-counter dots
\makeatother

\begin{document}

\tableofcontents

\chapter{First chapter}
\section{First section}
\subsection{First subsection}

\appendix
\chapter{Last chapter}

\end{document}

The dots are removed through a redefinition of \autodot, while the Appendix formatting is done via a \patchcmd of \@@makechapterhead when you call \appendix. The final patch adjusts the width of the \numberline box from 1.5em to 6.5em. This adjustment is specific to the chapter-related entry in the ToC as it deals with \l@chapter.

etoolbox provides the patching capability.

  • Nice! This works fine. But is it possible to also write the "Appendix A:" into the TOC? – s3lph Sep 30 '14 at 11:06
  • Yes, I'd like to have the colon in the TOC also, which isn't given in your updated anwser btw. – s3lph Oct 1 '14 at 14:23
  • Never mind, I found out where to add the colon: {Appendix~\thechapter:} – s3lph Oct 1 '14 at 14:26
  • @the_Seppi: I've added it to my answer. – Werner Oct 1 '14 at 14:32
2

Here you are

\documentclass{book}
\begin{document}
\makeatletter
\def\@makechapterhead#1{\vspace*{50\p@}{\parindent\z@\raggedright\normalfont\ifnum\c@secnumdepth>\m@ne\if@mainmatter\huge\bfseries\@chapapp\space\thechapter: \fi\fi\interlinepenalty\@M\Huge\bfseries#1\par\nobreak\vskip40\p@}}
\makeatother
\chapter{CHAPI}
\section{SECI}
\subsection{SUBSECTI}
\appendix
\chapter{CHAPII}
\section{SECII}
\subsection{SUBSECTII}
\end{document}
  • Doesn't work. Error: Undefined control sequence. \@makechapterhead ...mdepth >\m@ne \if@mainmatter \huge \bfseries \@chapapp ... l.108 \chapter{Definition of the Problem} Definition of the problem is the 1st numbered chapter – s3lph Sep 24 '14 at 20:20
  • @the_Seppi The line 108 isn't in the example here. You have tried another code. The code here works. – wipet Sep 29 '14 at 21:14
  • This is because of the document class. Here, book is used. However, I'm using scrreprt, in which the error occurs, if you change it here. Besides, this refers to line 108 of the .toc file – s3lph Sep 30 '14 at 10:59
2

Since KOMA-Script version 3.20 you can use \DeclareTOCStyleEntry. So here is a new suggestion:

\documentclass[
  %appendixprefix=true,%<- removed
  11pt,a4paper,
  numbers=noenddot% <- added
]{scrreprt}[2016/05/10]% needs at least version 3.20

\DeclareTOCStyleEntry[
  level=\chaptertocdepth,
  indent=0pt,
  numwidth=2.3em,
  dynnumwidth,
  linefill=\hfill,
  entryformat=\appendixtocformat,
  entrynumberformat=\appendixtocnumberformat,
  pagenumberformat=\appendixtocpagenumberformat
]{tocline}{appendixchapter}
\newcommand*\appendixtocformat[1]{{\usekomafont{chapterentry}#1}}
\newcommand*\appendixtocnumberformat[1]{{\def\autodot{:}\appendixname\ #1}}
\newcommand*\appendixtocpagenumberformat[1]
  {{\usekomafont{chapterentry}\usekomafont{chapterentrypagenumber}#1}}

\usepackage{xpatch}
\xapptocmd\appendix
  {%
    \renewcommand*{\chapterformat}{%
      \mbox{\appendixname{\nobreakspace}\thechapter:%
        \IfUsePrefixLine{}{\enskip}}%
    }%
    \renewcommand*{\chaptermarkformat}{\appendixname\ \thechapter:\enskip}%
    \xpatchcmd{\addchaptertocentry}
      {\addtocentrydefault{chapter}{#1}{#2}}
      {\addtocentrydefault{appendixchapter}{#1}{#2}}%
      {}{\PatchFailed}%
  }{}{\PatchFailed}

\begin{document}
\tableofcontents
\chapter{First chapter}
\section{First section}
\subsection{First subsection}
\appendix
\chapter{Last chapter}
\end{document}

Run three times to get:

enter image description here

enter image description here

1

The following is almost the same as @werners answer, with a slight difference. With the option appendixprefix you get the prefix appendix just before the number. This contradicts with having the title on the same line (this is the way it is implemented). The main idea is the same, patching \@@makechapterhead as there happens the magic. At first i saved just nothing to be printed as autodot. When the appendix starts, some redefinitions are made, for example
- restoring the autodot to print a colon (:)
- patching the linebreak to simply nothing (no need to fiddle with spaces, the default KOMA is in control)
- If you want only the chapter entries indented by the needed amount, it gets indented dynamically by the contents of \appendixTocString. If you decide at a later point, not to print the word Appendix, remove it frome the string and it will be properly indented.

enter image description here

enter image description here

enter image description here

\documentclass[
appendixprefix=true,
]{scrreprt}
%The packages used in my document
%\usepackage{scrhack}%patches listings
%\usepackage{listings}
%\usepackage{mathtools}
%\usepackage{graphicx}
%\usepackage{enumitem}
%\usepackage{chngcntr}
%\usepackage[usenames,dvipsnames]{color}
\usepackage{etoolbox}
\renewcommand{\autodot}{}%
\newcommand{\appendixTocString}{\appendixname\space\thechapter\autodot}%
\newlength{\appendixTocStringLength}%
\settowidth{\appendixTocStringLength}{\appendixTocString}%
\addtolength{\appendixTocStringLength}{1.5em}%
\makeatletter%
\gappto{\appendix}{%Doing everything in the appendix%
\patchcmd{\@@makechapterhead}{\endgraf\nobreak\vskip.5\baselineskip}{}{}{}%
\renewcommand{\autodot}{:}%
\addtocontents{toc}{%
    \protect\patchcmd{\protect\l@chapter}%
    {1.5em}%
    {\protect\appendixTocStringLength}%
    {}{}}%
\patchcmd{\@chapter}{\addchaptertocentry{\thechapter}{\scr@ds@tocentry}%
}{%
\addchaptertocentry{\appendixTocString}{\scr@ds@tocentry}}{}{}%
}%
\makeatother%
\begin{document}
\tableofcontents
\chapter{First chapter}
\section{First section}
\subsection{First subsection}
\chapter{Second chapter}
\section{Second section}
\subsection{Second subsection}
\appendix
\chapter{In case the chapter title is really long, we need to
make sure that it gets indented properly.}
\section{appendix section}
\subsection{appendix subsection}
\chapter{appendix chapter}
\section{appendix section}
\subsection{appendix subsection}
\chapter{appendix chapter}
\section{appendix section}
\subsection{appendix subsection}
\chapter{appendix chapter}
\section{appendix section}
\subsection{appendix subsection}

\end{document}
  • To be honest, i find the output highly quesionable and incosistent. If you want the autodot in the toc as well, remove the commentsign and merge the lines (removing the space). – Johannes_B Sep 30 '14 at 14:01
  • Did this, it added the colon in the TOC. But how do I remove the indentation of the non-appendix chapters and their subs in the TOC? I don't want them indented, only the appendix TOC entries. – s3lph Sep 30 '14 at 14:10
  • @the_Seppi I changed the example to dynamically adjust the indentation dependant of the string you want to print. – Johannes_B Oct 2 '14 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.