# titlesec image after

Can someone explain to me why the second of these two doesn't work?

The first produces an image before the section name, where the second produces an error claiming there is no \begin{document}

\titleformat{\section}[hang]{\color{black}\Large\bf\filcenter}{}{0in}{\includegraphics[width=\linewidth]{blue.png}}[]

\titleformat{\section}[hang]{\color{black}\Large\bf\filcenter}{}{0in}{}[\includegraphics[width=\linewidth]{blue.png}]

• Probably because the last (optional) argument is in vertical mode for hang shape. As a confirmation, try to see if it's OK with runin shape. Mar 20, 2014 at 10:00

The problem is the inner [] of the optional argument for \includegraphics, since LaTeX cannot balance square brackets, the closing ] of the optional argument is misinterpreted as the closing square bracket for the last optional argument of \titleformat; to prevent the error, you need to hide the inner square brackets using an extra pair of braces:

\documentclass[draft]{article}
\usepackage{xcolor}
\usepackage{titlesec}
\usepackage[demo]{graphicx}

\titleformat{\section}[hang]
{\color{black}\Large\bfseries\filcenter}
{}
{0in}
{\includegraphics[width=\linewidth]{blue.png}}[]

\titleformat{\section}[hang]
{\color{black}\Large\bfseries\filcenter}
{}
{0in}
{}
[{\includegraphics[width=\linewidth]{blue.png}}]

\begin{document}

\section{Test}

\end{document}


As a side note, \bf shouldn't be used anymore; use \bfseries instead.

The demo option for graphicx simply replaces actual figures with black rectangles; do not use that option in your actual document.

• Any idea as how I could ensure that the picture is always the same vertical distance underneath the section title? Previously I was encapsulating both the section and the image with a minipage.
– Bob
Mar 21, 2014 at 1:02