[Tex/LaTex] get the width (in pt) of a box

boxestikz-pgf

I would get the width (so a dimension) of a string in a box. I need to know the width of \hphantom{some text}, in order to use it into a tikz picture, for example as \draw (0,0)--(\mylen,0); that represents a line with the same width of the string some text.

Best Answer

If you only have regular text in the box then TikZ can measure it too, which is what you are trying to do manually anyways. It just uses its own temporary box to measure. But \textbf etc. stuff is not so easy to use. So use it with caution.

\begin{tikzpicture}
\pgfmathsetmacro\mylen{width("some text")}
\node[anchor=west,inner sep=0] {some text};
\draw[red,thick] (0,0) -- (\mylen pt,0);
\end{tikzpicture}

Oh and height works too.

enter image description here

Related Question