Search:   Help

Navigation

Groups

LyX documentation

Edit

Shared groups

Links

ModifyChapterEtc

Categories: Tips
<< | Page list | >>

How To Modify Chapter, Section, and the Like

The question arose on the mailing list how one could modify the chapter environment so that chapter titles would be single spaced even though the rest of the document was double spaced. The obvious thing to try would be something like:

\let\oldchap=\chapter
\renewcommand\chapter[2][]{\singlespacing\chapter[#1]{#2}\doublespacing}

The problem with this is that if you have a \chapter* environment, you'll get "*" as the title in the table of contents. The reason is that LaTeX then reads "*" as the first argument of \chapter.

The question is, obviously, more general: How can one adapt the chapter, section, and similar environments, while using the \let trick to keep all the old behavior as well?

Here's code that works:

\let\oldchap=\chapter
\renewcommand*{\chapter}{%
  \secdef{\Chap}{\ChapS}%
}
\newcommand\ChapS[1]{\singlespacing\oldchap*{#1}\doublespacing}
\newcommand\Chap[2][]{\singlespacing\oldchap[#1]{#2}\doublespacing}

The key to this is the \secdef macro. It does the following: First, it checks to see whether the next character is a "*" (using the \if@star macro). If so, then it is simply replaced by its second argument: So, in this case, \chapter* reduces to \ChapS, and the single argument of \chapter* becomes the single argument of \ChapS. But if the next character is not a "*", then \secdef does two things: First, it checks to see whether an optional argument was provided (using the macro \ifnextchar to check for a "["); if an optional argument was provided, then \secdef is simply replaced by its first argument, and the optional argument, and any following arguments, become arguments to that first argument. So, in this case, \chapter[Short]{Long} would reduce to: \Chap[Short]{Long}. If no optional argument was provided, however, then \secdef creates an optional argument whose content is duplicated from the first argument it finds. That is, in this case, \chapter{Long} would reduce to: \Chap[Long]{Long}.

Tips

Edit - History - Print - Recent Changes - All Recent Changes - Search
Page last modified on 2007-03-01 17:19 UTC