# insert code before chapter title

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?

-

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}


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}

-
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

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
\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}


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

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

\newcommand\toptitle{}
\makeatletter