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.

Recently, I learnt about the matlab code matlab2tikz. The output of this code is a .tikz file that can be used, I guess, alongside standard figure packages with a simple line \input{mysphere.tikz} in the figure environment.

This got me wondering if .tikz is a common way to include the TikZ code in your manuscript. It would be convenient for me to save TikZ scripts in separate files instead of having them all in my final manuscript. This will make my manuscript(s) more readable. Can anyone advise me in this regard?

I searched the pgfmanual for the string .tikz but found no results. I am still searching the web for examples of .tikz files.

(I asked this question TeX.SeX chat a few minutes ago and ported it here on the advise of @WillHunting.)

share|improve this question
    
I think you should be using the standalone package and class. With this you can have the tikz figures in a separate file which can be compiled by itself, and also be included in the main document. –  Peter Grill Nov 23 '12 at 22:05
    
Oh if mysphere.tikz is not a complete document and just the picture environment, then you should create a mysphere.tex (either by copying the code from mysphere.tikz, or using \input{mysphere.tikz}) using the standalone class. Now, mysphere.tex can be compiled to view/tweak the figure as necessary. –  Peter Grill Nov 23 '12 at 22:12
    
@PeterGrill My apologies. The source of this whole mess was my ignorance of the simple \input{filename.ext} command. I just realized after reading the wikibook on LaTeX. –  Shashank Sawant Nov 24 '12 at 3:15
    
This is essentially the same idea as in Outsourcing TikZ code, but using a .tikz file extension to make things clearer to you what the file contains. (Adding this also helps to link the questions in the 'related' pane on the right hand side of the site...) –  cyberSingularity Nov 24 '12 at 22:42

3 Answers 3

up vote 9 down vote accepted

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 is 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 explicitely 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).
share|improve this answer

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

share|improve this answer
    
One should be careful with these commands, as they can easily have awful side-effects on the picture font size for example. –  T. Verron Nov 26 '12 at 6:58

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: enter image description here

share|improve this answer
    
Awesome, it helps. Thanks man! –  Sibbs Gambling Nov 18 '13 at 14:26

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.