Including TikZ Pictures

In the following, I describe how I include TikZ pictures in my LaTeX documents. I decided to put every figure into a separate file, which I name e.g. "foo.tikz". The advantages of this approach are:

Actually, I do not only use \input to read the external files, but I am using the following code in the LaTeX document preamble:

\newif\iffinal % introduce a switch for draft vs. final document
%\finaltrue % use this to compile the final document

% [more preamble that in my case also uses \iffinal for other stuff]

\usepackage{tikz}
\pgfrealjobname{thesis} % <-- NOTE: this needs to be the real document's basename
                        %     (else you'll only get an empty output file)

\iffinal
  \newcommand{\inputTikZ}[1]{%
    \input{#1.tikz}%
  }
\else
  \newcommand{\inputTikZ}[1]{%
    \beginpgfgraphicnamed{#1-external}%
    \input{#1.tikz}%
    \endpgfgraphicnamed%
  }
\fi

Then, within the document I can just use the newly defined command to include the figure:

\begin{figure}
\begin{centering}
% like \input{...}, but adds caching via PGF's externalization feature:
\inputTikZ{Figures/house-example/house}
\end{centering}
\caption{\label{fig:house-example}An example drawing showing a house.}
\end{figure}

This means that I can use pdflatex --jobname foo-external thesis.tex (or Figures/house-example/house-external in the above example) to compile the included picture foo.tikz into foo-external.pdf, which is then automatically included for a faster compilation of thesis.tex. (See section "Externalizing Graphics" in the PGF/TikZ manual, i.e. section 60 at the time of writing.)

Here is an example foo.tikz file:

\begin{tikzpicture}[>=stealth]

\draw[->] (-4,0) -- (4.5,0);
\draw[->] (0,-1) -- (0,4.5);

\foreach \ang in {0,30,...,150}
  \foreach \scale in {1,2,4,8} {
    \draw[red,rotate=\ang,scale=\scale]
    (0.375,0) ellipse (0.125 and 0.0945);
  }

\end{tikzpicture}

As you can see, I added the tikzpicture environment within the .tikz file; one disadvantage of this approach is that one cannot easily scale the picture if it should have different sizes in different documents.

tikz2pdf

File: tikz2pdf(4843 bytes; Fri, Jan/16/2009) download 'tikz2pdf' to disk

For a quick preview of changes I make to my figures, I wrote this little python script, which creates a temporary .tex file that includes the .tikz file and compiles that to create a .pdf file. Even better, it stays running and watches the .tikz file for changes; as soon as the file is changed in an editor, the .pdf is recreated.

Its main features currently are:

Future Work

The following limitations are to be solved:


Valid XHTML 1.0! Valid CSS!
This page was last modified: Thursday, September 03, 2009 hacker emblem