17

Is there a way to use same figure multiple times -- without reinserting it? That is I don't want to pay it's weigth several times in the output pdf file.

I remember I saw someone telling adjustbox can do it, but I can't find it in its manual.

  • 2
    AFAIK the graphicx package does this for you. Just include it with \includegraphics as many times as you need, it will be just once in the PDF. – Daniel Oct 18 '13 at 8:06
  • @Daniel: That's cool. Probably You right. – Adobe Oct 18 '13 at 8:12
  • @Daniel: This is not the case for dvips + ps2pdf. The xsavebox package can be used here to include the external PS only once . See: tex.stackexchange.com/a/296122 . – AlexG Mar 2 '16 at 9:49
  • @AlexG: Good catch, thanks! I have updated my answer. – Daniel Mar 2 '16 at 15:37
18

The \includegraphics command offered by the graphics and graphicx packages already does this. Technically, the included image is placed only once in the resulting PDF and each additional instance just is a reference to the already embedded object.

Interestingly, this topic is not mentioned in the package documentation of these packages. (At least I was just not able to find it there). I learned it from the manual of the beamer class (§13 Graphics, page. 130), where Till Tantau writes about PGFs graphics commands:

Like \pgfdeclareimage, \includegraphics also includes an image only once in a .pdf file, even if it used several times (as a matter of fact, the graphics package is even a bit smarter about this than pgf)

However, ultimately this image folding feature depends on the graphics/graphicx backend driver, which is determined by the employed TeX/LaTeX engine: While it works out of the box with pdftex, xetex and luatex, images are included multiple times if going the dvips+ps2pdf route. For this case, user AlexG has suggested a workaround in another answer.

  • isn't there a spelling error? \includegraphicx vs. \includegraphics? – daleif Oct 18 '13 at 8:36
  • I have tried using the the dvips route with the famous tiger.eps. Unfortunately, it seems that the PDF file size scales linearly with the number of inclusions of tiger. Even with ps2pdf -dMaxInlineImageSize=0. – AlexG Oct 18 '13 at 8:56
  • @AlexG I tried with pdflatex, sice it struck my curiosity. Document with no image is 63 kB (\includepackage{lipsum}), with one figure environment it's 3 pages and 170 kB, with two environments it's 3 pages and 170 kB, with 4:4pg/170kB, 5:5pg/171kB, 6:5pg/171kB, 7:6pg/171kB, 8:6pg/172kB, 9:7pg/172kB, 10:7pg/172kB. So the PDF size scales, but not really linear with the number of included (same) images :) – Habi Oct 18 '13 at 9:17
  • @Habi: Well, I forgot to mention that the slope is approx. 1 in the case of dvips + ps2pdf. – AlexG Oct 18 '13 at 9:24
  • @AlexG you mentioned that the "file size scales linearly", which is slope = 1 in my understanding :) – Habi Oct 18 '13 at 9:35
2

With pdfLaTeX, LuaLaTeX and XeLaTeX, repeat \inludegraphics{...} as often as desired. For these engines, the graphicx package already makes sure that a graphics file is included only once, and that references are inserted at other places.

For latex ->dvips->ps2pdf this is not done automatically and the final PDF will contain multiple instances of the same included file. This can be avoided by means of the xsavebox package:

\usepackage{graphicx}
\usepackage{xsavebox} %include content only once
...
\xsavebox{img for frequent use}{\includegraphics{example}}%
\xusebox{img for frequent use}
...
\xusebox{img for frequent use}

This method can still be applied to the other engines listed above, because it may reduce compilation time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.