10

I'm writing a paper with many TikZ pictures. To keep the main file readable, I keep the pictures as separate files, but unfortunately I have now too many files. What is a nice solution to keep all the pictures in one file (separate from the main file), giving them names, and then calling them from the main file?

Display Name
  • 46,933
user8268
  • 259
  • 2
    Surely the best option is to keep them as separate files? Why do you feel you have too many files? – Seamus Jun 03 '11 at 16:35
  • 1
    The filecontents environment lets you put several files into one, but it still extracts all of them into separate files when running LaTeX. – Caramdir Jun 03 '11 at 17:11

2 Answers2

8

Store them in macros and then input the file(s).

In the minimal below, I have provided for a \CommandFactory macro to automate the process. You can also store all the names in a list and use a loop to list all of them (can be useful when drafting your document).

   \documentclass{article}
    \usepackage{pgfplots}
    \begin{document}
    \makeatletter
    \gdef\alist{}
    \def\addtolist#1#2{\g@addto@macro{#1}{#2,}}
    \def\CommandFactory#1#2{%
      \expandafter\def\csname#1\endcsname{#2}
      \addtolist{\alist}{#1}
    }

    \CommandFactory{bar}{\tikzpicture
        \axis[stack plots=y]
        \addplot coordinates
           {(0,1) (1,1) (2,2) (3,2)};
        \endaxis
    \endtikzpicture}

    \CommandFactory{foo}{\tikzpicture
        \axis[stack plots=y]
        \addplot coordinates
          {(0,1) (1,1) (2,2) (5,2)};
        \addplot coordinates
          {(0,1) (1,1) (2,2) (3,2)};
        \endaxis
    \endtikzpicture}
    %loop through all the records

    \foo
    \bar

    \@for \i:=\alist \do{%
      \@nameuse{\i}
    }
    \makeatother
    \end{document}

Following xport's suggestion you can trap duplicate name errors, by replacing the \CommandFactory with:

\def\CommandFactory#1#2{%
      \ifcsname#1\endcsname 
          \PackageError{}{Duplicate plot name in 
             \protect\CommandFactory}{Take a break!}
      \else
        \expandafter\def\csname#1\endcsname{#2}
        \addtolist{\alist}{#1}
      \fi
 }
yannisl
  • 117,160
5

Create your own package in the same folder in which the main file exists, namely mypictures.sty. Define a unique command for each picture as follows:

% ========================================================
% Copyright (c) 2011 xport. All rights reservered.
% LPPL LaTeX Public Project License 
% ========================================================    
\NeedsTeXFormat{LaTeX2e}[1994/06/01] 
\ProvidesPackage{mypictures}[2011/06/04 v0.01 LaTeX package for my own purpose]  
\RequirePackage{pstricks}

\newcommand{\myGrid}{%
\begin{figure}
\centering%
\begin{pspicture}[showgrid=true](-2,-2)(2,2)
\end{pspicture}
\end{figure}}

\newcommand{\myLine}{%
\begin{figure}
\centering%
\begin{pspicture}[showgrid=true](-2,-2)(2,2)
\psline(2,2)
\end{pspicture}
\end{figure}}

\endinput 
% mypictures.sty

In the preamble, put \usepackage{mypictures} and now you are ready to call the pictures from the main file as follows:

\documentclass{article}
\usepackage{mypictures}
\begin{document}
\myGrid

\myLine
\end{document}

Advantage: You will get a compile-name checking to guarantee each picture has a unique name. :-)

Display Name
  • 46,933