I have a bunch of TeX code for precalculus and calculus. The pictures are in tikzpicture environment. In our project we want to create TeX file on the fly and compile three times. The compilation is slow for files with many pictures. I wonder, if it is possible to prevent evaluation of a function when we draw graph using syntax like

\draw[red,thick,samples=100,domain=-10:10] plot(\x,{.......});

I am aware on gnuplot and externalize ways, but none of them is convenient for me.

It would be nice to have a setting which draws almost the whole picture and just skips the plots. Is it possible in tikz?

All pictures are cropped to some rectangle, so I hope removing the graph of the function will not change the size of the resulting picture.

Many thanks

  • 1
    I think what you're looking for is TikZ' external library, which compiles the pictures only on the first run and then just includes the pictures on subsequent runs. – Jake Mar 10 '13 at 16:44
  • Thanks, this was one of the ways, I mentioned it in my question. But our three compilations are not too much faster since the picture conversion also take some time. And I think it is not possible in our system to compile pictures in advance, since the TeX file is created on the fly and it would be difficult and unsafe to keep track of all pictures. – robert.marik.cz Mar 10 '13 at 17:59
  • Another thing is that with the external library I sometimes observed some minor shift in the picture position and the picture sometimes hides some part of the text. – robert.marik.cz Mar 10 '13 at 18:03
  • Oh yeah, sorry, I overlooked that you'd mentioned the external library. I don't understand what you mean with "the picture conversion takes some time", though. The pictures shouldn't be converted, they're merely included, which is usually really fast, and there shouldn't be any shifts or collisions. Are you using pdflatex? Could you also maybe make a small example document that shows a typical case? – Jake Mar 10 '13 at 18:41
  • I need to enable shell-escape, so I think that some external program which converts the tikz code into PDF is used. The PDF file is user.mendelu.cz/marik/temp/000040.pdf . The first with external library compilation takes 30 sec and the others are very fast. Without externalizing one compilation takes 8 sec, this makes three compilations in 24 sec. The author of the file above draws graphs of functions using one plot operation on function with absolute values, even it was possible to draw simply using straight lines. But there are many more more complicated graphs in our project. Thanks – robert.marik.cz Mar 10 '13 at 22:28

Well, you could introduce some special command-line switches.

This could be done as follows:



\expandafter\ifx\csname OMITPLOTS\endcsname\relax

\draw (0,0) grid (2,2);

\draw[red,thick,samples=100,domain=0:1] plot (\x,{(\x)^2});


This document has some TikZpicture for which the bounding box is independent of the plot (due to the grid statement).

I added custom logic for command-line argument processing: the \expandafter\ifx.... checks if "\OMITPLOTS" is a known macro. If so, it sets "\omitplotstrue".

Inside of each picture, you would insert \ifomitplots \else <code> \fi.

If you run your compilation via standard methods, \OMITPLOTS will be undefined and everything passes as usual. But if you run it via, say

pdflatex '\def\OMITPLOTS{1} \input P'

(assuming that your file is P.tex), the \OMITPLOTS macro is set (the value is ignored) and the \ifomitplots switch will become true.

Alternatively, you could set some specific style which reduces the number of plot samples to 2.

Note that I would be interested to see any failure cases of the external lib, i.e. cases where spurious spaces or shifts have been introduced.

| improve this answer | |
  • Thanks, I tried to make a minimal example and observed, that the problem with shift has been caused by another package. My original intention was not to modify the TeX files, since there have been several authors and the TeX code is sometimes far from well-arranged. But now I tried to grep plot word in the directory with TeX files and I have seen that and all plot commands are on a single line, so I can use sed to introduce the trick with \ifomitplots. Thanks. Anyway it would be great if someone finds a solution which does not need to modify the TeX files. – robert.marik.cz Mar 10 '13 at 22:52
  • Christian, you are the author of the external library? It is great tool, I finally decided to use this library. Now I build pdf files with pictures on fast computer and then move to my PC. I had to solve some problems (remove pagecolor from the pictures and insert enumerate-like counter to the name of the file with pictures) and everything works for me fine. The shifts have been introduced by another package. Thank you very much. – robert.marik.cz Mar 12 '13 at 6:29
  • @robert.marik.cz you're welcome. Just in case you identified the incompatibility with some other package clearly: could you create some minimal example and send it to me by email? I would try to solve the problem such that others do not suffer the same problems. You can find my address in pgfplots.sourceforge.net/pgfplots_unstable.pdf – Christian Feuersänger Mar 12 '13 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.