3

I am wondering whether there would be a simpler way to get a similar figure: (1) avoid adjusting by hand the size of each coloured box, (2) avoid computing by hand the middle of each box from which the arrows are drawn.

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{calc}

\def\constraint#1{\textsc{#1}}

\begin{document}

\begin{tikzpicture}
\node (ctr) {$\constraint{increasing\_max\_peak}$};
\fill[brown!20]  ($(ctr.south west)+(0.1,0)$)  rectangle ($(ctr.north west)+(2.07,0)$);
\fill[pink!20]   ($(ctr.south west)+(2.07,0)$) rectangle ($(ctr.north west)+(2.9,0)$);
\fill[violet!20] ($(ctr.south west)+(2.9,0)$)  rectangle ($(ctr.north east)-(0.1,0)$);
\node (ctr1) {$\constraint{increasing\_max\_peak}$};
\draw[->] ($(ctr.south west)+(1.085,0)$) -- ($(ctr.south west)+(1.085,-0.7)$) node[below] {\scriptsize \constraint{Condition}};
\draw[->] ($(ctr.south west)+(2.485,0)$) -- ($(ctr.south west)+(2.485,-0.3)$) node[below] {\scriptsize \constraint{Feature}};
\draw[->] ($(ctr.south west)+(3.35,0)$) -- ($(ctr.south west)+(3.35,-0.7)$) node[below] {\scriptsize \constraint{Pattern}};
\end{tikzpicture}

\end{document}

enter image description here

3

You could for example use \subnodes from the tikzmark library. Note this requires the addition of the remember picture option to the tikzpicture environment. That lets you use the anchors of the subnodes to draw the boxes and arrows.

You could also make use of the backgrounds library to avoid having to draw the same node twice.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{
  tikzmark,
  backgrounds
}

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

\begin{document}

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

\begin{scope}[
  every node/.append style={font=\scriptsize}
]
  \draw [->] (i.south) -- ++(0,-0.7) node[below] {\constraint{condition}};
  \draw [->] (m.south) -- ++(0,-0.4) node[below] {\constraint{feature}};
  \draw [->] (p.south) -- ++(0,-0.7) node[below] {\constraint{pattern}};
\end{scope}

\end{tikzpicture}

\end{document}
  • Importing the package amsmath (which I also need in my document) breaks the proposed solution: for some unknown reason the coloured boxes do not cover any more completely the three subwords. – Nicolas Beldiceanu Sep 2 '18 at 6:02
  • @NicolasBeldiceanu Not sure exactly what happens, but remove the two $ from the ctr node. – Torbjørn T. Sep 2 '18 at 6:11
  • Yes removing the $ solves the problem. – Nicolas Beldiceanu Sep 2 '18 at 6:28

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.