47

I am in the process of writing my master thesis and i am using Tikz to generate few graphs. Unfortunately, the input file for the graphs are not so small, so i already faced the problem that space limitation (solved). Nevertheless, it will take a long time to generate those plots.

My question is whether it is possible to save the generated Tikz figures into a file and to load the file instead? Since the problem is always that the labels do not have the right font size…

PS.

I am using scrbook and pdflatex, I would have no problem to change to something else if it will solve my problem.

Stefan Pinnow
  • 29,535
Eagle
  • 2,559

3 Answers3

34

You can do this with the TikZ library external. It saves each picture in an external file, which has a proper graphics format. From now on TikZ will read the external file unless you tell it to regenerate the external file.

There are several methods that regenerate the external graphics files. For example, by setting the TikZ key external/force remake at the start of your document, you tell TikZ to regenerate subsequent external pictures. You may set the key as follows: tikzset{external/force remake}. Other methods, including methods that use Makefiles, are explained in Section 32.4.3 of the pgf manual.

The following is shamelessly copied from the TikZ manual.

Note: You have to run LaTeX with shell-escape enabled.

\documentclass{article}
% main document, called main.tex
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize % activate!
\begin{document}
\begin{tikzpicture}
\node {root}
child {node {left}}
child {node {right}
child {node {child}}
child {node {child}}
};
\end{tikzpicture}
A simple image is \tikz \fill (0,0) circle(5pt);.
\end{document}
LondonRob
  • 4,422
  • 2
    This is not quite true. Even if the picture has changed, you have to ask for the figure to be remade. TikZ will happily assume that anything with the right file name is up to date unless you do, for example by using \tikzset{external/force remake}. – qubyte Feb 14 '12 at 10:26
  • @MarkS.Everitt Thanks. I'll change the text. –  Feb 14 '12 at 10:45
  • by setting tikzset{external/force remake}, will it force it to generate the pdf files each time? or only in case of a change? – Eagle Feb 14 '12 at 11:39
  • 1
    @Eagle It will do it for every tikzpicture in the current group. By putting the picture in a group and enforcing external/force remake at the start of the group, you can restrict the scope of the regeneration to the pictures inside the group. –  Feb 14 '12 at 11:53
  • @Eagle I should have added after the \tikzset{external/force remake} at the end of the first sentence of my previous comment. –  Feb 14 '12 at 12:03
  • 1
    ... but it will do it on each compilation run when that option is set. In a document with lots of pictures, I use the external/remake next key just before the one I'm currently working on and then delete it when I'm happy. Note that if you don't specify names for your pictures then they are named numerically and so if you insert a new picture not at the end, you'll have to regenerate the lot. – Andrew Stacey Feb 14 '12 at 12:03
  • @AndrewStacey Yes it will do it each compilation run. You can also restrict the scope of the remaking to a group, which may be nicer. –  Feb 14 '12 at 12:05
  • 1
    I like to keep a commented out force remake with every TikZ picture. Whenever I need to regenerate one I uncomment it. – qubyte Feb 14 '12 at 14:59
  • @Mark A better idea may avoiding relying on commenting. I haven't implemented this yet, but it's the next thing I'll do. I'll define a global mechanism that can turn all external picture generation on. The mechanism depends on a draft/final mode. In draft mode, generation is turned off, unless you explicitly say so. In final mode, all pictures will be generated. Switches for the draft mode may be defined to turn generation on or off. Since I use my own class files, this requires little configuration in the top-level document. –  Feb 14 '12 at 15:12
  • @MarcvanDongen: If you want to regenerate them all then just comment/uncomment \tikzset{external/force remake} in the preamble. – qubyte Feb 14 '12 at 15:37
  • 1
    @MarcvanDongen: Now what would make my day is a routine that runs a checksum of the code in a TikZ picture and compares it with the previous run (may need two compilations). Then it could automatically rebuild when a change is made. – qubyte Feb 14 '12 at 15:39
  • @MarkS.Everitt Using comments makes it impossible to drive the compilation by an external command. With my approach it is possible, which is convenient. –  Feb 14 '12 at 15:46
  • @MarkS.Everitt Checksums aren't completely reliable. I don't know the exact possibilities, but I'd write the body of each tikzpicture to a separate external file, compare them with the previous version and only regenerate the external picture files if the bodies differ. Of course I'd also update the save tikzpicture bodies. –  Feb 14 '12 at 15:49
  • Warning: when tikzpictures are given by macros external it is useless :-( From the TikZ manual Sec. 33.2 page 399: "It CAN'T expand macros during this step, so the only requirement is that every picture's end is directly reachable from its beginning, without further macro expansion." – loved.by.Jesus Oct 26 '16 at 11:30
  • Related practical question here. – Karlo Jan 05 '21 at 17:01
  • Is it possible to generate the figure-pdf as cropped instead of a complete single page? – alper Jan 08 '23 at 13:57
6

If you are on Windows or Linux you can use QtikZ, a dedicated TikZ editor that offers live compilation of your TikZ code and export to PDF and other formats.

You can also consider the standalone package to generate PDF compiling your TikZ code alone with pdflatex.

Finally, TikZ offers the external library to generate PDFs during the first compilation of your main file (chapter 32 of the manual).

qubyte
  • 17,299
andrMollo
  • 433
1

You can try a semi graphical tikz editor Tikzedt which I feel has many features like creating pdf file and standalone files.