for simple use I've written an environment wrapping the tikzpicture environment. So far no trouble. Now I realized I've got a huge amount of tikzpictures in my document and I've read once about tikz externalization, so I thought that would be a good idea. So I've got this code



% place in preamble and add "--shell-excape" to latex-run (for cluttex --shell-escape)
\tikzexternalize[prefix=figures/] % activate and define figures/ as cache folder

    \newcommand{\myCmd}[1]{\node[draw] {##1};}
    \scope[local bounding box=output]

        \myCmd{lore ipsum}

which spits out

Runaway argument?

! File ended while scanning use of \tikzexternal@laTeX@collect@until@end@tikzpi
<inserted text>
<*> main.tex


I think this ia related to 53.2 (Requirements) of the tikz manual. Is there a way for me to still use externalization but don't give up the simplicity of the environment declaration?

(on each picture I draw some nodes in the beginning, put all of the things from inside my step env in a tikz-scope and draw some nodes after the termination of the scope. Replacing all step-env occurrences with that declaration (which actually is always the same) would be ...unpleasant)

EDIT: Some way to e.g. use \begin{tikzpicture}[myTikz] which in turn will automatically put my things at the beginning/end of the tikzpicture content would suffice too, but I'm not sure if there's a way to do that with tikz.

  • 1
    Not sure if there's an easier way, but there's always the way of grab the whole environment verbatim, transform it accordingly, then output the code of a tikzpicture environment again. Are you using LuaLaTeX?
    – user202729
    Dec 10, 2021 at 10:52
  • 1
    Or easier (to program, not to type) (but may or may not be acceptable depends on your use case), redefine/override the tikzpicture environment itself.
    – user202729
    Dec 10, 2021 at 11:02
  • 1
  • 1
    Okay, good thing that tikzpicture isn't verbatim. The answer linked in in the above does answer this question (in fact this question is a subset).
    – user202729
    Dec 10, 2021 at 12:16
  • 1
    Use the method linked previously tex.stackexchange.com/questions/361738/… to grab the whole content of the environment verbatim, process it, then tex.sprint back. (not necessary in this case, but that would be the easiest one that works with verbatim or catcode-changing environments)
    – user202729
    Dec 10, 2021 at 12:29


Browse other questions tagged .