TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}
share|improve this question
up vote 90 down vote accepted

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

\parbox{\widthof{my text}}{...}
share|improve this answer
Thanks. That's exactly what I looked for. – Stefan Endrullis May 19 '11 at 12:05
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 7:20
@M.Reza mu can be used only in math and only for \mkern or \mskip. – egreg Nov 22 '13 at 10:04

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.


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


as well as


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.

\wd\mybox % width
\ht\mybox % height
\dp\mybox % depth

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

share|improve this answer
How do you automate the addition of these two values together? – kando Jun 27 at 19:31
@kando: You can use additions inside \setlength when you load the calc package. – Martin Scharrer yesterday

This can be done without the calc package


  \settowidth{\myl}{test text}

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

share|improve this answer
\settowidth is actually part of standard LaTeX. Comment out the calc line and you'll see it still works. – Matthew Leingang May 19 '11 at 9:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.