[NTG-context] filter module / externalizing TikZ graphics

Henning Hraban Ramm texml at fiee.net
Thu Jan 28 14:51:38 CET 2021


Still working on that.
I hope you can help me here, Aditya!
I’ll add these issues to the tracker of t-filter, if it makes sense.


Testing with: ConTeXt  ver: 2021.01.24 16:07 LMTX  fmt: 2021.1.25

The setup below works, but only with \starttikz ... \stoptikz so far, I don’t like the output file names, and using the working directory is tricky.


(1) \processFILTERfile

\processtikzfile{img/example.pgf}
can’t find the file:

t-filter        > cached output file 'tikztemp/example1.pgf.pdf' missing. Rerunning filter
t-filter        > file 'tikztemp/example1.pgf.pdf' cannot be found
t-filter        > current filter : tikzThick
t-filter        > base file : example1.pgf
t-filter        > input file : img/example1.pgf
t-filter        > output file : tikztemp/example1.pgf.pdf
...
system          >   13: filename=img/example1.pgf foundname=img/example1.pgf fullname=.../img/example1.pgf usedmethod=direct

Looks like ConTeXt can find the file, but the filter module cannot.

--trackers=resolvers.schemes (or \enabletrackers) doesn’t change anything WRT log/output.

Since this fails already, I don’t know if \processFILTERfile also uses bufferbefore/-after (it should).


(2) working directory

Since ConTeXt can’t put generated (temporary) files into a directory and --result also only renames afterwards, the directory key doesn’t help, I had to add "mv" and "cd" to my filtercommand.
I didn’t check yet if caching works.


(3) file names

Using \processFILTERfile the original file name base is preserved, according to the log entry above.

If I use \starttikz[name=example], I get "JOBNAME-temp-tikzThick-example.tmp".
I’d like to get something like "COMPONENTNAME-##-example.tmp" or a path like "COMPONENTNAME/example_##.tmp".
Is there a configuration hook in the module to change name generation?


(4) subprocess parameters

I’d like to forward some parameters from \starttikz to the temp buffer, e.g. 
\starttikz[name=example][mystyle=thick]
(using the same syntax as \startsection with a second pair of brackets for custom parameters).
And then have
\starttikzpicture[\filterusersetupvariable{mystyle}]
in the buffer.

My workaround so far is a copy of the filter setup (tikz/tikzThick), since we have only a few different configurations.



Best regards, Hraban


""" % file: t-tikzfilter.tex

\startmodule[tikzfilter]

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

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


% normal setup
\startbuffer[tikz::before]
\environment env_world
\environment env_TikZ

\startTEXpage
\starttikzpicture
\stopbuffer

% setup for graphics with thicker lines
\startbuffer[tikzThick::before]
\environment env_world
\environment env_TikZ

\startTEXpage
\starttikzpicture[thick] % only difference
\stopbuffer


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


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

\startluacode
 thirddata = thirddata or {}

 local report_tikz = logs.reporter("tikzfilter")

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

 function thirddata.readpdffile(name)
   -- name is like \TIKZTEMP/mainfile-temp-tikz-21.pdf
   report_tikz("name='%s'", name) % doesn’t work
   context("\\externalfigure[" .. name .. "]")
 end
\stopluacode

\defineexternalfilter[tikz][
  cache=yes,
  % directory=\TIKZTEMP, % doesn’t work with ConTeXt
  bufferbefore={tikz::before},
  bufferafter={tikz::after},
  readcommand=\ReadPDFFile,
  output={\TIKZTEMP/\externalfilterbasefile.pdf},
  filtercommand={mv \externalfilterbasefile.* \TIKZTEMP/ && cd \TIKZTEMP\space && context --batchmode --nodummy --once \externalfilterbasefile.tmp},
]

\defineexternalfilter[tikzThick][tikz][
  bufferbefore={tikzThick::before},
]

\stopmodule
"""


""" % file: tikztest.tex

\usemodule[t-tikzfilter]
\usemodule[visual]


\starttext

\placefigure[margin,here][fig:example]{A box.}
{\vskip1in\noindent%
\starttikzThick[name=example0]
	\draw[fill=black!10] (0,4.75) rectangle (1.5,-4.75);
\stoptikzThick
}

\fakewords{150}{200}

\processtikzThickfile{tests/example1.pgf}

\stoptext
"""



More information about the ntg-context mailing list