[Tex/LaTex] How to control the exact dimension of a picture in LaTeX

graphicslengths

This question is pretty simple: I'd like to have the possibility to get the exact size of a picture in LaTeX with, for example, the macro \the.

In fact, I've founded the way \settoheight{} and so on, but it didn't work at all. What I've got doesn't match with what I can see in the image properties (I suppose that's the name in English). The result: 8pt versus 1300 pixels. It's may be a mis-use…

Edit: In the end, my aim was to find a way to fit the picture in LaTeX. If width and height were both less than the dimensions of the body, then it should just be centred. It look like this :


Require:

\usepackage{graphicx}
\usepackage{xifthen}
\usepackage{calc}

Code (pretty ugly, I agree):

% Centrering+fitting for image, with captation and label (not auto)
\newlength\imageheight
\newlength\imagewidth
\newcommand{\pic}[3]{%
\settoheight\imageheight{\includegraphics{pictures/#1.png}}%
\settowidth\imagewidth{\includegraphics{pictures/#1.png}}%
\ifthenelse{\lengthtest{\imagewidth > \textwidth}}{\ifthenelse{\lengthtest{\imageheight > \imagewidth}}{\par\vspace{0.95em}%
\begin{figure}
    \centering\includegraphics[height=0.85\textheight]{pictures/#1.png}
    \caption{#2}
    \label{#3}
\end{figure}%
\par\vspace{0.95em}}{\par\vspace{0.95em}%
\begin{figure}
    \centering\includegraphics[width=0.95\textwidth]{pictures/#1.png}
    \caption{#2}
    \label{#3}
\end{figure}%
\par\vspace{0.95em}}}{\ifthenelse{\lengthtest{\imageheight > \textheight}}{\par\vspace{0.95em}%
\begin{figure}
    \centering\includegraphics[height=0.85\textheight]{pictures/#1.png}
    \caption{#2}
    \label{#3}
\end{figure}%
\par\vspace{0.95em}}{\par\vspace{0.95em}%
\begin{figure}
    \centering\includegraphics{pictures/#1.png}
    \caption{#2}
    \label{#3}
\end{figure}%
\par\vspace{0.95em}}}}
%

To use it : put your png files in a "pictures" directory, then, just simply call the picture like this : \pic{name_whitout_extension}{Captation_if_needed}{Label_if_needed}.


Edit 2 :

Thanks to Martin Scharrer's easyfig package, I've got a more or less 1,6 times faster solution to put my pictures, centred, with the perfect size.

CTAN: http://www.ctan.org/pkg/easyfig

VC: https://bitbucket.org/martin_scharrer/easyfig

Version v1.2 – 2012/05/15


This could be useful for some others, I hope.

Best Answer

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}
Related Question