[NTG-context] externalizing TikZ graphics

Henning Hraban Ramm texml at fiee.net
Mon Jan 25 20:08:57 CET 2021


> Am 25.01.2021 um 18:58 schrieb Aditya Mahajan <adityam at umich.edu>:
> 
> 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.

No, you need additional file system commands.

> Here is a simple example:

Thank you!
Meanwhile, looking at my LilyPond module, I got as far as:

"""
\startmodule[tikzfilter]

\usemodule[filter]
% docs see https://github.com/adityam/filter

\def\TIKZTEMP{tikztemp} % name of folder for TikZ/buffer files

\startbuffer[tikz::before]
\environment env_world % contains font setup etc.
\environment env_TikZ % contains TikZ settings

\startpagefigure
\starttikzpicture[thick,domain=-90:90]% TODO: make this configurable
\stopbuffer

\startbuffer[tikz::after]
\stoptikzpicture
\stoppagefigure
\stopbuffer

\define[1]\ParseTikZFile% #1 is the name of the output file
  {\ctxlua{thirddata.parsetikzfile("#1")}}

\startluacode
 thirddata = thirddata or {}

 -- create temp folder if missing
 if not lfs.isdir("\TIKZTEMP") then
   lfs.mkdir("\TIKZTEMP")
 end

 function thirddata.parsetikzfile(name)
   -- name is like \TIKZTEMP/mainfile-temp-tikz-21.pdf
   logs.report("TIKZFILTER","name='" .. name .. "'")
   context("\\externalfigure[" .. name .. "]")
 end
\stopluacode

\defineexternalfilter[tikz][
  cache=yes,
  purge=no,
  directory=\TIKZTEMP,
  bufferbefore={tikz::before},
  bufferafter={tikz::after},
  readcommand=\ParseTikZFile,
  output={\externalfilterbasefile.pdf},
% ConTeXt never writes results to other directories
  filtercommand={context --batchmode --once --autopdf=auto --result=\externalfilterbasefile\space \externalfilterinputfile\space && mv \externalfilterbasefile.pdf\space \TIKZTEMP/},
]

\stopmodule


Hraban


More information about the ntg-context mailing list