200

Is there a TeX command that returns the width of a given text as length value, so that I can use the result directly as a length argument of another command?

I mean, I would like to have a command \getWidth{my text} and want to use the result directly as the first argument of \parbox:

\parbox{\getWidth{my text}}{my foo\\bar text}

4 Answers4

171

Use the calc package (\usepackage{calc}):

\parbox{\widthof{my text}}{...}

An “only primitives” approach would be

\newdimen\mywidth
\setbox0=\hbox{<text to measure>}
\mywidth=\wd0

and then use \mywidth.

egreg
  • 1,121,712
  • I think calc package may redefine some units. For example, I get an error using math unit mu when the package is loaded. – M.Reza Nov 22 '13 at 07:20
  • 2
    @M.Reza mu can be used only in math and only for \mkern or \mskip. – egreg Nov 22 '13 at 10:04
  • Thanks for the answer! I am trying to use this with textpos to specify width for the textblock, but it seems like the with returned by \widthof can not be taken by textblock, with error showing Missing number, treated as zero. – zyy May 01 '20 at 19:44
  • 2
    @zyy \widthof returns a suitable result only in some places, one of which is the argument to \parbox. Unfortunately textpos cannot use it, as far as I know. – egreg May 01 '20 at 19:58
  • @egreg Thanks! I will look for something else. – zyy May 01 '20 at 20:15
  • 2
    @zyy One usually does something like \settowidth{\dimen0}{<text>} and then passes \dimen0 to the macro that has to use the width. – egreg May 01 '20 at 20:26
  • 1
    @egreg I found out why, textblock takes in a relative value with no units while \widthof returns with a unit, this is explained in the documentation of textpos where it talks about troubleshooting Missing number, treated as zero. – zyy May 02 '20 at 04:09
  • 2
    @zyy Oh, yes. I tried textblock quite a long time ago; there is textpos* that accepts lengths, AFAIR. – egreg May 02 '20 at 08:26
  • He asked for a TeX command, loading the calc package is not possible in all macro packages. It does however work with LaTeX – M0M0 Jun 23 '23 at 11:11
  • 1
    @M0M0 What do you mean? The question has \parbox that looks LaTeX to me. I modified the answer, so you can retract your undeserved downvote. – egreg Jun 23 '23 at 12:08
  • @egreg Sorry for the downvote and thanks for the edit. I was looking for an answer and google kept leading me to LaTeX-only solutions... I changed my downvote to an upvote :) – M0M0 Jun 24 '23 at 00:02
163

I like to answer the question in a more general way, so that it is useful to a wider group of people.

There are the following macros which allow to store the width, height (the material above the baseline) and depth (the material below the baseline) of a given content.

\settowidth{\somelength}{<content>}
\settodepth{\somelength}{<content>}
\settoheight{\somelength}{<content>}

The calc package also provides one for the total height (height + depth):

\settototalheight{\somelength}{<content>}

as well as

\widthof{<content>}
\heightof{<content>}
\depthof{<content>}
\totalheightof{<content>}

which can be used directly inside \setlength or \addtolength.

If you need multiple dimension of the same content you can also store it in a box register and use its dimension directly (the above macros do this as well internally). These are dimension expressions and can be prefixed with a factor, e.g. .5\wd\mybox is half the width.

\newsavebox\mybox
\sbox{\mybox}{<content>}
\wd\mybox % width
\ht\mybox % height
\dp\mybox % depth

For the totalheight you need to add \ht\mybox and \dp\mybox together.

Martin Scharrer
  • 262,582
116

This can be done without the calc package

\documentclass{article}

\begin{document}
  \newlength{\myl}
  \settowidth{\myl}{test text}
  \the\myl
\end{document}

\the\myl will print out the value ~37pt.

Scz
  • 1,633
Martin H
  • 18,164
6

Here is an option with pgf

% !Mode:: "TeX:UTF-8"
\documentclass{article}
\usepackage{pgf}

\begin{document} \noindent \pgfmathwidth{"length you wanted"} \parbox{\pgfmathresult pt}{the text you want to print out} \end{document}

where \pgfmathwidth returns width of the text contained in the double quotes to \pgfmathresult, notice the result of \pgfmathwidth is always in points and only the numeric value is provided

parbox

By the way, \pgfmathwidth works for Chinese characters as well.

zyy
  • 2,146