Extract one of multiple tikzpictures from a tex file

this is my first question. I searched a while but couldn't find a similar question yet, maybe I lack the correct search phrase. Anyway, here it comes.

During writing a thesis, I have a problem of inputting plots: I got calculation results listed in .txt files and I want to avoid having an own tex file for each plot. So my idea is to put all the diagrams in one file, distinguishing them by an own "tikzpicture" environment. Now what I miss is a way to tell my main document to only input lets say the third or fifth diagram from this tex file. Is there a way to do this? Maybe giving each tikzpicture something like a tag?

My aim is to a) produce fewer files b) define a format for all my diagrams and in case I want to change it, only have to do this once and not in all diagram files.

If its too complex, maybe you can suggest another way to realise this?

• As long as your files are well named, I don't really see the need to reduce the number of files. Defining the format can be done by defining the relevant TikZ styles in your preamble. I think you're needlessly complicating your problem here. – Alan Munn Apr 30 '17 at 14:36
• Hm ok. So, I guess I must include that Tikz style in the main document preamble to work, right? Makes sense, so I could define it there once and have the same effect. – Atd Apr 30 '17 at 14:45
• You could define them all as pics and turn it into a library. I wouldn't recommend it, though. – cfr May 1 '17 at 1:45

The easiest, but also a sloppy solution would be the definition of a \newcommand, for each tikzpicture that you make in your .tex-file and then inserting that into your main file.

You could have a pictures.tex file like so:

\newcommand{\tikzI}{
\begin{tikzpicture}
<insert tikz contents here>
\end{tikzpicture}
}

\newcommand{\tikzII}{
\begin{tikzpicture}
<another tikzpicture>
\end{tikzpicture}
}


While your main file may look like this:

<preamble>
\begin{document}
\input{pictures}

<some text here>
\tikzI
<more text>
\tikzII
\end{document}


However, as Alan Munn suggested, it might just be easier and more comfortable to assign every picture to its own file and simply call these files seperately for each tikzpicture. (Making a directory specifically for tikzpictures, so that they won't clutter your main directory)

• Alright, this seems to be sloppy indeed. Nevertheless, thanks for your time. I will follow Alans advice. – Atd Apr 30 '17 at 14:55
• @Atd Instead of accepting this answer right away, you might want to wait to see if other answers show up. See Should I wait before accepting answers. – Alan Munn Apr 30 '17 at 15:13

I don't think you should put all your images into a single file. It is much easier to manage multiple files in the long run.

This is an ideal use case for the standalone package and class combination. Each individual graphic can be created as a standalone document and tested by itself, but they can then be input into the master document using the standalone package. You can put your common TikZ styles into a small package. Here's an example.

Put common TikZ styles into a package

First we create a package of our TikZ styles. I've just defined one style as an example.

\ProvidesPackage{mytikzstyles}[2017/04/30]
\tikzset{my line/.style={red,very thick}}


Save this document as mytikzstyles.sty in the same folder as your document, or if you are using them for many documents, put it into your local texmf folder in <path-to-local-texmf>/tex/latex/mytikzstyles/.

Create individual standalone image documents

Next we create standalone class documents for each image. The advantage of this is that you can test and tweak them individually since they are fully compilable documents. In each document you just need to add \usepackage{mytikzstyles} to access the common styles you have created.

Image-1.tex

\documentclass[tikz=true,border=10pt]{standalone}
\usepackage{mytikzstyles}
\begin{document}
\begin{tikzpicture}
\draw[my line] (0,0)  -- (0,3)  -- (3,3) -- cycle;
\end{tikzpicture}
\end{document}


Image-2.tex

\documentclass[tikz=true,border=10pt]{standalone}
\usepackage{mytikzstyles}
\begin{document}
\begin{tikzpicture}
\draw[my line] (0,0)  -- (0,3)  -- (3,0) -- (3,3) -- cycle;
\end{tikzpicture}
\end{document}


Use the standalone class to input the files

Now our main document can input the files in any way we like. To keep your main file directory cleaner, you can put all the image documents into their own folder. (Make sure there are no spaces in the folder or filenames.)

Here's an example:

\documentclass{article}
\usepackage{standalone}
\usepackage{tikz}
\usepackage{mytikzstyles}
\begin{document}
\begin{figure}
\centering
\input{Image-2}
\caption{This is image 2}
\end{figure}
\begin{figure}
\centering
\input{Image-1}
\caption{This is image 1}
\end{figure}
\end{document}


• Thanks for your answer. I didnt know about creating own package, seems quite helpful. Thanks! – Atd Jun 6 '17 at 6:41