0

I'm trying to write a small subtitle, but I want to place it over the section name: something like this

I use the documentclass report. Thanks in advance!

1
  • 2
    Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
    – cabohah
    May 15 at 10:10

2 Answers 2

5

For not numbered sections with \section*{…} you can use something like:

\documentclass{report}

\begin{document}

\section*{%
  {\small\normalfont\itshape Small subtitle\\}%
  Section name}

\end{document}

But note, that this is more or less just an ugly hack.

A clean solution is very easy for chapters, using the KOMA-Script report class scrreprt:

\documentclass[sfdefaults=false]{scrreprt}

\begin{document}
\setchapterpreamble[o]{\textit{Small subtitle}}
\chapter*{Chapter name}
\end{document}

enter image description here

For sections preambles can be added just with a redefinition of \sectionlinesformat, which can be done using xpatch:

\xpretocmd{\sectionlinesformat}{%
  \Ifstr{#1}{section}{%
    \ifx\@sectionpreamble\@empty\else
      {\@sectionpreamble\par}%
      \nobreak
      \global\let\@sectionpreamble\@empty
    \fi
  }{}%
}{\typeout{patching \string\sectionlinesformat\space
    successfull}}{\PatchFailure}

This just outputs \@sectionpreamble immediately before the section heading, if the command is not empty. To set the preamble, you could define a new command:

\newcommand*{\sectionpreamble}[1]{\def\@sectionpreamble{#1}}

However, I would like to set the preamble text using an extended option of \section. KOMA-Script allows to define such new options, when you activate the extended option feature using one of the options headings=optiontotocandhead, headings=optiontotoc, or headings=optiontohead. Additionally it would be nice, not to need to set the font every time, so I also define a new font element:

\documentclass[sfdefaults=false,headings=optiontotocandhead]{scrreprt}

\usepackage{xpatch}

\makeatletter
\xpretocmd{\sectionlinesformat}{% do this before the standard \sectionlinesformat
  \Ifstr{#1}{section}{% if it is a section
    \ifx\@sectionpreamble\@empty\else% and \@sectionpreamble is not empty
      \begingroup % output \@sectionpreamble
        \normalfont\usekomafont{sectionpreamble}{\@sectionpreamble\par}%
      \endgroup
      \nobreak
      \global\let\@sectionpreamble\@empty% and make it empty
    \fi
  }{}%
}{\typeout{patching \string\sectionlinesformat\space
    successfull}}{\PatchFailure}
\newcommand*{\@sectionpreamble}{}% default is empty
\newkomafont{sectionpreamble}{\small\itshape}% new font element
\FamilyStringKey[.section]{KOMAarg}{preamble}{\@sectionpreamble}% new section option to set the preamble
\makeatother

\begin{document}
\setchapterpreamble[o]{\textit{Small subtitle}}
\chapter*{Chapter name}

\section[preamble={Small subtitle},nonumber]{Section name}

\end{document}

enter image description here

The KOMA-Script solutions also work for numbered chapters and sections:

\documentclass[sfdefaults=false,headings=optiontotocandhead]{scrreprt}

\usepackage{xpatch}

\makeatletter
\xpretocmd{\sectionlinesformat}{% do this before the standard \sectionlinesformat
  \Ifstr{#1}{section}{% if it is a section
    \ifx\@sectionpreamble\@empty\else% and \@sectionpreamble is not empty
      \begingroup % output \@sectionpreamble
        \normalfont\usekomafont{sectionpreamble}{\@sectionpreamble\par}%
      \endgroup
      \nobreak
      \global\let\@sectionpreamble\@empty% and make it empty
    \fi
  }{}%
}{\typeout{patching \string\sectionlinesformat\space
    successfull}}{\PatchFailure}
\newcommand*{\@sectionpreamble}{}% default is empty
\newkomafont{sectionpreamble}{\small\itshape}% new font element
\FamilyStringKey[.section]{KOMAarg}{preamble}{\@sectionpreamble}% new section option to set the preamble
\makeatother

\begin{document}
\setchapterpreamble[o]{\textit{Small subtitle}}
\chapter{Chapter name}

\section[preamble={Small subtitle}]{Section name}

\end{document}

enter image description here

So a good solution depends on your real requirements. Unfortunately you've not shown a minimal working example and not explained how you really want to use it.

2
  • 1
    Thank you so much! But how about if it a section? May 15 at 10:12
  • 1
    @MohammedItani A section does not always start a new page. So without a MWE (see the comment below the question) I don't see a great problem in printing something before a section title. So if you need more help, have a look at the link in the comment to your question.
    – cabohah
    May 15 at 11:52
2

You could overload \section:

\documentclass{article}

\NewCommandCopy{\latexsection}{\section}
\RenewDocumentCommand{\section}{sO{#3}mo}{%
  \IfBooleanTF{#1}
    {% \section*
      \IfNoValueTF{#4}
        {% no subtitle
          \latexsection*{#3}%
        }%
        {% with subtitle
          \makesubtitlesectionstar{#3}{#4}%
        }%
    }%
    {% \section
      \IfNoValueTF{#4}
        {% no subtitle
          \latexsection[#2]{#3}%
        }%
        {% with subtitle
          \makesubtitlesection{#2}{#3}{#4}
        }%
    }%
}

\ExplSyntaxOn

\NewDocumentCommand{\formatsubtitle}{}{\normalsize\normalfont\itshape}

\NewDocumentCommand{\makesubtitlesectionstar}{mm}
 {
  \seq_set_split:Nnn \l_tmpa_seq { ~ } { #1 }
  \seq_pop_left:NN \l_tmpa_seq \l_tmpa_tl
  \latexsection*{%
   \begin{tabular}[b]{@{}l@{}}\makebox[0pt][l]{\formatsubtitle #2}\\\l_tmpa_tl\end{tabular}
   \c_space_tl\seq_use:Nn \l_tmpa_seq { ~ }
  }
 }
\NewDocumentCommand{\makesubtitlesection}{mmm}
 {
  \seq_set_split:Nnn \l_tmpa_seq { ~ } { #2 }
  \seq_pop_left:NN \l_tmpa_seq \l_tmpa_tl
  \latexsection[#1]{%
   \begin{tabular}[b]{@{}l@{}}\makebox[0pt][l]{\formatsubtitle #3}\\\l_tmpa_tl\end{tabular}
   \c_space_tl\seq_use:Nn \l_tmpa_seq { ~ }
  }
 }

\ExplSyntaxOff

\begin{document}

\tableofcontents

\section*{Section title}[Small subtitle]

\section{Section title}[Small subtitle]

\section{Section title, very long so that it has to be split across lines}[Small subtitle]

\end{document}

Without the trailing optional argument, you don't get a subtitle.

Note that the table of contents is unaffected.

The idea is to overset the subtitle to the first word of the section title, in a zero width box.

enter image description here

You must log in to answer this question.

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