13

I'm using XeLaTeX to take notes in class, and I am using a new section for each lecture. Since the lectures have different teachers, I'd like a small italic subtitle for each \section.

I'm using the article class.

How would I go about doing that?

2 Answers 2

11

One problem with simply adding some formatted "teacher name" text after \section is that nothing prevents a page break immediately after the teacher name. (By default, LaTeX ensures that a section heading is followed by at least two lines of text.) Therefore I would use the titlesec package and its ability to execute additional code after the section title has been typeset (but still within \section).

\documentclass{article}

\newcommand*{\sectionpostamble}{}
\newcommand*{\teacher}[1]{%
  \def\sectionpostamble{#1}%
}

\usepackage{titlesec}
\titleformat{\section}
  {\normalfont\Large\bfseries}{\thesection}{1em}{}
  [\normalfont\small\itshape\raggedleft\sectionpostamble
  \global\let\sectionpostamble\relax]

\usepackage{blindtext}

\begin{document}

\teacher{Donald~E. Knuth}
\section{foo}

\blindtext

\section{bar}

\blindtext

\end{document}

(The blindtext package is only used to add some dummy text to the example.)

2
  • What would I have to change to make this work with subsections as well? Feb 11, 2011 at 18:37
  • 1
    @InfiniteSquirrel: I believe just change \titleformat{\section} to \titleformat{\subsection}, then adjust the formatting in the three lines following to typeset the subsection how you like. For me, this involved changing \Large to \large and \thesection to \thesubsection, both in the first line following the \titleformat command.
    – Tim
    Nov 4, 2011 at 13:57
11

You can define such a command yourself. For example:

\newcommand\teacher[1]{\begin{flushright}\small\textit{#1}\end{flushright}}

This can be used as

\section{TeX}
\teacher{Prof.~Knuth}
7
  • 2
    Should be \teacher{Prof.~Knuth}, shouldn't it? To get the correct spacing I mean. (My German nature tells me to also add the Dr.~, while I'm not sure if the full title is necessary for class notes.) Feb 1, 2011 at 17:44
  • @Martin: Well, I agree it's a good manner to use ~. However, ~ does not modify the spacing, but only mean "nonbreak space". There's no different here (it's very short).
    – Leo Liu
    Feb 1, 2011 at 17:57
  • 4
    @Leo Liu: There is normally no change in spacing inside normal text if ~ is used, BUT TeX adds an extra wide space after the . because it thinks its the end of a sentence. Using a ~ or \ fixes that. The ~ has the added benefit of being non-breakable as you pointed already out. I would therefore add a \frenchspacing in the flushright environment to directly disable the extra space after .. Feb 1, 2011 at 18:11
  • @Martin: You are right. I forgot that.
    – Leo Liu
    Feb 1, 2011 at 18:23
  • Is there any way of eliminating the line spacing between the \section and the \teacher? Feb 1, 2011 at 20:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .