# Using a tikz picture as a section name

I would like to use part of the answer to question TikZ, avoiding uneccessary ad-hoc coordinates calculation as the title of a section. How can I use such tikzpicture as the title of a section?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{
tikzmark,
backgrounds
}

\newcommand\constraint[1]{\textsc{#1}}

\begin{document}

\section{how to include the tikzpicture below as the title of this section?}

\begin{tikzpicture}[remember picture]
\node (ctr) {\constraint{\subnode{i}{increasing}\subnode{a}{\_}\subnode{m}{max}\subnode{b}{\_}\subnode{p}{peak}}};
\begin{scope}[on background layer]
\fill[brown!20] (i.north west) rectangle (a.south);
\fill[pink!20] (a.south) rectangle (m.north east);
\fill[violet!20] (b.north |- p.north) rectangle (p.south  east);
\end{scope}
\end{tikzpicture}

\end{document}


You have to pack it in a robust command in the preamble of your document. The command has to be visible before \begin{document} because it will be in written to the .aux file.

\documentclass{article}
\pagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{
tikzmark,
backgrounds
}

\newcommand\constraint[1]{\textsc{#1}}

\DeclareRobustCommand\sectionpicture{%
\begin{tikzpicture}[remember picture,baseline=(ctr.base)]
\node (ctr) {\constraint{\subnode{i}{\strut increasing}\subnode{a}{\strut\_}\subnode{m}{\strut max}\subnode{b}{\strut\_}\subnode{p}{\strut peak}}};
\begin{scope}[on background layer]
\fill[brown!20] (i.north west) rectangle (a.south);
\fill[pink!20] (a.south) rectangle (m.north east);
\fill[violet!20] (b.north |- p.north) rectangle (p.south  east);
\end{scope}
\end{tikzpicture}%
}

\begin{document}

\section{how to include the tikzpicture below as the title of this section? \sectionpicture}

\end{document}


• Fine, but how to ensure that all 3 rectangles have the same height, which is not any more the case. – Nicolas Beldiceanu Sep 2 '18 at 11:06
• @NicolasBeldiceanu Add \strut, see updated answer. – Henri Menke Sep 2 '18 at 11:28
• How does it come that the display of the font \textsc is changed from upper case to lower case? Is it not possible to keep the upper case? – Nicolas Beldiceanu Sep 2 '18 at 11:34
• @NicolasBeldiceanu You need to use a font with bold small caps or don't use bold for section titles. – Henri Menke Sep 2 '18 at 11:35
• @NicolasBeldiceanu See Small Caps and Bold Face for more details. – Henri Menke Sep 2 '18 at 11:42