[Tex/LaTex] Saving tikz figures in external file

includeinputtikz-pgf

I have several tikz figures, some of which are over 400 lines of code. This complicates my working on the main file since I have to scroll past all that code quite often so I was thinking I put it in an external file which I can then simply include in my main document. Now I don't know how to proceed, I see two ways:

  • Putting every picture in a separate file, compile as PDF, include the PDF figures
  • Putting every picture in a separate file, include the files, compile the main document

Which is the best way to do this? And how exactly should I go about it? For example in option two, I don't know what code is needed in the sub-files: should I start defining a \documentclass and proceed like it was a standalone document or can I just put the code I'm "cutting" out of the main document?

What should I do then? include? input?

MWE

\documentclass{scrreprt}
\usepackage{tikz}
\usepackage{lipsum}
\tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt        

\begin{document}
\chapter{Foo}
\lipsum[1]

\begin{figure}[h!]
    \centering
    \begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]

    %Shape: Rectangle [id:dp6874714272670446] 
    \draw   (600.5,167) -- (608.5,167) -- (608.5,181.5) -- (600.5,181.5) -- cycle ;
    %Shape: Rectangle [id:dp25216565311879857] 
    \draw   (569,127) -- (639,127) -- (639,167) -- (569,167) -- cycle ;
    %Shape: Rectangle [id:dp10652138678304235] 
    \draw   (593.5,181.5) -- (614.5,181.5) -- (614.5,188.5) -- (593.5,188.5) -- cycle ;
    %Shape: Rectangle [id:dp27982392636707765] 
    \draw   (572.5,130.71) -- (635.5,130.71) -- (635.5,163.29) -- (572.5,163.29) -- cycle ;
    %Shape: Rectangle [id:dp29031818189807423] 
    \draw   (572.75,193.2) -- (637.25,193.2) -- (637.25,212.43) -- (572.75,212.43) -- cycle ;
    %Shape: Rectangle [id:dp6960940497473749] 
    \draw   (577.35,196.4) -- (581,196.4) -- (581,198.41) -- (577.35,198.41) -- cycle ;
    %Shape: Rectangle [id:dp012575111125552318] 
    \draw   (583.35,196.4) -- (587,196.4) -- (587,198.41) -- (583.35,198.41) -- cycle ;
    %Shape: Rectangle [id:dp7462575500863122] 
    \draw   (589.35,196.4) -- (593,196.4) -- (593,198.41) -- (589.35,198.41) -- cycle ;
    %Shape: Rectangle [id:dp74377024282381] 
    \draw   (595.35,196.4) -- (599,196.4) -- (599,198.41) -- (595.35,198.41) -- cycle ;
    %Shape: Rectangle [id:dp5681813434227054] 
    \draw   (577.35,200.41) -- (581,200.41) -- (581,202.41) -- (577.35,202.41) -- cycle ;
    %Shape: Rectangle [id:dp45670728197953436] 
    \draw   (583.35,200.41) -- (587,200.41) -- (587,202.41) -- (583.35,202.41) -- cycle ;
    %Shape: Rectangle [id:dp4491046180017293] 
    \draw   (589.35,200.41) -- (593,200.41) -- (593,202.41) -- (589.35,202.41) -- cycle ;
    %Shape: Rectangle [id:dp6731288033777918] 
    \draw   (595.35,200.41) -- (599,200.41) -- (599,202.41) -- (595.35,202.41) -- cycle ;
    %Shape: Rectangle [id:dp7160498396948172] 
    \draw   (601.35,196.4) -- (605,196.4) -- (605,198.41) -- (601.35,198.41) -- cycle ;
    %Shape: Rectangle [id:dp13325729640208062] 
    \draw   (607.35,196.4) -- (611,196.4) -- (611,198.41) -- (607.35,198.41) -- cycle ;
    %Shape: Rectangle [id:dp2394144125721973] 
    \draw   (601.35,200.41) -- (605,200.41) -- (605,202.41) -- (601.35,202.41) -- cycle ;
    %Shape: Rectangle [id:dp19114186601978278] 
    \draw   (607.35,200.41) -- (611,200.41) -- (611,202.41) -- (607.35,202.41) -- cycle ;
    %Shape: Rectangle [id:dp6393250197036928] 
    \draw   (616.35,196.4) -- (620,196.4) -- (620,198.41) -- (616.35,198.41) -- cycle ;
    %Shape: Rectangle [id:dp5875707462557691] 
    \draw   (616.35,200.41) -- (620,200.41) -- (620,202.41) -- (616.35,202.41) -- cycle ;
    %Shape: Rectangle [id:dp2632343129417658] 
    \draw   (622.35,196.4) -- (626,196.4) -- (626,198.41) -- (622.35,198.41) -- cycle ;
    %Shape: Rectangle [id:dp16732048982102876] 
    \draw   (628.35,196.4) -- (632,196.4) -- (632,198.41) -- (628.35,198.41) -- cycle ;
    %Shape: Rectangle [id:dp994069766161821] 
    \draw   (622.35,200.41) -- (626,200.41) -- (626,202.41) -- (622.35,202.41) -- cycle ;
    %Shape: Rectangle [id:dp22742437505582824] 
    \draw   (628.35,200.41) -- (632,200.41) -- (632,202.41) -- (628.35,202.41) -- cycle ;
    %Shape: Rectangle [id:dp7394772484697936] 
    \draw   (577.35,204.42) -- (581,204.42) -- (581,206.42) -- (577.35,206.42) -- cycle ;
    %Shape: Rectangle [id:dp22090387903101294] 
    \draw   (583.35,204.42) -- (587,204.42) -- (587,206.42) -- (583.35,206.42) -- cycle ;
    %Shape: Rectangle [id:dp3094625226147294] 
    \draw   (589.35,204.42) -- (593,204.42) -- (593,206.42) -- (589.35,206.42) -- cycle ;
    %Shape: Rectangle [id:dp2693883419330956] 
    \draw   (595.35,204.42) -- (599,204.42) -- (599,206.42) -- (595.35,206.42) -- cycle ;
    %Shape: Rectangle [id:dp879541864109227] 
    \draw   (577.35,208.42) -- (581,208.42) -- (581,210.43) -- (577.35,210.43) -- cycle ;
    %Shape: Rectangle [id:dp005070405348506002] 
    \draw   (583,208.42) -- (599.35,208.42) -- (599.35,210.43) -- (583,210.43) -- cycle ;
    %Shape: Rectangle [id:dp7109131109721036] 
    \draw   (601.35,204.42) -- (605,204.42) -- (605,206.42) -- (601.35,206.42) -- cycle ;
    %Shape: Rectangle [id:dp34805202583561967] 
    \draw   (607.35,204.42) -- (611,204.42) -- (611,206.42) -- (607.35,206.42) -- cycle ;
    %Shape: Rectangle [id:dp88037563000339] 
    \draw   (601.35,208.42) -- (605,208.42) -- (605,210.43) -- (601.35,210.43) -- cycle ;
    %Shape: Rectangle [id:dp199400132263712] 
    \draw   (607.35,208.42) -- (611,208.42) -- (611,210.43) -- (607.35,210.43) -- cycle ;
    %Shape: Rectangle [id:dp4342588107618768] 
    \draw   (616.35,208.42) -- (620,208.42) -- (620,210.43) -- (616.35,210.43) -- cycle ;
    %Shape: Rectangle [id:dp9519101603214133] 
    \draw   (622.35,204.42) -- (626,204.42) -- (626,206.42) -- (622.35,206.42) -- cycle ;
    %Shape: Rectangle [id:dp4542360869287658] 
    \draw   (622.35,208.42) -- (626,208.42) -- (626,210.43) -- (622.35,210.43) -- cycle ;
    %Shape: Rectangle [id:dp5417437553938629] 
    \draw   (628.35,208.42) -- (632,208.42) -- (632,210.43) -- (628.35,210.43) -- cycle ;

    \end{tikzpicture}
