1

In order to avoid misalignment issues with tikz nodes I have decided to set the options text height and text depth so that all nodes have the same height and depth as a normal line of text. According to Wikipedia, the standard height of a line of text is .7\baselineskip and the standard depth .3\baselineskip. Therefore I do

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
  \node[draw,text height=.7\baselineskip,text depth=.3\baselineskip] at (0,0) {x};

\end{tikzpicture}
\end{document}

But this means I have to remember these values.

Is there a command that would compute the height and depth of box that I can use in these circumstances? E.g. text depth=\depthof\strut.

Note:

This question is very similar to How to fix TikZ node's height with \heightof?. However the solution given there involving the calc package no longer seems to work.

Ernest A
  • 2,773
  • Package calc provides these commands: \widthof{text}, \heightof{text}, \depthof{text}, and \totalheightof{text} (sum of height and depth). Could this be what you are looking for? – Andreas Oct 12 '17 at 17:48
  • @Andreas Yes, this could be it. There's a related question here: https://tex.stackexchange.com/questions/78288/how-to-fix-tikz-nodes-height-with-heightof – Ernest A Oct 12 '17 at 18:10

2 Answers2

3

Without any packages:

\documentclass[]{article}

\begin{document}
\setbox0\hbox{foo}
\begin{tabular}[]{ll}
  width & \the\wd0\\
  depth & \the\dp0\\
  height & \the\ht0
\end{tabular}

\setbox0\hbox{goo}
\begin{tabular}[]{ll}
  width & \the\wd0\\
  depth & \the\dp0\\
  height & \the\ht0
\end{tabular}
\end{document}

Note that \setbox is not expandable. You might use \vbox if \hbox doesn't work with the contents you need, but the width doesn't work that way (ends up as \linewidth)

Skillmon
  • 60,462
  • Thanks. This seems much more intricate than the solution that uses \baselineskip though. – Ernest A Oct 12 '17 at 18:08
  • @ErnestA well I just answered the question formulated by your title without reading much of your question :) You could as well insert some \struts if all the nodes are one line -- though the answer in the possible duplicate looks like the way to go. – Skillmon Oct 12 '17 at 18:16
2

Use \strutbox as a frame of reference. Here is some reference material:

enter image description here

\documentclass{article}

\begin{document}

\fbox{\rule[-.3\baselineskip]{2em}{\baselineskip}} % Your reference
\fbox{\usebox\strutbox} % \strutbox
\fbox{\rule[-\dp\strutbox]{2em}{\dimexpr\ht\strutbox+\dp\strutbox}}% \strutbox height/depth

\end{document}

  • The first box has a depth of .3\baselineskip and a total height of \baselineskip (implying a height of .7\baselineskip), similar to what you're using.

  • The second box prints \strutbox (a zero-width box with height/depth of a \strut).

  • The third box replicates the first using \strutbox as a measure.

The above shows that you should be able to use \ht\strutbox (the height of \strutbox) for text height and \dp\strutbox (the depth of \strutbox) for text depth.

Werner
  • 603,163