externalize only some pictures

The global externalization setting can be temporary disabled/enabled (until the end of the TeX group) using \tikzexternaldisable and \tikzexternalenable. Another option is to set the /tikz/external/export={true/false} key; the variant /tikz/external/export next={true/false} applies only for a single picture.

All these are described in the chapter "Externalization Library" of the TikZ userguide.


  1. put this at the begin of your preamble

    \usepackage{etoolbox}
    \providetoggle{externalize}
    \settoggle{externalize}{true}
    
  2. put this at the end of your preamble, and most important, after \makeindex

    \newcommand{\notexternal}{%
      \iftoggle{externalize}{\tikzset{external/export next=false}}{}
    }
    
    \iftoggle{externalize}{\tikzexternalize[prefix=figures-compiled/]}{}
    
  3. put \notexternal before tikz environment/command you don't want to be externalized

In this way:

  • you have a short command to disable externalization for a single picture
  • you can disable externalization for the whole document by changing \settoggle{externalize}{true} to \settoggle{externalize}{true}, without having to change anything else

EDIT: Alternative version

Externalized pictures are named incrementally, and if you add/remove/swap pictures in your document you have to recompile them all, unless you specify the name for each picture, which can be done with this code:

  1. unchanged

  2. new code:

    \newcommand{\externalize}[1][]{%
      \iftoggle{externalize}{%
        \ifstrequal{#1}{false}{%
          \tikzset{external/export next=false}%
        }{%
          \ifstrempty{#1}{%
            \ifcurrfiledir{./figures/}{%
              \tikzsetnextfilename{\currfilebase}
            }{}
          }{%
            \tikzsetnextfilename{#1}
          }
        }
      }{}
    }
    
    \iftoggle{externalize}{\tikzexternalize[prefix=figures-compiled/]}{}
    
  3. three alternatives:

    • put \externalize[false] before tikz environment/command you don't want to be externalized
    • put \externalize[figure_a] if you want the external picture to be named figure_a.pdf
    • if the picture is included in your document with \include{./figures/figure_1.tex}, you can put \externalize with no argument if you want the external picture to be named figure_1.pdf (if the file is not in the figures folder, it will be named incrementally)
    • don't put anything if you want the figure to be externalized and named incrementally