How do I control the exact dimension of a picture in LaTeX?
If all you want is to limit the width of your image to some maximum value (like \linewidth
), you could simply use the approach described in the TeX FAQ:
\documentclass[a5paper]{article}
\usepackage{graphicx}
\makeatletter
\def\maxwidth{%
\ifdim\Gin@nat@width>\linewidth
\linewidth
\else
\Gin@nat@width
\fi
}
\makeatother
\begin{document}
\centering\includegraphics[width=\maxwidth]{largebottle}
\includegraphics[width=\maxwidth]{largesidebottle}
\end{document}
To actually measure and output the dimensions, \settoheight
and \settowidth
should work. You can convert the pt
units to other units using the printlen
package, as Werner pointed out in the comments.
ImageMagick (identify -verbose bottle.jpg
) shows the geometry to be 1408x714 pixels, with a resolution of 300x300 pixels per inch, which translates to 1408 px/300 px per in*72.27 pt per in=339.1872 pt
and 714 px/300 px per in * 72.27 pt per in = 172.0026 pt
.
\documentclass[11pt ]{article}
\usepackage{graphicx}
\usepackage{printlen}
\uselengthunit{cm}
\newlength\imageheight
\newlength\imagewidth
\begin{document}
\settoheight\imageheight{\includegraphics{bottle}}
\settowidth\imagewidth{\includegraphics{bottle}}
\includegraphics{bottle}
Height: \the\imageheight\ (\printlength{\imageheight})
Width: \the\imagewidth\ (\printlength{\imagewidth})
\end{document}
LaTeX doesn't use pixels. Pixels aren't a print format. pdflatex
allows a px
unit but this is simply converted to points (pt
) like everything else in TeX. The pixel-to-point ratio depends on the image density which is in dots-per-inch (DPI). By default 72 DPI are used. A TeX point is 1/72.27 inches and a PostScript/PDF point (bp
in TeX) is 1/72 inches. If your image has a large DPI value but pdflatex
can't read that from the images meta-data this conversion might be wrong (because 72 DPI are assumed) and your image is sized incorrectly. Sometimes simply converting your image or opening and saving it fixes the meta-data.
About how to measure all dimensions of the image:
Don't include the image multiple times to measure its different sizes. Store it manually in a box (e.g. \mybox
) and then you can access the box dimensions directly using \ht\mybox
(height), \wd\mybox
(width) and \dp\mybox
(depth). All the \setto...
macros you have mentioned do exactly the same with a temp box.
\newsavebox\mybox
\newcommand{\pic}[1]{%
\sbox\mybox{\includegraphics{#1}}%
Height: \the\ht\mybox
Width: \the\wd\mybox
Depth: \the\dp\mybox % For images usually 0 except if rotated
\ifdim\wd\mybox>.6\textwidth
% width is larger than 60% of the text width ...
\fi
% Insert the image:
\usebox\mybox
}
You can also need the \@tempboxa
box register (requires \makeatletter
.. \makeatother
) or directly the box register number 0
: \sbox0{...} \ht0 ...
Although the question asks how to do this in LaTeX, I am also adding an answer to show how to do this using a high level interface in ConTeXt.
Whenever you use \externalfigure
, ConTeXt makes the dimensions (and number of pages for pdf images) available using the macros \figurenaturalwidth
, \figurenaturalheight
, and \noffigurepages
. If you don't want the image to be included in the document but just want to measure its dimensions, use \getfiguredimensions
. The following example gets the dimensions for a remote hosted image:
\starttext
\getfiguredimensions[http://placekitten.com/500/300][method=png]
\startTABLE
\NC Width \NC \the\dimexpr\figurenaturalwidth \NC \NR
\NC Height \NC \the\dimexpr\figurenaturalheight \NC \NR
\NC Pages \NC \noffigurepages \NC \NR
\stopTABLE
\stoptext
\figurenaturalwidth
and ...height
return the units in sp
. I use\the\dimexpr
to convert the result to points. One could also use the Lua function number.todimen(...)
for a more versative conversion routine.
NOTE As in LaTeX, the dimension calculations use the default DPI specified in pdftex/luatex. So, Jake's comment of comparing px
and pt
is still relevant.
Based on the edited question, in ConTeXt you can use:
\externalfigure[figurename][maxwidth=\textwidth, maxheight=\textheight]
This will shrink the image so that its height and width do not exceed maxheight
and maxwidth
. If you need to set these options for many figures, you can use:
\defineexternalfigure[MaxWidthAndHeight][maxwidth=\textwidth, maxheight=\textheight]
\externalfigure[figurename][MaxWidthAndHeight][frame=on]
(where I used frame=on
only to show how to add other options, if needed).