Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
2  
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
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 3 '11 at 17:11

2 Answers 2

up vote 5 down vote accepted

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

share|improve this answer
1  
+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\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
 }
share|improve this answer
    
+1 for this nice factory :-) –  xport Jun 3 '11 at 18:19
1  
It will be nicer if you put a name clash checking in the factory. –  xport Jun 3 '11 at 18:21
    
@xport please feel free to edit... :) –  Yiannis Lazarides Jun 3 '11 at 18:24
    
Disadvantage of this method: no compile-name checking :) –  xport Jun 3 '11 at 19:04
    
@xport I added a brief note as to how to incorporate. –  Yiannis Lazarides Jun 3 '11 at 19:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.