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.
\begingroup \setbox0=\hbox{...} \message{height: \the\ht0, depth: \the\dp0, width: \the\wd0}\endgroup– plante Sep 22 '21 at 10:45