20

I can use \widthof{text} to fix a minimum width of a TikZ's node. I would like to use \heightof{text} to fix its minimum height but it doesn't work.

I just want to fill my nodes with one word but I want to draw some empty nodes with same height as filled ones. But I would like not to use a trial and error system to fix it's size.

Next code shows what I want to do. I've tried uncommenting \usepackage{calc} but nothing changed. .log file doesn't show any error about knowing or not command \heightof.

A possible solution would be to use \vphantom{text} inside empty nodes, but I would like to know what's wrong with this one.

\documentclass[tikz,border=1mm]{standalone}
%\usepackage{calc}
\usetikzlibrary{positioning}

\tikzset{
box/.style={draw,%
    minimum width=5mm,%
    minimum height=\heightof{Cap},%
    align=center},
}

\begin{document}
\begin{tikzpicture}[font=\small\sffamily]

\node[box] (h0) {Cap};
\foreach \i [remember=\i as \lastx (initially 0)] in {1,...,6} 
    \node[box,right= 0mm of h\lastx] (h\i) {};
\node[box,right=0mm of h\lastx] {cua};

\end{tikzpicture}
\end{document}

This is the result. You can see that minimum height is not fixed with \heightof. Why?

enter image description here

4
  • 1
    Don't use minimum height (or add \depthof to \heightof). Use text depth and text height. For example: text height=1em,text depth=.2em. Oct 19, 2012 at 9:52
  • @PaulGaborit: OK, with text height=\heightof{Cap} and text depth=\depthof{Cap} I obtain equal height nodes, but why minimum height=\heightof{Cap} doesn't work? As you can see in my result empty nodes are shorter than C.
    – Ignasi
    Oct 19, 2012 at 10:38
  • @PaulGaborit: Tom answered my question, If I fix minimum heigth I must include inner sep in it for empty nodes.
    – Ignasi
    Oct 19, 2012 at 11:08
  • Actually, it seems to already do this, see my edit. Oct 19, 2012 at 15:54

1 Answer 1

21

The minimum height ist the total height, not taking into account the inner sep of the nodes. Setting the outer sep to zero will align them nicely:

Code

\documentclass[tikz,border=1mm]{standalone}
\usetikzlibrary{positioning}

\pgfmathsetmacro{\myinnersep}{2}% inner sep in mm

\tikzset{
box/.style={draw,%
        inner sep=\myinnersep,%
        outer sep=0,%
    minimum width=5mm,%
    minimum height=\heightof{Cap}+2*\myinnersep*1mm,%
    align=center}
}

\begin{document}
\begin{tikzpicture}[font=\small\sffamily]

\node[box] (h0) {Cap};
\foreach \i [remember=\i as \lastx (initially 0)] in {1,...,6} 
    \node[box,right= 0mm of h\lastx] (h\i) {};
\node[box,right=0mm of h\lastx] {cua};

\end{tikzpicture}
\end{document}

Output

enter image description here

5
  • Understood! Do you know how to consider font size? Something like \heightof{\Large{Cap}}.
    – Ignasi
    Oct 19, 2012 at 11:12
  • No, it doesn't consider fontsize. It works for you because you change \myinnersep. Try with minimum height=\heigthof{Cap}+\depthof{Cap} and \myinnersep=0, it should draw a 'tight' Cap box and it does only for normalsize font.
    – Ignasi
    Oct 19, 2012 at 17:38
  • Nice solution. Can it be done generically? I.e. without having manually provide the contents of the largest node to the minimum height? But rather that it equalises the sizes of the boxes based on the largest node? Apr 15, 2014 at 13:51
  • @FrederickNord: New questions made within comments have difficult visibility, it's better that you make it as a new question with link to this answer.
    – Ignasi
    Apr 16, 2014 at 7:40
  • 4
    heightof is not longer working with TikZ, the new command is height("Cap")
    – maetra
    Feb 10, 2016 at 10:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .