0

I defined a new command to divide a rectangle in three parts, each containing a title. However, the middle part is somehow. Can someone please explain me why this is the case?

\newcommand{\segmentation}[3]{
\begin{tikzpicture}
\node[text width=20mm, draw, align=center ] (first)  {#1};
\node[text width=22mm, align=center, right=0pt of first](second){#2};
\node[text width=35mm, align=center, right=0pt of second] (third) {#3};
\draw(second.north west) -- (second.north east) (second.south west)--(second.south east) (second.north east) -- (second.south east);
\draw(third.north west) -- (third.north east) (third.south west)--(third.south east) (third.north east) -- (third.south east);
\end{tikzpicture}
}

enter image description here

  • One solution is setting minimum height to a large enough length. Another is to put a \strut into the nodes contents – Bordaigorl Jul 21 '16 at 20:51
3

The reason is that the center node contains a "g" and a "b" thus the text occupies a greater total height than the neighbours which do not have letters with big descends.

There are two simple fixes:

1. Fix the height of your nodes

\newcommand{\segmentation}[3]{
\begin{tikzpicture}[segment/.style={align=center, minimum height=2ex}]
\node[text width=20mm, draw, segment ] (first)  {#1};
\node[text width=22mm, segment, right=0pt of first](second){#2};
\node[text width=35mm, segment, right=0pt of second] (third) {#3};
\draw(second.north west) -- (second.north east) (second.south west)--(second.south east) (second.north east) -- (second.south east);
\draw(third.north west) -- (third.north east) (third.south west)--(third.south east) (third.north east) -- (third.south east);
\end{tikzpicture}
}

2. Put a strut in your nodes

The \strut macro inserts an invisible letter taking up no horizontal space which is as tall and as deep as the highest and deepest letter in your font. Inserting it in the nodes will make them as high as needed to accommodate any text

\newcommand{\segmentation}[3]{
\begin{tikzpicture}
\node[text width=20mm, draw, align=center ] (first)  {\strut#1};
\node[text width=22mm, align=center, right=0pt of first](second){\strut#2};
\node[text width=35mm, align=center, right=0pt of second] (third) {\strut#3};
\draw(second.north west) -- (second.north east) (second.south west)--(second.south east) (second.north east) -- (second.south east);
\draw(third.north west) -- (third.north east) (third.south west)--(third.south east) (third.north east) -- (third.south east);
\end{tikzpicture}
}

3. Multipart nodes

Have a look at the manual, there is a special shape for nodes called "multipart" which does what you are simulating with your macro, but in a more general way.

| improve this answer | |

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.