Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I need (publisher requirement) to format the chapter headings in a document in such a way, that the chapter number is aligned with the text on the page, but if the chapter is longer than one line, the following lines should have a horizontal space of 5mm from the beginning of the first line.

Maybe best explained in this picture:

enter image description here

The green arrow should be 5mm, but the blue arrow might not be 5mm.

Since I use KOMA, I can't use titlesec. Instead I tried to achieve this, by modifying an example from the KOMA guide:

\documentclass{scrbook}

\renewcommand*{\chapterformat}{\makebox[5mm][l]{\thechapter}}

\begin{document}
\chapter{A very long chapter title asd asd asd asd asd asd asd asd asd asd asd asd asd asd asd asd}
kjdhfaksdjhf 
\end{document}

This adds an \mbox of length 5mm around the chapter number, but the problem is now, that (in the picture) also the blue arrow is always 5mm long. When the chapter number gets bigger (2 digits), the 5mm are too short.

Now the question: is there a way to change the horizontal space only on the second/third/... line (when the KOMA-script class is used).

The distance between the chapter number and the first letter of the chapter heading should be the same no matter what the number is. That means no matter whether the number is one digit or two digits.

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

One way of doing this would be to patch the command that typesets the chapter heading \@@makechapterhead and insert the desired space. This way it is automated and won't form part of the ToC entries. Patching is possible using the etoolbox package.

In the MWE below, you need to specify the lengths \chapnumwidth and \chaplabelsep using \setlength commands, and the patch will take care of the rest. Using/setting lengths this way makes for a more generalized solution that can be modified in the future with very little hassle.

enter image description here

\documentclass{scrbook}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\makeatletter
\newsavebox{\@chapnumbox}% Box to store chapter number
\patchcmd{\@@makechapterhead}% <cmd>
  {\size@chapter{#1}}% <search>
  {\sbox{\@chapnumbox}{\size@chapter{\thechapter}}% Store chapter number
   \size@chapter{\hspace*{\dimexpr\chaplabelsep-\chapnumwidth+\wd\@chapnumbox\relax}#1}}% <replace>
  {}% <success>
  {}% <failure>
\makeatother
\renewcommand*{\chapterformat}{\makebox[\chapnumwidth][l]{\thechapter}}
\newlength{\chapnumwidth} \setlength{\chapnumwidth}{5mm}% Chapter number/label width
\newlength{\chaplabelsep} \setlength{\chaplabelsep}{2em}% Chapter number/label separation
\begin{document}
\chapter{A very long chapter title asd asd asd asd asd asd asd asd asd asd asd asd asd asd asd asd}
kjdhfaksdjhf

\setcounter{chapter}{21}

\chapter{A very long chapter title asd asd asd asd asd asd asd asd asd asd asd asd asd asd asd asd}
kjdhfaksdjhf 

\end{document}

The format for patching is

\patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}

which replaces <search> in <cmd> with <replace> and executes <success> if successful, or <failure> otherwise. You'll notice that the patching in the MWE saves the chapter number (in the appropriate font size/format) in a box

\sbox{\@chapnumbox}{\size@chapter{\thechapter}}% Store chapter number

and adds

\hspace*{dimexpr\chaplabelsep-\chapnumwidth+\wd\@chapnumbox\relax}

before the chapter heading (#1). This means that your chapter heading will start exactly \chaplabelsep from the chapter label, since the removed \chapnumwidth is the actual \chapterformat width. Of course, you can modify as needed.

In summary, the width from the left margin for the chapter heading is specified by the \chapterformat command (as you've already done), while the first line is adjusted using the patched form of \@@makechapterhead which restores the \chapterformat width and subsequently advances it by the width of the chapter number/label and \chaplabelsep.

share|improve this answer
    
Thank you for the answer. However I might have said one important thing not clear enough: The distance between the chapter number and the first letter of the chapter heading should be the same no matter what the number is. That means no matter whether the number is one digit or two digits. If you had a modified answer to this, that would be great. Thank you. –  xubuntix Nov 17 '11 at 20:40
1  
@xubuntix: I've updated my answer accordingly, and generalized it to be more generic, if future needs arise. –  Werner Nov 17 '11 at 21:12
    
really great thanks! –  xubuntix Nov 17 '11 at 21:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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