2

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?

2
  • why not python? I'd use perl or emacs lisp but python should be fine (and better than tex at editing files) Commented Jan 31, 2020 at 15:44
  • There are a few subtleties that make python tricky (not least I'd have to learn python first!). Another complexity is that there are bits of the TeX document (solutions to exercises) that get written to a file, then read back in at the end, and these also contain tikzpicture environments. To have the tikzpictures appear in the order they appear in the PDF, a python script would need do more than just pull out the tikzpicture environments. Commented Jan 31, 2020 at 16:25

2 Answers 2

3

If you just want to extract some pictures, leaving aside all the auxiliary tikzpicture environments, you can use a different name for those you want to extract.

\documentclass{article} \usepackage[active,generate=testoutput,extract-env={xtikzpicture}]{extract} \usepackage{tikz,tikz-cd} \usepackage{amsmath} \newenvironment{xtikzpicture}{\begin{tikzpicture}}{\end{tikzpicture}} \begin{document} \begin{xtikzpicture} \draw (0,0) rectangle (1,1); \end{xtikzpicture} \[ \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} 

The testoutput.tex file will be

%% %% This is file, `testoutput.tex', %% generated with the extract package. %% %% Generated on : 2020/01/31,17:03 %% From source : extik.tex %% Using options: active,generate=testoutput,extract-env={xtikzpicture} %% \documentclass{article} \begin{document} \begin{xtikzpicture} \draw (0,0) rectangle (1,1); \end{xtikzpicture} \end{document} 

You'll need to edit the generated file anyway, in order to make it usable.

2
  • This might be the best solution, thanks. It is super-simple, if not totally automatic (but then nothing is ever going to be truly automatic). Commented Jan 31, 2020 at 16:46
  • @rbrignall I think that extract changes the working of \tikzpicture and \endtikzpicture and obviously this affects any macro that internally calls them. Commented Jan 31, 2020 at 16:54
2

An alternative response, the ltximg script automates this process, create standalone files (and images) for all tikzpicture.

The ltximg script you can do the following:

$ ltximg --subenv --imgdir=mypics --prefix=tkz -o test-out test.tex 

Now you will have a new document test-out.tex with the tikzpicture environments converted into images (.pdf) and you will also have each tikzpicture environment in standalone files and one file test-all.tex with (only) all tikzpicture.

If your input file is test.tex:

\documentclass{article} \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} 

The test-out.tex file will look like this:

\documentclass{article} \usepackage{tikz,tikz-cd} \usepackage{amsmath} \graphicspath{{mypics/}} \usepackage{grfext} \PrependGraphicsExtensions*{.pdf} \begin{document} \includegraphics[scale=1]{test-tkz-1} \[ \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} 

And the standalone file test-tkz-1.tex, located in ./mypics, will look like this:

\documentclass{article} \usepackage{tikz,tikz-cd} \usepackage{amsmath} \pagestyle{empty} \begin{document} \begin{tikzpicture} \draw (0,0) rectangle (1,1); \end{tikzpicture} \end{document} 

If you only need to create the standalone files you can use:

$ ltximg --subenv --imgdir=mypics --prefix=tkz --norun test.tex 

First check the documentation to see the options and conditions that the input file must meet.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.