\caption{This is just a part of the total picture.}
\end{figure}
\lipsum[2]
\end{document}

Best Answer

Simple answer, you can do both. However, in a very closer inspection, each has its own advantages and disadvantages.

Method 1

%&pdflatex
% !TeX TXS-program:compile = txs:///pdflatex/[--shell-escape]
\documentclass{scrreprt}
\usepackage{tikz}
\usepackage{lipsum}
\tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt        
\usetikzlibrary{external}
\tikzexternalize[prefix=tikzexfig/]
\usepackage{standalone}
\begin{document}
\chapter{Foo}
\lipsum[1]
\begin{figure*}[h]
    \centering{
        \input{ex.tex}
        \caption{This is just a part of the total picture.}
        \label{f1}
    }
\end{figure*}
\lipsum[2]
\end{document}

Note: tikzexfig is a folder within your master directory.

Update 1: You must escape the shell to compile the document.

ex.tex

 \begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
    %Shape: Rectangle [id:dp6874714272670446] 
    \draw   (600.5,167) -- (608.5,167) -- (608.5,181.5) -- (600.5,181.5) -- cycle ;
    \end{tikzpicture}

Advantages:

  1. Easier in-line compilation due to smart caching of figures in pdf.

Disadvantages:

  1. When using for publications, you most certainly have to use standalone package to compile them separately.

  2. Memory limit would be reached (pdftex/LaTeX).

  3. When editing, you must delete the already generated pdf's manually. (I am not yet aware of a better alternative to automate it, yet). I had a good discussion with @DavidCarlisle on putting a wrapper around it, as similar to psfrag. However, the result was a terrible failure ;-).

  4. First compilation with all the figues will take a hell-a-lot of time. Subsequent compilations are much faster.

Method 2

I am not going to discuss it, as it is the standard standalone package implementation.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
 \begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
    %Shape: Rectangle [id:dp6874714272670446] 
    \draw   (600.5,167) -- (608.5,167) -- (608.5,181.5) -- (600.5,181.5) -- cycle ;
    \end{tikzpicture}
\end{document}

Advantages:

  1. Easier file-like externalisation.

  2. Gives your output in different formats. For more info, visit the package documentation.

  3. We can take the output and compile in shorter time.

Disadvantages:

  1. Different sub-files. Atleast, I am not yet aware to include it as in Method 1.

Disclaimer 1: I remember seeing somewhere that tikzexternal and standalone should not be used together. But I don't remember why and not sure of its impacts.

Disclaimer 2: With pdftex/LaTeX, memory limit is a common problem for both the methods per se.