4

I would like to do redefine the section title command by enclosing it with ornaments like the following:

~ornament~ SectionTitle ~ornament~

I had looked up the titlesec documentation and many more example on how to redefine titles without any clues on how to add text after the title.

The aim is to add text in the section title after the \thesection command.

tldr;

I find out how to redefine \section to do that:

~ornament~ SectionTitle

but not how to do that:

~ornament~ SectionTitle ~ornament~

2 Answers 2

2

The last mandatory argument of \titlesec may end with a one argument command, which the title is passed to.

\documentclass{article}
\usepackage{titlesec}

\titleformat{\section}[block]
 {\Large\bfseries}
 {\thesection}
 {1em}
 {\makeornament}

\newcommand{\makeornament}[1]{$\clubsuit$~#1\unskip~$\clubsuit$}

\begin{document}

\section{A section}
\section*{Unnumbered section}

\end{document}

enter image description here

1
  • Thank you both for your answers that was exactly what I wanted ! I prefer egreg's answer which is simpler.
    – tugdual
    Dec 12, 2018 at 22:51
2

The following seems to be what you're after; you can redefine \secornament to be/use whatever you want.

enter image description here

\documentclass{article}

\usepackage{xparse}

\newcommand{\secornament}{$\clubsuit$}
\newcommand{\secornamentstyle}{}

\let\oldsection\section
\RenewDocumentCommand{\section}{ s o m }{%
  \IfBooleanTF{#1}
    {\oldsection*{\secornamentstyle{#3}}}% \section*{...}
    {\IfValueTF{#2}
       {\oldsection[#2]{\secornamentstyle{#3}}}% \section[..]{...}
       {\oldsection[#3]{\secornamentstyle{#3}}}% \section{...}
    }%
}

\begin{document}

% Keep \secornamentstyle to do nothing, otherwise \tableofcontents would set
% \section*{Contents} using the ornaments
\tableofcontents

\renewcommand{\secornamentstyle}[1]{\secornament~#1~\secornament}
\section{A section}
\section*{Unnumbered section}

\end{document}

\section is redefined using xparse for ease of distinguishing between the optional starred * version and optional arguments.

You must log in to answer this question.

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