使用 tikz 图片作为节名称

使用 tikz 图片作为节名称

我想使用问题的部分答案 TikZ,避免不必要的临时坐标计算 作为章节的标题。我怎样才能将这样的 tikzpicture 用作章节的标题?

\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}

在此处输入图片描述

答案1

您必须将其打包到文档前言中的强大命令中。该命令必须之前可见,\begin{document}因为它将写入文件.aux

\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}

在此处输入图片描述

相关内容