27

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.

4 Answers 4

18

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.

1
  • 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. Commented Oct 4, 2010 at 13:48
20

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

5
  • 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
    Commented Jan 5, 2014 at 20:10
  • @Dror: Just remove the `\centering\usebox{\Image}` line...
    – Dror
    Commented Jan 5, 2014 at 20:48
  • Note that depth and height are different, see tex.stackexchange.com/questions/40977/… for example.
    – Tim
    Commented Feb 5, 2014 at 12:50
  • @YiannisLazarides Do you know whether a similar approach would be possible for tikz images?
    – Karlo
    Commented Oct 6, 2023 at 8:59
  • 1
    @Karlo YOu can use a similar approach, just place the tikz code in a box and measure it, although I think you will be able to also get it through TikZ as the bounding box. Why dont you ask the question and I am sure someone will answer.
    – yannisl
    Commented Oct 7, 2023 at 11:24
10

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.

3
  • I've always wanted to know how to do this. Thanks! Commented Oct 1, 2010 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
    Commented Oct 3, 2010 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! Commented Oct 4, 2010 at 13:50
4

Inspired by this answer of Martin Scharrer♦ I would say that you can use the package adjustbox in order to get these dimensions.

The key features are related to gstore and you can use the measurements to solve your problem.

Here is an example:

\documentclass{beamer}

\usepackage{adjustbox}
\newlength\myHeight % creating lenghts
\newlength\myWidth

\begin{document}

\begin{figure}
    \adjustbox{gstore width=\myWidth, gstore height=\myHeight, center}{
        \includegraphics[scale=0.5]{example-image}
    }
\end{figure}

Height: \the\myHeight % print Height

Width: \the\myWidth % print Width

\end{document}

enter image description here

2
  • Very nice work.
    – internet
    Commented Dec 10, 2022 at 13:53
  • Fantastic! Works great!
    – Tropilio
    Commented Aug 8, 2023 at 15:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .