47

I would like to create a custom vector drawing in Inkscape and then use it many times in many tikz pictures.

Is it possible?
Should I use PDF or SVG or other output from Inkscape?
How to use external vector image in Tikz node?
How to define anchors / dimensions(bounding box)?

lockstep
  • 250,273
Łukasz Lew
  • 8,543
  • 12
  • 43
  • 42
  • 2
    Some of your questions are answered in http://tex.stackexchange.com/questions/2099/how-to-include-svg-diagrams-in-latex – Caramdir Aug 21 '10 at 13:30

1 Answers1

66

PGF tools for working with external images:

  • use \pgfdeclareimage to make your external image known to pgf/TikZ

  • use \pgfuseimage{name} to include the image into your document

  • encapsulate the image by \pgfbox to be able to use it within a node

An advantage of this way is that each image will be included just once in a pdf document and may be reused several times, thus saving space.

Follow general recommendations about including pictures: prefer vector format like pdf.

Code example:

This small example builds a tree with nodes using external images. I've simple taken images from our site for this.

\documentclass{article}
\usepackage{tikz}
\pgfdeclareimage{gold}{gold}
\pgfdeclareimage{silver}{silver}
\pgfdeclareimage{bronze}{bronze}
\begin{document}
\begin{tikzpicture}[sibling distance=12em]
  \node {\pgfbox[center,bottom]{\pgfuseimage{gold}}}
    child {node {\pgfbox[center,top]{\pgfuseimage{silver}}}}
    child {node {\pgfbox[center,top]{\pgfuseimage{bronze}}}};
\end{tikzpicture}
\end{document}

Output:

tree with images as nodes

Further explanation of the pgf commands:

  • \pgfdeclareimage[options]{image name}{file name} declares an image without printing it. It understands key=value options like height, width, page, interpolate and mask. The usual formats are supported and their extensions would automatically be tried.

  • \pgfuseimage{image name} simply outputs that image.

  • \pgfbox[horizontal alignment,vertical alignment]{content} outputs the content aligned left, center or right respectively top, center, bottom or base.

Detailed syntax, options and examples could be found in the pgf user guide.

The normal LaTeX approach:

You could simply use \includegraphics, use it within \pgftext. The example modified in this sense:

\documentclass{article}
\usepackage{tikz}
\usepackage{graphicx}
\begin{document}
\begin{tikzpicture}[sibling distance=12em]
  \node[anchor=south] {\pgftext{\includegraphics{gold}}}
    child {node[anchor=north] {\pgftext{\includegraphics{silver}}}}
    child {node[anchor=north] {\pgftext{\includegraphics{bronze}}}};
\end{tikzpicture}
\end{document}

Choosing the right way:

Using \includegraphics instead of pgf commands is recommended here because \includegraphics is a better implementation. The pgf way may be preferred if the document size matters and graphics are multiply used or if masking is desired.

Stefan Kottwitz
  • 231,401
  • How do you know about pgfdecareimage? It is not to be found in pgfmanual.pdf. Can you answer in http://tex.stackexchange.com/questions/1900/undocumented-useful-commands-in-tikz ? – Łukasz Lew Aug 21 '10 at 14:30
  • 1
    Can I include PDF or SVG? – Łukasz Lew Aug 21 '10 at 14:32
  • 1
    As far as I know PDF, JPEG, PNG, EPS and PS but not SVG. – Stefan Kottwitz Aug 21 '10 at 14:40
  • 4
    \pgfdeclareimage is mentioned in the beamer manual: as Till Tantau wrote both pgf and beamer I guess he just assumed it was covered. I've added an answer to the other question about this. – Joseph Wright Aug 21 '10 at 15:11
  • The community here is amazing. You make this site a gold mine. Thanks to all. – Łukasz Lew Aug 21 '10 at 15:32
  • Could you provide an example where including the same image multiple times give a significantly larger output if done with graphicx and \includegraphics compared to pgf and \pgf{declare,use}image? If I do a simple experiment with a \loop \repeat the resulting pdf-file is slightly smaller if I use \includegraphics. My small test was done with pdflatex-1.40.10. – Martin Heller Aug 21 '10 at 16:45
  • Perhaps pdf compression removes that difference. I suggest this issue could be discussed in a separate question, thus further user could test and report. – Stefan Kottwitz Aug 21 '10 at 18:06
  • 1
    I have posted a question about this: http://tex.stackexchange.com/questions/2163/resulting-document-size-when-including-a-graphics-file-multiple-times-pgf-vs-g – Martin Heller Aug 21 '10 at 21:15
  • @Stefan: The image above was accidently lost by the provider. See http://meta.tex.stackexchange.com/questions/1409/the-image-you-are-requesting-does-not-exist-or-is-no-longer-available/1411#1411 – Display Name Jun 16 '11 at 12:26
  • @Stefan: Maybe it's worth re-producing the image? – Hendrik Vogt Jun 23 '11 at 15:09
  • @HendrikVogt: thank you and xport, I re-uploaded the image. – Stefan Kottwitz Nov 19 '11 at 14:46
  • According to Till Tantau the only advantage of \pgfdeclareimage over \includegraphics is the masking; apparently \includegraphics is even slightly better wrt. duplicate elimination: 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, currently only pgf offers the ability to include images that are partly transparent. [beameruserguide.pdf, p132] – Daniel Nov 19 '11 at 15:19
  • @Daniel In pgf manual p647, there is also a rough argument about why and how he thinks it is better. – percusse Nov 19 '11 at 15:26
  • egreg, could you please explain why one have to use \pgfbox/\pgftext I’m using \includegraphics directly as node content and it works fine, I guess … – Tobi Nov 19 '11 at 15:30
  • Maybe it's a good idea if you swap the order of the approaches in your answer. \includegraphics is almost always preferred, and definitely easier to use. Having the answer in the current order has the potential to confuse people who are just looking for a straightforward way to include an image in TikZ. – Jake Jun 27 '13 at 13:18