# [NTG-context] externalizing TikZ graphics

Mon Jan 25 18:58:20 CET 2021

```On Mon, 25 Jan 2021, Henning Hraban Ramm wrote:

>
> > Am 25.01.2021 um 17:31 schrieb Hans Hagen <j.hagen at xs4all.nl>:
> >
> >> If nobody has better advice, I’ll try to come up with a filter setup. That would mean we’d avoid the tikz module, every TikZ diagram will get written to a buffer of configurable name, and that (with preamble setup) will get run through LaTeX. I read pgf had some restrictions in plain TeX mode, but perhaps we can also use LuaTeX in plain mode to avoid the LaTeX dependency.
> > In the following case the buffer only gets typeset when it changes:
>
> Thank you (and Henri)! But we’d need to include the same setup header every time, so the filter module is probably really the best way.

It is not only the tikz setup; you also need to copy the font setup etc.

\typesetbuffer[setups,figure]

or some variant of that should also work. But the filter module does make it easier to store the results in another directory so that they are out of sight.

> There’s just a problem with labels, I guess, if I go the filter-LaTeX or filter-TeX route. So filter-ConTeXt might make sense.
> I think I understand now why integrating MetaPost made a lot of sense.

Here is a simple example:

\usemodule[filter]

\startbuffer[tikz-before]
\usemodule[tikz]
\startTEXpage
\starttikzpicture
\stopbuffer

\startbuffer[tikz-after]
\stoptikzpicture
\stopTEXpage
\stopbuffer

\defineexternalfilter
[externaltikz]
[
bufferbefore=tikz-before,
bufferafter=tikz-after,
cache=yes,
filtercommand={context --batchmode \externalfilterinputfile\space --purgeall --result=\externalfilteroutputfile},
output={\externalfilterbasefile.pdf},
]