I have a number of (long) documents from which I want to extract all of the tikzpicture environments into a separate file.
The extract package seems the right place to start, but there's a problem: extract finds instances of tikzpicture environments not just in the tex file, but (of course) in anything that gets expanded.
Example document
The following code has a tikzpicture in it, but also a tikzcd environment (which invokes a tikzpicture).
\documentclass{article} \usepackage[active,generate=testoutput,extract-env={tikzpicture}]{extract} \usepackage{tikz,tikz-cd} \usepackage{amsmath} \begin{document} \begin{tikzpicture} \draw (0,0) rectangle (1,1); \end{tikzpicture} \[ \begin{tikzcd}[sep=huge] Z & n,m \arrow{r}{+ \text{ in }Z} \arrow[swap]{d}{\sigma} & n+m \arrow{d}{\sigma} \\ K & n^*,m^* \arrow[swap]{r}{+ \text{ in }K} & (n+m)^* = n^*+m^* \end{tikzcd} \] \end{document} This does not compile cleanly with the extract line uncommented, giving all sorts of issues with the tikzcd environment. The output file created looks like this:
\documentclass{article} \begin{document} \begin{tikzpicture} \draw (0,0) rectangle (1,1); \end{tikzpicture} \begin{tikzpicture}[/tikz/commutative diagrams/.cd,every diagram,sep=huge]\ifx \arrow \tikzcd@arrow \pgfutil@packageerror {tikz-cd}{Diagrams cannot be nested}{}\fi \let \arrow \tikzcd@arrow \let \ar \tikzcd@arrow \def \rar {\tikzcd@xar {r}}\def \lar {\tikzcd@xar {l}}\def \dar {\tikzcd@xar {d}}\def \uar {\tikzcd@xar {u}}\def \urar {\tikzcd@xar {ur}}\def \ular {\tikzcd@xar {ul}}\def \drar {\tikzcd@xar {dr}}\def \dlar {\tikzcd@xar {dl}}\global \let \tikzcd@savedpaths \pgfutil@empty \matrix [/tikz/matrix of \iftikzcd@mathmode math \fi nodes,/tikz/every cell/.append code={\tikzcdset {every cell}},/tikz/commutative diagrams/.cd,every matrix]\bgroup Z & n,m \arrow{r}{+ \text{ in }Z} \arrow[swap]{d}{\sigma} & n+m \arrow{d}{\sigma} \\ K & n^*,m^* \arrow[swap]{r}{+ \text{ in }K} & (n+m)^* = n^*+m^* \end{tikzpicture} If we modify the code in the original tex file so that the extract package is called using the line
\usepackage[active,generate=testoutput,extract-env={tikzpicture,tikzcd}]{extract} it doesn't work, but in a slightly different (albeit related) way.
The following does, of course, work:
\usepackage[active,generate=testoutput,extract-env={tikzcd}]{extract} ...but that doesn't extract normal tikzpictures.
If it were just the tikzcd environment that is causing trouble, I could almost certainly just replace it with a more vanilla tikz figure.
Unfortunately, the class file I am using has a number of predefined environments which use the tikzpicture environment to draw pretty boxes around things (theorems, exercises, that sort of thing), and these are also causing trouble.
The documentation for the extract package has some detail about conditionals, etc, but there seems little way to get round this (other than revert to writing a python script, which I'm loathe to do). Any suggestions?
tikzpictureenvironments. To have the tikzpictures appear in the order they appear in the PDF, a python script would need do more than just pull out thetikzpictureenvironments.