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

  • 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. Commented Nov 23, 2012 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. Commented Nov 23, 2012 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. Commented Nov 24, 2012 at 3:15
  • 1
    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...) Commented Nov 24, 2012 at 22:42

3 Answers 3


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).
  • 3
    I don't think it answers to the user question how do you actually include it?
    – G M
    Commented Aug 22, 2019 at 21:19
  • @GM It's in the last paragraph, you \input it. But the author of the question knew that already. You might also want to have a look at the linked question: tex.stackexchange.com/q/79594/9517
    – T. Verron
    Commented Aug 28, 2019 at 8:43
  • Sorry, I did not saw that part of the question (I thinkg that the title of the question is wrong bytheway) I change the vote. thanks
    – G M
    Commented Aug 28, 2019 at 10:37

I wrote the following mwe using trial and error.

The main file mwe.tex:



The file temp.tikz simply contains:

\draw (0,0) --(1,2);

The output of mwe.tex looks like this: enter image description here


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.

% \usepackage{graphicx}

    \draw (0,0) --(1,2);

\caption{my figure drawn in tikz}\label{fig:myfigure}


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

  • 1
    One should be careful with these commands, as they can easily have awful side-effects on the picture font size for example.
    – T. Verron
    Commented Nov 26, 2012 at 6:58
  • 12
    I \usepackage{tikzscale} for this, so that I can \includegraphics[width=2cm]{temp.tikz}.
    – Gustaphe
    Commented Apr 10, 2018 at 11:51
  • @Gustaphe will it also have side effects as the previous poster was talking about?
    – stephanmg
    Commented Oct 15, 2020 at 8:42
  • 1
    @stephanmg No. Slightly different side effects, but font size is maintained.
    – Gustaphe
    Commented Oct 16, 2020 at 9:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .