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 would like to insert some code before the chapter title appears. I have defined:

\newcommand{\toptitle}[2]{\itshape #1 \hfill #2 \par}

Then, if I am writing for example:

\begin{document}
\toptitle{Quantum Mechanics}{15.10.2011}
\chapter{My Chapter}
\end{document}

the chapter title does not appear because this code precedes it. I am using the titlesec package but couldn't find a command to append code before the chapter title. Is it possible?

share|improve this question
add comment

2 Answers

If you use a documentclass of KOMA you can use the command \setchatperpreamble In the following example you can see the using. You can combine \setchapterpreamble with the command \dictum which is also provided by KOMA.

\documentclass{scrreprt}

\usepackage{lipsum}

\begin{document}
\setchapterpreamble[o]{Text}
\chapter{foo}
\lipsum

\end{document}

For more information please see the documentation.

By using the package titlesec you can define you own toptitle command. One suggestion is seen below

\documentclass{book}
\usepackage{titlesec}

%\titleformat{command}
%            [shape]
%            {format}
%            {label}
%            {sep}
%            {before-code}
%            [after-code]

\makeatletter
\newcommand\toptitle[1]{%
   \def\@toptitle{#1}}
\toptitle{}
\newcommand*\outputtoptitle[1][\linewidth]{%
    \ifx\@toptitle\@empty
      no Toptitle\else
      \parbox{\linewidth}{\@toptitle}\par\kern12pt\fi
}
\titleformat{\chapter}%
            {\outputtoptitle\huge\bfseries}%
            {\thechapter}%
            {12pt}%
            {}

\makeatother 
\usepackage{lipsum}

\begin{document}
\toptitle{\lipsum[1]}
\chapter{foo}
\lipsum

\end{document}
share|improve this answer
    
Thanks but is there a solution with the book class, maybe with the titlesec package as I wrote? –  Andro Oct 15 '11 at 10:20
    
@Andro: You didn't wrote any documentclass. With the package titlesec you can define you own style with \titleformat. One argument is for precode. –  Marco Daniel Oct 15 '11 at 11:44
    
I couldn't find such an argument. Only one before the chapter title body (and after the label). Could you give a MWE? –  Andro Oct 15 '11 at 12:30
    
@Andro: I edited my post –  Marco Daniel Oct 15 '11 at 14:42
    
Thanks. but I would like to set the \toptitle by hand after \begin{document} every time use a new chapter. I see there is a lot of tex programming. Is there a simple solution just with an option of the titlesec package? –  Andro Oct 15 '11 at 16:30
show 1 more comment

Without using titlesec, you can redefine the \@makechapterhead command (defined in book.cls) which actually typesets the chapter title; you could do something along these lines:

\documentclass{book}
\usepackage{lipsum}

\newcommand\toptitle{}
\makeatletter
\def\@makechapterhead#1{%
  \vspace*{50\p@}%
  {\parindent \z@ \raggedright \normalfont
    \ifnum \c@secnumdepth >\m@ne
      \if@mainmatter
        \toptitle\par
        \huge\bfseries \@chapapp\space \thechapter
        \par\nobreak
        \vskip 20\p@
      \fi
    \fi
    \interlinepenalty\@M
    \Huge \bfseries #1\par\nobreak
    \vskip 40\p@
  }}
\makeatother

\begin{document}

\renewcommand\toptitle{{\itshape Quantum Mechanics\hfill 15.10.2011}}
\chapter{Test chapter}
\lipsum

\end{document}

enter image description here

Using the etoolbox package, the above redefinition can be done in a shorter way:

\documentclass{book}
\usepackage{etoolbox}
\usepackage{lipsum}

\newcommand\toptitle{}
\makeatletter
\patchcmd{\@makechapterhead}{\if@mainmatter}{\if@mainmatter\toptitle\par}{}{}
\makeatother

\begin{document}

\renewcommand\toptitle{{\itshape Quantum Mechanics\hfill 15.10.2011}}
\chapter{Test chapter}
\lipsum

\end{document}
share|improve this answer
    
Hi. 2 things: (1) is it possible to achive my goal only using the titlesec package, without dealing with TeX primitive? (2) in your example, is it possible to modify the \toptitle such that it would suffice to write \toptitle{Quantum Mechanics}{15.10.2011} (without design)? it means change the \patchcmd accordingly. thanks –  Andro Oct 15 '11 at 16:39
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.