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.

lockstep
  • 250,273

4 Answers4

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

Tim
  • 530
yannisl
  • 117,160
  • 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 05 '14 at 20:10
  • @Dror: Just remove the \centering\usebox{\Image}\ line... – Dror Jan 05 '14 at 20:48
  • Note that depth and height are different, see https://tex.stackexchange.com/questions/40977/confused-with-tex-terminology-height-depth-width for example. – Tim Feb 05 '14 at 12:50
  • @YiannisLazarides Do you know whether a similar approach would be possible for tikz images? – Karlo Oct 06 '23 at 08: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 Oct 07 '23 at 11:24
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.

Christian
  • 19,238
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.

Seamus
  • 73,242
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

Leone
  • 576