# How to get the true width (including margins) of a page for inserting images? \textwidth and \textheight are too large

When I enter the following:

text width in cm: \printinunitsof{cm}\prntlen{\textwidth}

text height in cm: \printinunitsof{cm}\prntlen{\textheight}


I see output:

text width in cm: 15.11078cm
text height in cm: 21.58693cm

However, when I create an image exactly 15cm by 21cm, and insert it into my document, it is far too large, by about the amount of the margin sizes.

I was under the impression that textwidth was supposed to be only the text box, already accounting for margins. Why is the output I see too large?

I'm looking to include some pre-sized images in my document but can't divide the page in any reasonable way without knowing how large it is. Any hints on what is going on with the output above, or how I might do this better?

EDIT, updated to include MWE

\documentclass[12pt]{report}
\usepackage{graphicx} % Required for inserting images
\usepackage{layouts}
\usepackage{lipsum}
\graphicspath{{/my/path}}
\DeclareGraphicsExtensions{.png}

\begin{document}

text width in cm: \printinunitsof{cm}\prntlen{\textwidth}

text height in cm: \printinunitsof{cm}\prntlen{\textheight}

line width in cm: \printinunitsof{cm}\prntlen{\linewidth}

column width in cm: \printinunitsof{cm}\prntlen{\columnwidth}

\lipsum{1-4}

\newpage

%\makebox[\textwidth][c]{\includegraphics{/my/path/myimg}}

\end{document}


Now, run this, you'll see this output:

text width in cm: 13.70499cm
text height in cm: 19.27483cm
line width in cm: 13.70499cm
column width in cm: 13.70499cm
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut...

Go to your favorite image program, and make a colored rectangular image corresponding to what this output says the text size is, that is 13.7cm x 19.2cm.

Uncomment the insert image above,

\makebox[\textwidth][c]{\includegraphics{/my/path/myimg}}


When you do you, and rerun it, you will see that the image extends well into the footer area. The output of \textwidth and and \textheight just isn't corresponding to the text box---these commands don't seem to account for margins, headers, footers---but I was under the impression this is exactly what the text box ought to correspond to---the printable part of the page, after you deduct all the rest.

-
Please provide a minimum working example (MWE) of the code that's giving you problems. –  Mico Sep 21 '11 at 18:11
Yeah, OK, it looks like it's one of the packages I'm using that is giving me a different value for \textwidth Not sure which is the culprit... –  Mittenchops Sep 21 '11 at 18:31
Really wish I could find this, actually. Apparently one of the packages I have in my preamble is causing the value these commands output to be too large. Fancyhdr and geometry look most likely, but unsure. –  Mittenchops Sep 21 '11 at 18:42
we've discovered that even if no dimensions are reset, the geometry package does have an effect on the settings in the ams document classes. we're telling our authors never to use it in documents submitted to ams for publication since it wreaks havoc with what we prepare for pdf-to-plate reproduction. –  barbara beeton Sep 21 '11 at 18:50
@barbarabeeton The last releases of geometry provide a pass option, that will prevent its default to be applied to the document. This is useful for exploiting its \newgeometry feature. But since AMS classes use a very different method for setting the page shape parameters, I agree that your advice is good. –  egreg Sep 21 '11 at 21:36

Create a complete example which shows that it doesn't work. Here is an example which shows that it is possible:

\documentclass{article}
\usepackage{graphicx}
\begin{document}
foo

\includegraphics[width=\textwidth,height=\textheight]{tiger}

\rule{1pt}{\textheight}bar

baz
\end{document}


The second page of my test document:

-
Hmmm, so, are you saying the solution is to specify height, in addition to width in the \includegraphics statement? Or to not surround it in an mbox? Because including an image the exact size of the output of \textwidth and \textheight still is not working, and if those are actually dimensions of the text box area, it really seems like like they should work. –  Mittenchops Sep 23 '11 at 9:23
You have to specify both, if you want to keep the aspect ration then use \includegraphics[width=\textwidth,height=\textheight,aspectratio]{tiger} –  Herbert Sep 23 '11 at 9:29
I agree that that keeps the aspect ratio, but it doesn't address my question in that when I create an external image corresponding to the output of the \textwidth and \textheight commands, it still doesn't fit---it's too big. I know that I can resize as you suggest within LaTeX, but my concern is why including an image of exactly these dimensions behaves differently than when they are specified internally, as you suggest. Thanks! –  Mittenchops Sep 24 '11 at 13:03

Since it appears that it's one of the packages you're loading that's giving you trouble, try modifying your document's preamble along the following lines (I'm assuming just by way of example that your paper dimension is "A4", but it could be anything else too, obviously):

\documentclass[a4paper,...]{article}
\usepackage[a4paper]{geometry}
\begin{document}
...
\end{document}


By passing the option "a4paper" to the geometry package explicitly, any untoward redefinitions of variables such as \paperwidth will be undone.

If you know that your paper size is, say, exactly 15.1m wide and 21.6cm high, you should of course load the geometry package as follows:

\usepackage[paperwidth=15.1cm,paperheight=21.6cm,...]{geometry}


\usepackage[papersize={15.1cm,21.6cm},...]{geometry}