12

I'm creating some graphs for my thesis in a single .tex file using the TikZ package in LaTeX.
I'm using the standalone document class so that my graphs are generated without whitespaces.

How can I export every graph in a separate PDF file so I can load them individually in another project?

pchaigno
  • 11,313
  • 2
  • 29
  • 54
Kostas
  • 270
  • 1
  • 3
  • 10

3 Answers3

14

The tikz library external does just what you want:

\usetikzlibrary{external}
\tikzexternalize

It will generate one PDF per tikzpicture. You can define an output directory with:

\tikzexternalize[prefix=./dir/]

(Without / at the end it simply appends "dir" to the beginning of the output filenames)

pchaigno
  • 11,313
  • 2
  • 29
  • 54
  • 1
    Note that you may need to add the `-shell-escape` flag when compiling. E.g. `pdflatex -shell-escape main.tex`. – drumath Jun 01 '22 at 09:08
2

When you compile generate1.tex file, it will produce generate1.pdf file. When you rename the .tex file (f.e. generate2.tex), new pdf will also have different name (generate2.pdf), thus the old pdf won't be deleted.

If you want to display TikZ images only, use \PreviewEnvironment{tikzpicture}. If you are interested, you can find the sample code here.

If you want to load them, use includepdf.

njsg
  • 125
  • 8
mrek
  • 1,655
  • 1
  • 21
  • 34
  • Instead of renaming the .tex file for every image, you could use the `-jobname=...` option when compiling. You can include the pdf files just like other images - no need for the `includepdf` package unless you want them as fullpaper images. – samcarter_is_at_topanswers.xyz Mar 15 '23 at 20:12
0

How can I export every graph in a separate PDF file so I can load them individually in another project?

You don't need to have them as separate PDF files, you can use the page=... to select the page with the image you would like to include:

\documentclass{article}

\usepackage{graphicx}

\begin{document}

\begin{figure}[htbp]
\centering
\includegraphics[page=42]{example-image-duck}
\caption{cute duck}
\end{figure}

\begin{figure}[htbp]
\centering
\includegraphics[page=99]{example-image-duck}
\caption{even cuter}
\end{figure}

\end{document}