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.

I'd like to generate only a pgf/TikZ image with LaTeX, but so far I just managed to insert it into a document. The problem is that so far I can only get a page/a beamer slide rather than just the image I'd like to have.

What document class should I specify to get what I want?

share|improve this question
    
Have a look at Fitting page size to a tikz figure, and please tell us if the answer there (or here) helped. If here, you could accept the answer by clicking the checkmark (you probably know). –  Stefan Kottwitz Sep 28 '11 at 20:14
add comment

migrated from stackoverflow.com Sep 28 '11 at 20:09

This question came from our site for professional and enthusiast programmers.

4 Answers

up vote 14 down vote accepted

Assuming that you are producing the images in a separate document, you can use the standalone class.

For example, this will produce a cropped image by default.

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\draw [red] (0,0) rectangle (1,1);
\end{tikzpicture}
\end{document}

Alternatively, you can include this file directly into your main file and have the image produced there.

share|improve this answer
    
Thank you so much, it was exactly what I was looking for –  mariosangiorgio Sep 28 '11 at 20:22
add comment

Just another option is preview. With it you can also generate cropped figures but with a great advantage: you can have several figures on the same .tex file. The result will be a pdf file with a cropped figure per page. Then you can select which figure include in your document with 'page=' option of graphicx package.

One example:

\documentclass{article}
\usepackage[active,pdftex,tightpage]{preview}
\usepackage{tikz}

\PreviewEnvironment{tikzpicture}

\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle (2,3);
\end{tikzpicture}

\begin{tikzpicture}
\draw (0,0) rectangle (3,2);
\end{tikzpicture}
\end{document} 

Process this file and will obtain a pdf with two pages, then select which figure to include in your main document:

\documentclass{article}
\usepackage{graphicx}
\begin{document}
 \begin{figure}\centering
  \includegraphics[page=1]{Preview}%<-- Replace 'Preview' with your figures file's name
  \caption{First rectangle}
 \end{figure}
 \begin{figure}\centering
  \includegraphics[page=2]{Preview}%<-- Replace 'Preview' with your figures file's name
  \caption{Second rectangle}
 \end{figure}
\end{document}

Update:

With verson 1.x of standalone class you don't need to use preview package because my previous description is standalone class default behaviour with option tikz. So, with this simpler code:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle (2,3);
\end{tikzpicture}
\begin{tikzpicture}
\draw (0,0) rectangle (3,2);
\end{tikzpicture}
\end{document} 

you will also get a pdf file with two pages with one cropped figure in each page.

share|improve this answer
add comment

If you create the tikzpicture in an empty document,

\documentclass{article}
\usepackage{tikz}% http://ctan.org/pkg/pgf
\pagestyle{empty}% Remove page headers/footers
\begin{document}
\begin{tikzpicture}
  ...
\end{tikzpicture}
\end{document}

you can use pdfcrop to trim the excess edges/whitespace.

share|improve this answer
add comment

What about KTikZ ?

It produces figures from tikz code.

share|improve this answer
add comment

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.