Posts Tagged ‘ pgfplots ’

Precompile pgfplots using tikzexternalize

Using pgfplots allows you to quickly create very beautiful plots, that look like they belong to your paper, by automatically using the correct fonts and styles. It does this by using the latex compiler to make the figures. This approach is very powerful but has a major drawback: the latex compiler was not made to do this. As a result, latex tends to run out of memory very fast, and projects with a lot of pgfplots tend to take a long time to compile.

We’ve previously shown how to increase the amount of memory latex has available, but this only solves half the problem.

A proper way to fix this is using the tikzexternalize command. It causes latex to compile the figures in a separate run, generating pdfs. These pdfs are then included in the main document, reducing both the memory requirements and the compilation time.

Using tikzexternalize is very simple, two step process

  1. Call latex with the “shell-escape” option.
    This looks like this on linux:

    pdflatex -shell-escape mydocument.tex

    The easiest way to do this is to modify the build command of your latex-editor of choice.

  2. Add the following lines of code to your latex preamble

    \usepgfplotslibrary{external} 
    \tikzexternalize[prefix=TikzPictures/]
    

    The first line loads the tikzexternalize library, the second line activates it. The second line also contains an option that specifies the folder where the resulting pdfs and intermediary files should be stored.

Latex will now externalize any tikzpicture you make. You can optionally use

\tikzsetnextfilename{name_of_resulting_pdf}

to specify a name for the resulting pdf file. This is strongly recommended, because latex will only recompile a figure if you remove the resulting pdf.

Advertisements

Extending latex memory

Latex is a very powerful typesetting application. A powerful package to be used with this is pgfplots, which allows you to render your plots using the latex engine. Unfortunately the default latex settings assume you’re running latex on an ancient 20 MHz 486 with less ram than your average mobile phone. This is ok if you’re just asking it to process text, but if you’re using pgfplots it will run out of memory trying to draw your images.

The solution is to increase the amount of memory latex uses.

Windows – Miktex
We’re doing this in the windows command line. You can get to this by opening the start menu and do run .... Type cmd to start the terminal. On vista and up, you can just type cmd in the search field.

  1. Go the folder where Miktex is installed. Most likely this is C:\Program Files\Miktex 2.7\
  2. Go to the subfolder Miktex
  3. Go to the subfolder bin
  4. run
    initexmf --edit-config-file=pdflatex
    

    Replace pdflatex with latex or xetex if you’re using that.

  5. You’ll get a notepad screen with a file called pdflatex.ini. Add the line
    main_memory=2000000
    

    Don’t worry about the exact number, just make it big.

  6. Save the file
  7. run
    initexmf --dump=pdflatex
    
  8. That’s it. Enjoy your nice pgfplots figures

Linux – Texlive
Do the following things in a terminal as root.

  1. Find out where your texmf.cnf is located
    kpsewhich texmf.cnf
    

    This will most likely be /usr/share/texmf/web2c/texmf.cnf

  2. Open it and search for main_memory line and modify it to
    main_memory=2000000
    
  3. Save the file
  4. run
    fmtutil-sys --all
    

    to load the new settings