12

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_

Vivi
  • 26,953
  • 31
  • 77
  • 79
Elmer
  • 2,254

1 Answers1

22

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.

Martin Scharrer
  • 262,582
  • 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 00: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 00: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
  • @Martin Scharrer i tried ur standalone option. Compiling my tex file with the tikz picture works fine. But when i use the include function in my mainfile i get this error: PGF math error: unknown function of while i dont get this compiling the file with the tikz picture itself. Odd – Elmer Feb 21 '11 at 20:18
  • @Elmer: You should use \input not \include. The error might be because you forgot a Tikz libery in the main file? – Martin Scharrer Feb 21 '11 at 20:50
  • @Martin Scharrer that was a typo sorry. I am using \input and im sure i didnt forget anything, since i didnt change anything in the external tikz picture. And i can compile the tikz picture itself without any problems. – Elmer Feb 21 '11 at 21:04
  • @Martin: Yes, I did that. Dumb question - is it a copyright violation to include the sty file in my code repository (license GPL v2 or later). Maybe this should be a separate question; please indicate if so. – Faheem Mitha Feb 21 '11 at 21:16
  • @Faheem: It's under the normal, free LaTeX LPPL license. I don't mind when it is put into a GPLv2 or later code repository. – Martin Scharrer Feb 21 '11 at 21:23
  • @Elmer: I would have to see the source code to see what is wrong. As test simply copy the {tikzpicture} environment directly in the main file. If the error is still there it isn't caused by standalone. – Martin Scharrer Feb 21 '11 at 21:25
  • @Martin Scharrer then it works fine. No error when copy pasting the code in my mainfile. This is kinda odd. – Elmer Feb 21 '11 at 21:33
  • @Elmer: Do you mind to show me the source code of that file? My email address would be in the package documentation. – Martin Scharrer Feb 21 '11 at 21:35
  • @Martin Scharrer i will once i have a a free moment. Thx – Elmer Feb 22 '11 at 21:10