How to create and include .tikz files in your manuscript?
I wrote the following mwe
using trial and error.
The main file mwe.tex
:
\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\input{temp.tikz}
\end{document}
The file temp.tikz
simply contains:
\begin{tikzpicture}
\draw (0,0) --(1,2);
\end{tikzpicture}
The output of mwe.tex
looks like this:
There are several things you need to consider :
- Readability : tikz scripts tend to be quite long, and can easily cause a big break in the rest of your source code. From that view, leaving only the document-specific issues in the main document (that is the figure environment, caption, etc), and loading the tikz file from an external file is positive. The same argument may apply for tables, algorithms, etc.;
- Dirty hacks : I put it here because it directly follows the previous point. We may not like it, but there are times when the float engine doesn't do what we (or our publishers) want it to do. Then you have to move the float around the text, sometimes putting it in the middle of a completely unrelated block of text. In that case, you'll really appreciate the picture being only 4-5 lines long, instead of 100;
- Reusing : you may want to reuse the same graphic through several documents, in that case separating it from the main document can save time;
- Debugging the figure : this is in case you want to write tikz pictures yourself. I have no idea how
matlab2tikz
works, but in case it doesn't provide you with an out-of-box graphic, you may want to tweak it to fit your purpose. In that case, the standalone package comes in handy, allowing you to compile only the figure, and gain precious compilation time with long documents (you can see Peter Grill's comments to your question for more specific advice on that matter). Note that other solutions, such as emacs region compiling features, exist, which can make use of the tikz picture being in a separate file, or work even if the picture is still in the main document; - Portability : depending on your workflow it may be more convenient to have only one file to email to your cowriters, for example. Note that this can easily be circumvented, for example by sharing a dropbox, or setting up a VCS server. And also that even with email, it is usually minor inconvenience (only send the files you are working on at the moment, and if you need to send a full compilable project, zip it all in one file).
That's all I can think of, as you can see, it mainly goes towards the "separate files" view. But I may be biased, in that case I expect other answers will go the other way round.
I'll just add a few more notes :
- you can, of course, treat differently a 2-lines tikz script and a full floating picture with caption and label;
- another approach which I never tried is to compile the tikz files to pdf, and then include the resulting pdf in the document. The main advantage is that it saves compilation time for the whole document, the drawbacks are that you lose tikz integration features, such as labels. The
tikzexternalize
library provides a compromise (and can very well be used with separate files or only one file); - your question seems to be about the file extension as well. File extensions depend on the system you use : on windows, they are used to tell the OS what the file is, while on linux they are for the user only. Anyway, in that specific case and unless you have a windows program which is designed to edit tikz scripts, there shouldn't be any difference. It seems to be that most users end all their side-files with the
.tex
extension, no matter what the file is, but I may be wrong and consider only too small a sample. And I can't find any good reason to use always the same extension. So it's really up to you; - last point : I just reread your question, and especially the end of it, finding tikz examples. There would be much more to say about "what is a tikz file", but the documentation (which you already have) is excellent and will tell you that better than me. When looking for examples, it's pretty easy actually : even if it isn't stated explicitly every time, every code snippet beginning with
\begin{tikzpicture}
and\end{tikzpicture}
can be put in a.tikz
(or.tex
) file and input in your main file (provided you load the required libraries in the main file preamble).
Now this question may be considered as concluded as it has an accepted answer. But what if one wants to resize the figure? or add a caption? etc... Remedy is with resizebox
from graphicx
.
\documentclass{article}
\usepackage{tikz}
% \usepackage{graphicx}
\usepackage{filecontents}
\begin{document}
\begin{filecontents*}{temp.tikz}
\begin{tikzpicture}
\draw (0,0) --(1,2);
\end{tikzpicture}
\end{filecontents*}
\begin{figure}[htb]
\centering
\resizebox{2cm}{!}{\input{temp.tikz}}
\caption{my figure drawn in tikz}\label{fig:myfigure}
\end{figure}
\end{document}
In \resizebox{2cm}{!}{\input{temp.tikz}}
, first argument {2cm} is the width and second is the height
!
is for allowing natural height of the figure (so as to keep the aspect ratio intact.).