6

Is there a way to insert a diagram generated by Tikz/PGF directly from within a LaTex file? Currently, I am writing it in its own file, compiling it with pdflatex and then including the generated output file as an image.

lockstep
  • 250,273
puk
  • 3,061
  • 5
  • 30
  • 41
  • Oops, this appears to be a duplicate question http://tex.stackexchange.com/questions/28258/what-is-the-correct-way-to-caption-a-tikzpicture – puk Feb 08 '12 at 03:56
  • If you are producing the pictures in a separate file (recommended), then you should also consider using the standalone package. An example is in this question on crop entire document around figure – Peter Grill Feb 08 '12 at 16:22
  • @PeterGrill the standalone package appears to be a more complex version of the input command does it not? – puk Feb 08 '12 at 20:25
  • The standalone package can be used to produce complete compilable documents with the tikzpictures, and then you can use \input to imports those directly into the parent document without having to remove/comment the preamble required to compile the tikzpicture by itself. – Peter Grill Feb 08 '12 at 20:48
  • @puk: Have a look at the answer to this question: http://tex.stackexchange.com/questions/31825/figure-preview-wrapper – hpekristiansen Feb 09 '12 at 00:11
  • @Hans-PeterE.Kristiansen yes I see the benefit now. – puk Feb 09 '12 at 00:53

2 Answers2

12

Yes, this is possible. If you have a file diagram.tex with your TikZ instructions, just use the following

\begin{figure*}[tbp]
\input{diagram.tex}
\caption{This is my diagram.}
\label{f:diagram}
\end{figure*}

This should work just fine.

Mensch
  • 65,388
  • I was just about to flag this question as a duplicate, but then you answered. I was not aware that you could input it via an external file. Thanks. – puk Feb 08 '12 at 03:55
5

In the preamble add \usepackage{tikz} and necessary tikz libraries and in the document

    \begin{figure}[htb]
    \centering{
    \resizebox{0.8\textwidth}{!}{\input{diagram.tex}}}
    \caption{This is my diagram} \label{fig:diagram}
    \end{figure}

This enables to control the figure dimensions in your text.

  • can you describe what \resizebox{0.8\textwidth}{!} does – puk Feb 08 '12 at 04:38
  • @puk You can adjust the size of the picture. Change 0.8\textwidth to say 0.4\textwidth and see your self. –  Feb 08 '12 at 07:50