3

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}]
1
  • 1
    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.
    – Bernard
    Mar 20, 2014 at 10:00

1 Answer 1

4

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}

enter image description here

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.

1
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.