2

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}

enter image description here

1 Answers1

0

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}

enter image description here

Henri Menke
  • 109,596