6

I'm trying to customize the style of my chapter headings with tikz. Unfortunately, I can't figure out how to properly use tikz in the (optional) ⟨after⟩ argument of the the \titleformat command. With the minimal example below, my compiler seems to get lost in an endless loop; the ⟨after⟩ argument has to be the culprit because, if I remove it, I don't run into any trouble. Any idea of what's going wrong?

\documentclass[12pt]{scrbook}

\usepackage{tikz}
\usepackage{titlesec}
\usepackage{titletoc}

\titleformat{\chapter}
[hang]
{\huge}
{}
{0em}
{}
[\large \begin{tikzpicture} \draw [ultra thick] (0,0) -- (0,1);\end{tikzpicture}]
% the previous line does not work when [ultra thick] is included

\begin{document}

\chapter{Introduction}

Some text

\end{document}
jub0bs
  • 58,916
Mandy
  • 199

1 Answers1

1

The problem here is the parsing of optional arguments. LaTeX reads \titleformat{\chapter} ... [ and finds a bracket: the optional argument starts. And per definition the next closing bracket will close the optional argument. So the argument (excluding surrounding brackets will be \large \vbox{\begin{tikzpicture} \draw [ultra thick, which is obviously not want we think it should be. The trick here is to hide the ] from LaTeX by enclosing the whole argument (or the relevant part, i.e. the {tikzpicture}) in braces:

\documentclass[12pt]{scrbook}

\usepackage{tikz}
\usepackage{titlesec}
\usepackage{titletoc}

\titleformat{\chapter}
[hang]
{\huge}
{}
{0em}
{}
[\large {\begin{tikzpicture} \draw [ultra thick] (0,0) -- (0,1);\end{tikzpicture}}]

\begin{document}

\chapter{Introduction}

Some text

\end{document}

Works fine …

Tobi
  • 56,353