My Tex files get very confusing since I have multiple long TikZ code in them. How do you organize your files when it comes to TikZ?
4 Answers
Like with any TeX code you can put your TikZ picture environment into own .tex
files and load them in your document with \input{<filename>}
(never with \include
). Because I faced similar issues like you while writing my thesis I wrote standalone
class and package which allows you to add a full preamble to these files and compile them on their own while still be able to \input
them into a main document without any changes.
The exact way I organize my TikZ pictures is as follows:
- One .tex file per TikZ picture.
- The files include a full preamble which loads all packages and libraries required and use the
standalone
class. - The files can be in a subdirectory, e.g.
figures
. - In the main document I load the
standalone
package and\input
the TikZ pictures where I want to have them, e.g. inside afigure
environment. - The newer versions of
standlone
also provide a\includestandalone[<options>]{<filename>}
macro which can be used instead of\input
but allows you to use the same options like for\includegraphics
, e.g. resize and turn the content, etc. - It is also possible to compile all the TikZ files to single PDFs and include these in the document. This speeds up the compilation process significantly (except the first one, of course).
- For existing documents you can also just copy every TikZ picture to a single
.tex
file without preamble and simply\input
that one. However,standalone
gives you the great benefit of being able to compile the TikZ picture on its own. This is a huge time safer while coding the picture.
Example
Tikz picture:
% Tikz File 'mytikz.tex'
\documentclass{standalone}
\usepackage{tikz}
%\usetikzlibrary{...}
\begin{document}
\begin{tikzpicture}
\draw (0,0) -- (1,1);
%%%
\end{tikzpicture}
\end{document}
Main document:
% Main document
\documentclass{book}
% Repeat required preamble stuff or use `subpreambles` option of the `standalone` package
\usepackage{tikz}
%\usetikzlibrary{...}
\usepackage{standalone}
\begin{document}
Text ...
\begin{figure}
\includestandalone[width=\textwidth]{mytikz}% without .tex extension
% or use \input{mytikz}
\caption{My TikZ picture}
\label{fig:tikz:my}
\end{figure}
\end{document}
See the standalone
manual for more. There are a couple of more options and features.
-
3I like the
subfiles
package, which works likestandalone
, but has the added benefit of custom commands set with\newcommand
in the main file's preamble still available in the other files. Actually, as preambles in the subfiles are simply\documentclass[example.tex]{subfiles}
, the whole preamble of the main file is compiled when the single subfile is compiled. A drawback is that all subfiles have to be in the same directory as the main file. Commented Oct 29, 2012 at 19:17 -
6So glad that you got the rep for this :) Thanks for a great package, and all you do for the community :)– cmhughesCommented Oct 29, 2012 at 19:53
-
2@gerrit: You don't need to scale the picture when you are using
\includestandalone
. It is also good for other things, like thebuild
option, which can automatically build and include TikZ files. Also, you mean\includegraphics
not\includepdf
, right? The latter is only required for full page PDFs. Commented Oct 29, 2012 at 22:03 -
1Is it correct that
\includestandalone[width=\textwidth]
first compiles the graphic and then scales it? Because it seems that text is also rescaled. Is it somehow possible to reverse the order of rendering and scaling, such that the font size matches that of the document? (Not sure if I should ask this as a separate question...)– JostCommented Sep 24, 2013 at 16:04 -
1@FrederickNord: Yes, use the
subpreambles
package option. See the manual ofstandalone
for further details. Commented Jun 21, 2016 at 18:46
I 'll try to describe the approach I used for my master thesis. Since I had a lot of tikz figures, compilation time was significant. In order to reduce it I decided to compile them to standalone pdfs and use \includegraphics{}
(Martin's answer already mentioned that - I am just elaborating).
In order to do that I followed the following scheme:
$ tree
.
├── chap1
│ ├── chap1.tex
│ └── figures
│ ├── figure1.tikz
│ └── figure2.tikz
├── chap2
│ ├── chap2.tex
│ └── figures
│ ├── figure1.tikz
│ └── figure2.tikz
├── main.tex
└── tikz_preamble.tex
So I created a folder for each chapter containing a tex file and a subfolder containing the tikz files. In my main.tex
i used the import
package and I used the command \subimport{chap1/}{chap1.tex}
to include the tex files of each chapter. In the tex file of each chapter I was just using \includegraphics{figures/figure1.pdf}
Now, in order to create the tikz figures and convert them to pdfs I used the following approach.
I created them using ktikz and I saved them in the appropriate folder. NOTE: Each tikz file contained only the
\begin{tikzpicture}...\end{tikzpicture}
. NOT the preamble! NEITHER\begin{document}
and\end{document}
. You can find an example here.I created a a file named
tikz_preamble.tex
containing the necessary preamble for the compilation of the tikz figures AND\begin{document}
-\end{document}
. You can find an example here.Lastly I wrote a python script that would search recursively for files with
*.tikz
extension and would compile them using thetikz_preable.tex
. On subsequent runs, only newly created files and files with changes would be compiled. The*.pdf
files are created in the same folder as the*.tikz
files. So thefigures
folder would look like this:└── figures ├── figure1.pdf ├── figure1.tikz ├── figure2.pdf └── figure2.tikz
Keeping the tikz files preamble as a separate file may seem as inconvenient but it allows greater flexibility. If you decide to make severe changes in your preamble (e.g. change font or font size - that actually happened to me, one day before the presentation!) then all you have to do is to adjust tikz_preamble
and to call the script with a certain command line argument and it will update all your tikz files without any hassle.
PS. I would be interested in reading other people's work-flows.
-
I understand the thinking behind this process, and this is how I started also. However, the main benefit you describe of a change needed in the
tikz_preamble
can still be achieved with thestandalone
pacakge by using it as\usepackage{tikz_preamble}
. Then the preamble can be easily changed if needed, the pictures are compilable by themselves, and can also be imported into the main document. Commented Oct 30, 2012 at 20:29 -
@PeterGrill Keeping the preamble together with the
tikzpicture
environment is not so convenient when you work withktikz
. Also you have to ensure thattikz_preamble
package is within the TeX path (actually it is trivial to do it - e.g. symlinking to thetexmf-local
). Anyway, the other main benefit is the ability to compile automatically all your source files with a single command (e.g. if you use version control and you clone your repo).– pmav99Commented Oct 31, 2012 at 20:47
Naturally I can't compete with Martins answer :) but I have a different approach which does not include the use of 'yet another' additional package (you know what I mean).
Resulting Document
First of all -- here is the result:
Screeshots of the Folder Structure
Here is my simplified folder structure
The content of every folder is
Main File
The main file looks like this:
\documentclass[parskip]{scrartcl}
% The Usual Suspects
\usepackage[latin1]{inputenc}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{float}
\usepackage{lmodern}
% Nice Captions for Tables and Figures etc.
\usepackage[%
font={small,sf},
labelfont=bf,
format=hang,
]{caption}
\usepackage{tikz}
% For Random Text
\usepackage{blindtext}
% Load Custom Styles
\input{TikZ-Styles/myUnitStyle.tex}
\input{TikZ-Styles/myLineStyle.tex}
\input{TikZ-Styles/myColorStyle.tex}
\input{TikZ-Styles/myGrayStyle.tex}
% -------------------------- Start Document --------------------------
\begin{document}
\section*{Test Section}
\blindtext
% That's all!
\input{Figures-Input-Code/fig_Squares.tex}
\blindtext
\end{document}
% -------------------------- End Document --------------------------
Figure Input Code
The Figure Input Code looks like this:
\begin{figure}[H]
\centering
\input{TikZ-Code/Code_Squares.tex}
\caption{Example TikZ picture that has nothing special to offer.}
\label{fig_Squares}
\end{figure}
The label of the figure always has the filename of the Figure Input Code file. In this case the file is named fig_Squares.tex
. So I have a consistent principle. The TikZ code file also shares the filename -- but the prefix is Code
. Here it is therefore Code_Squares.tex
.
TikZ Code
The TikZ Code looks like this:
\begin{tikzpicture}[myUnitStyle,myLineStyle]
\draw [myColorStyle] (0,0) rectangle (10,10);
\draw [myGrayStyle] (12,0) rectangle (22,10);
\end{tikzpicture}
TikZ Styles
The TikZ Styles look like this:
\tikzset{myColorStyle/.style={fill=yellow,draw=red}}
\tikzset{myGrayStyle/.style={fill=gray,draw=black}}
\tikzset{myLineStyle/.style={line width=2pt}}
\tikzset{myUnitStyle/.style={x=1mm,y=1mm}}
The filenames for the styles reflect the name of the style, as you can see.
Advantages
I see the following advantages
- Lean and clean main document
- You don't need to worry how to name/label the figures
- It's easy to reuse the code in other documents
- It's easy to only compile parts by commenting the
\input
commands -- this also makes it easier to debug - You do not need to include a separate preamble to every TikZ picture -- I guess it would be annoying to edit every preamble if you need to change it or if you want to reuse the code in other documents
Of course Martins solution is better. But in my experience new LaTeX users are happy if they do not need to understand an additional package.
-
1I understand the problem with adding new packages. I run out of memory using "pdflatex" after including the "standalone" package. I do not want to run "lualatex" or "latexmk" because they had other issues..... Commented Nov 5, 2015 at 19:20
-
1RiWelcome. :-) I have not seen in this period. My best regards. Commented Aug 29, 2020 at 19:11
-
@sebastiani I moved to China and then came back to Germany recently. Currently my life is pretty busy. Therefore I was not active on this site. I am happy that you say hi to me! Commented Aug 29, 2020 at 23:02
You can create a style sheet commandsty.sty with the following contents:
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{commandpackage}
[2014/12/09 v0.01 LaTeX package for my own purpose]
\RequirePackage{tikz}
\newcommand{\tikzpic}[1][fill=black]{\begin{tikzpicture}
\end{tikzpicture}
}
\endinput
Then you may include it by simply using \usepackage{commandpackage} in your main tex file. Then you may simply use \tikzpic to place it inline.