# How to manage pictures in a single file to be easily picked by the main file?

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?

-
Surely the best option is to keep them as separate files? Why do you feel you have too many files? – Seamus Jun 3 '11 at 16:35
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 3 '11 at 17:11

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. :-)

-
+1 for a lesson in effective simplicity – Brent.Longborough Jun 3 '11 at 21:19
I like both answers, but this is the one I decided to use - that's why I'm accepting it. – user8268 Jun 4 '11 at 10:27
@user8268: Thank you for accepting my answer. :-) – xport Jun 4 '11 at 10:29
One typo detected: "reservered" should be written as "reserved". – xport Jun 4 '11 at 11:22

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\CommandFactory#1#2{%
\expandafter\def\csname#1\endcsname{#2}
}

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

\CommandFactory{foo}{\tikzpicture
\axis[stack plots=y]
{(0,1) (1,1) (2,2) (5,2)};
{(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}