# Determine text width

For sizing figures in LaTeX it's helpful to know how wide the text is so that figures can be sized to match without rescaling. LaTeX provides \textwidth for reference by other commands, e.g.
\includegraphics[width=\textwidth]{figure}
but including \textwidth "raw" in the document produces errors.

Is there a way to determine what this value is?

-

## migrated from stackoverflow.comDec 26 '11 at 22:57

This question came from our site for professional and enthusiast programmers.

See also Force stringification of a length. – Martin Scharrer Dec 26 '11 at 23:04

Using \the\textwidth will allow you to see the value LaTeX is using. However, the default unit of measurement is probably points, not a likely option for your graphics software.

Fortunately, the conversion function is available in the standard layouts package: \usepackage{layouts}
...
textwidth in cm: \printinunitsof{cm}\prntlen{\textwidth}
will print the value in cm. All the usual LaTeX units of measurement are also available if cm aren't what you want.

-
textwidth in inches: \printinunitsof{in}\prntlen{\textwidth} – Sean Oct 14 '13 at 11:10
For the article class, this gives the result 4.7747 inches. – Andrew Cashner Sep 21 '15 at 15:48

The printlen package:

\documentclass{article}
\usepackage{printlen}
\begin{document}
\printlength\textwidth
\end{document}


produces "345.0pt". It can print in units other than pt, too.

-
printlen isn't included by default in teTeX which makes this solution a bit harder, but I'm impressed by the quick response! – blahdiblah Apr 2 '09 at 1:27
Just use \the, it's built into TeX itself. – Josh Lee Apr 2 '09 at 1:28
teTeX is unsupported for a couple of years now. You should be using TeXLive instead. – Will Robertson Apr 2 '09 at 2:34
Thanks for the heads up about teTeX. Once TeXLive makes it into fink's repository I'll gladly move to it. – blahdiblah Apr 3 '09 at 23:15

I encountered the same challenge. However, I did not manage to use the abovementioned options. My LaTeX distribution did not recognized \printinunitsof as a valid statement.

What worked perfectly fine for me was to take a screen shot of the PDF and measure the width of the text and the width of the document in pixels using a programme like Paint. As the total document width in cm is known, you can calculate:

textwidth_cm = documentwidth_cm*textwidth_pixels/document_pixels


The textwidth is also equal to the documentwidth minus the left and right margins. If you explicitly specify the margins (e.g. with the geometry package), the textwidth is also easily calculated.

-