# Insert a TikZ picture from an external file

I tried to search a bit, but i guess my poor English prevented me from finding the answer.

I made a few tikz pictures that i want to use in my report. I made the tikz pictures in separate tex files so i don't have to compile my whole report just to check the image.

But now, when copy pasting the tikz code into my report the image looks different. Some stuff moved or is not in the right position.

I was hoping this could be fixed by instead of putting the tikz code in my report, inserting the tikz.tex file similar to \include or \insert.

This way i hope my picture will look like it does in the separate file and i will not have to compile it when i compile my report.

I guess this is possible, but i just don't know how and i could not find it. Does anyone know_

-

Your problem of different results is most likely due to different font sizes or other settings in the picture and main file. This is especially true if you are using em or ex as placing units. I can't tell more about it without knowing your code.

However, you can avoid this by ensuring identical settings in both files. Should this not be possible you can still compile the external picture to a PDF an include it using \includegraphics. There is a small size overhead coming with it, but it should not be an issue. It has actually a nice speed benefit with larger TikZ pictures.

For external TikZ pictures I recommend the following procedure: Use the standalone class for the TikZ picture file and then use the standalone package in the main file. This way you can use \input to read the external file while its preamble is ignored or even copied to the main preamble automatically. When the external picture is compiled by its own standalone makes sure it is placed on a tight page using the preview package. The resulting PDF pictures are very suitable to be included into the main document. I'm currently working on a new version of standalone which gives you an \includestandalone with an option if the .tex or .pdf should be included!

### Update Apr 2012:

The v1.0 of the standalone package now provides \includestandalone which works like \includegraphics, but with standalone files (and similar). The package has a mode=<mode> option which controls if the standalone source file is compiled with the main document (mode=tex) or a PDF version is included as image (mode=image). The PDF can also be build if required (mode=build, mode=buildnew or mode=buildmissing).

An important option for this question is obeyclassoptions, which will try to obey all class option used in the standalone file even if this file is compiled as part of the main document and therefore the standalone preamble is ignored. This means if the external files use \documentclass[12pt]{standalone} then a 12pt font size is also used when this file is included (as source code) in the main document.

-
Thx for the answer! I will check it out tomorrow (dont have the time now) –  Elmer Feb 20 '11 at 19:59
Not exactly an answer to your technical question but maybe you can avoid the problem completely if you use Qtikz to check your images or even draw them directly as I usually do. –  percusse Feb 21 '11 at 0:08
Thanks @percusse. I used Qtikz/ktikz before a little bit. It is a very useful program, but I personally like the above way better, especially if you use the picture in several documents. –  Martin Scharrer Feb 21 '11 at 0:13
@Martin: standalone sounds useful, but does not appear to be in Tex Live 2009 (currently in Debian). If I understand the usage correctly, this makes it possible for the same external tex file either to compile a standalone TikZ picture (say), or to be included in a larger document and compiled as part of that larger document. Support for switching seamlessly between a PDF include and including the latex code directly would be nice, if that is what the latter part of your comment says. –  Faheem Mitha Feb 21 '11 at 19:43
@Faheem: The package is newer then 2009. You can download standalone from CTAN. Simply run the .ins file through latex to extract the .sty and .cls files. –  Martin Scharrer Feb 21 '11 at 20:02