'Standalone' TikZ pictures
You can use the standalone
class for this. In v0.x it used preview
internally, but for v1.x it also has an alternative crop
option, which works similar to the preview
option/package, but avoids its issues with XeTeX.
There is now (v1.0) also a tikz
class option which turns any (outer) tikzpicture
into a single tight page. This avoids issues with trailing implicit paragraphs. In addition it automatically loads the tikz
package.
% tikzpic.tex
\documentclass[crop,tikz]{standalone}% 'crop' is the default for v1.0, before it was 'preview'
%\usetikzlibrary{...}% tikz package already loaded by 'tikz' option
\begin{document}
\begin{tikzpicture}
\draw (0,0) -- (10,10); % ...
\end{tikzpicture}
\end{document}
Then compile it as usual with pdflatex
or xelatex
etc.
To include this tikzpicture
into a main document load the standalone
package there with the option mode=buildnew
. Then use \includestandalone[<options>]{<filename>}
instead of \includegraphics
. This will compile all includes standalone files automatically as graphics and build these graphics if the source file is newer than the existing graphics file. This needs -shell-escape
to be enabled to allow the main LaTeX run to call further LaTeX compilers.
See the standalone
manual for more details.
% main.tex
% compile with `pdflatex -shell-escape main` or `xelatex -shell-escape main`
\documentclass{article}
\usepackage[mode=buildnew]{standalone}% requires -shell-escape
\usepackage{tikz}
%\usetikzlibrary{...}
\begin{document}
Lorem ipsum ...
\includestandalone[width=.8\textwidth]{tikzpic}
Lorem ipsum ...
\end{document}
I ended up using the external library
, as diabonas suggested.
Here's an example with XeTeX:
\documentclass{article}
\usepackage{fontspec}
\setmainfont[Mapping=tex-text]{Linux Libertine O}
\usepackage{tikz}
\definecolor{blue}{cmyk}{1,1,0,0.07}
\usetikzlibrary{external}
\tikzexternalize
\tikzset{external/system call={xelatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource"}}
\tikzset{external/force remake=true}
\begin{document}
\tikzstyle{place}=[circle,draw=blue,fill=blue!20,line width=2pt]
\begin{tikzpicture}
\node at (0,0) [place] {Ti\textit{k}Z};
\end{tikzpicture}
\end{document}
One has to compile using -shellescape
.
A minor inconvenience is, that the appropriately cropped file is named <filename>-figure0
.
Perhaps a combination of standard article class and pdfcrop
or the cropping facilities of adjustbox
would be a work around.
So, you make the picture in a separate file with article
class, for instance. Then you use clipbox
from the adjustbox
package to clip the image included via \includegraphics
to the part you actually want...