# How to get (measure) the height or the width of an object?

If I want to specify the dimension of some object like rule{<width>}{<height>}, such that these dimensions are those of other objects, then how can I get (measure) the height and width of these other object and pass them to the arguments of rule{<width>}{<height>}. I'm looking for sth that takes a suitable object Object and gives one of its dimensions like h = \height{Object_1} , or like w = \height{Object_2} .

The main issue is getting the dimensions of given objects, not using the rule

• \begingroup \setbox0=\hbox{...} \message{height: \the\ht0, depth: \the\dp0, width: \the\wd0}\endgroup Sep 22, 2021 at 10:45

LaTeX provides \settowidth{<len>}{<stuff>} and \settoheight{<len>}{<stuff>} that sets <len> to either the width or the height of <stuff>. Here's an example:

\documentclass{article}

\begin{document}

\newlength{\mylen}

abc has width
\settowidth{\mylen}{abc}\the\mylen,
height
\settoheight{\mylen}{abc}\the\mylen{}
and depth
\settodepth{\mylen}{abc}\the\mylen.

def has width
\settowidth{\mylen}{def}\the\mylen,
height
\settoheight{\mylen}{def}\the\mylen{}
and depth
\settodepth{\mylen}{def}\the\mylen.

ghi has width
\settowidth{\mylen}{ghi}\the\mylen,
height
\settoheight{\mylen}{ghi}\the\mylen{}
and depth
\settodepth{\mylen}{ghi}\the\mylen.

\end{document}


You can also capture the content in a box and then pull the dimensions from there (width via \wd<box>, height via \ht<box> and depth via \dp<box>):

\documentclass{article}

\begin{document}

\newsavebox{\mybox}

\savebox{\mybox}{abc}%
abc has width \the\wd\mybox,
height \the\ht\mybox{}
and depth \the\dp\mybox.

\savebox{\mybox}{def}%
def has width \the\wd\mybox,
height \the\ht\mybox{}
and depth \the\dp\mybox.

\savebox{\mybox}{ghi}%
ghi has width \the\wd\mybox,
height \the\ht\mybox{}
and depth \the\dp\mybox.

\end{document}


\the above is just used to print the lengths. See The \the command.