Take the 2-minute tour ×
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.

Suppose I have a figure in my LaTeX document:

\begin{figure}
  \includegraphics[scale=0.5]{some_graphic}
\end{figure}

My question is pretty simple: Is there a way to get the size that some_graphic will be rendered to in points/inches/whatever unit into a variable for use in the rest of the document? Here's a slightly more reasonable motivating example from beamer:

\begin{frame}
  \begin{block} \begin{block}<2->{Title}
    Text on the first part.
    \only<2>{
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \vspace{2.19in}  % Would be nice not to have to tweak this value manually
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    }
    \only<3>{
    \begin{figure}\centering
      \includegraphics[width=0.60\textwidth,clip,trim=0in 2.1in 0in 0in]{figures/scheme}
    \end{figure}
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \vspace{0.775in} % Would be nice not to have to tweak this value manually
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    }   
    \only<4>{
    \begin{figure}\centering
      \includegraphics[width=0.60\textwidth]{figures/scheme_new}
    \end{figure}
    }
  \end{block}
\end{frame}

In other words, I realize that there is a way around this problem manually, by tweaking the vspace values, but that's not very reasonable, and it seems to go against the whole idea behind TeX of letting the rendering engine do the work.

share|improve this question

3 Answers 3

up vote 13 down vote accepted

The literal method for doing this looks something like as follows:

\usepackage{calc}
...
\def\mygraphic{\includegraphics{...}}

\newlength\graphicheight
\setlength\graphicheight{\heightof{\mygraphic}}
% OR: \settoheight\graphicheight{\mygraphic}
...
\mygraphic % to insert the graphic
...
\vspace{\graphicheight} % whitespace same size as the graphic

But there is also the \phantom command which creates an empty box of the same size as its contents, which might suit your specific circumstances better:

\mygraphic % insert the graphic
...
\phantom{\mygraphic} % insert a blank box of the same size

Additionally, \includegraphics takes a draft option to replace individual images by placeholders, so

\def\mygraphic\{includegraphics[draft]{...}}

might perform better than including the actual image.

share|improve this answer
    
Thanks, Will. An excellent answer, as always. I've been wanting to know how to do this ever since I first used the \includegraphics command. –  David Hollman Oct 4 '10 at 13:48

This is a solution that I have recently developed to measure the dimensions of an image

\documentclass[11pt]{article} 
\usepackage{graphicx}

\begin{document}
\newlength{\imageh}
\newlength{\imaged}
\newlength{\imagew}

\newcommand{\setimageh}[1]{
 \settoheight{\imageh}{\usebox{#1}}
}

\newcommand{\setimagew}[1]{
 \settowidth{\imagew}{\usebox{#1}}
}

\newcommand{\setimaged}[1]{
 \settodepth{\imaged}{\usebox{#1}}
}


\newcommand{\ImageDimensions}[1]{
   % create and save the box
  \newsavebox{\Image}
  % Set graphics to fixed size for this example
  \savebox{\Image}{\includegraphics[width=80pt, height=120pt]{#1}}
  \centering\usebox{\Image}\
  \setimageh{\Image}
  \setimagew{\Image}
  \setimaged{\Image}
  \footnotesize
     {\vskip7pt
      The height of the image (#1) is : \the\imageh\\
     The width of the  image (#1) is : \the\imagew\\
     The depth of the  image (#1) is : \the\imaged\\}
}

\ImageDimensions{./DFS8U.png}
\end{document}

It renders as shown below. There are three dimensions shown (width, height and depth)

Example render of measuring picture dimensions

share|improve this answer
    
Is there a way to save the information into dimensions variables without inserting the picture at all? Not as a phantom, or anything like that. –  Dror Jan 5 at 20:10
    
@Dror: Just remove the `\centering\usebox{\Image}` line... –  Dror Jan 5 at 20:48
    
Note that depth and height are different, see tex.stackexchange.com/questions/40977/… for example. –  Tim Feb 5 at 12:50

If this is for beamer, an alternative solution might be to look into the \begin{overlayarea} command which should allow you to replace one figure with another while keeping the text outside the overlayarea unmoved. See page 79 et seq. of the beamer user guide.

share|improve this answer
    
I've always wanted to know how to do this. Thanks! –  Will Robertson Oct 1 '10 at 16:26
    
overlayarea is not especially user friendly, but for simple stuff, like what the OP seems to want, it's fairly good. –  Seamus Oct 3 '10 at 15:26
    
In this specific case, your answer probably works better, even if it isn't the answer to the general question. Thanks for the help! –  David Hollman Oct 4 '10 at 13:50

Your Answer

 
discard

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.