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.

Sometimes I'm using tikz as a plotting tool, without embedding the figure in a document. In this case, I normally, want to include the resulting PDF in a different TeX file (yes I know I could include the TikZ code directly, but it is not helpful in this case). Therefore, I want the picture to fit exactly to the page, so I won't have white space around it. To this end, I'm using the geometry package and clip of TikZ. For example, here:

\documentclass{minimal}
\usepackage{tikz}
\usepackage[paperwidth=1cm,paperheight=1cm,hmargin=0cm,vmargin=0cm]{geometry}

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

Is there a better/nicer/more elegant way to do it?

EDIT 1: For the sake of clarifying, I used clip because I thought it will make things simpler. Without it, things are actually more straightforward.

EDIT 2:

Unfortunately one can only accept one answer. In this case I could accept all. For me, I got to know three new tools, all are slightly different, to accomplish my goal. I choose the Martin's answer due to its standalone nature; no need of neither external tools nor post-proccessing. The other two options yielded more or less the same result.

share|improve this question
add comment

3 Answers

up vote 13 down vote accepted

Use the preview package with tightpage,active options to only display elements you want as single pages. You need to wrap the tikzpicture in preview environments or declare it as \PreviewEnvironment{tikzpicture}.

The standalone class gives you exactly this functionality in a very short form:

\documentclass[class=minimal,border=0pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\clip (0,0) rectangle (1,1);
\draw[red] (0,0) -- (1,1);
\draw[red] (1,0) -- (0,1);
\draw[blue] (0,0) rectangle (1,1);
\end{tikzpicture}
\end{document}

Result

(Dark border is the PDF viewer background and not part of the PDF)

You then can load the standalone package (!) in a document and simply \input this file. The preamble and document environment will be automatically striped.

Note:

You are currently clipping at the center of the blue rectangle and cutting it in half. You need to add half the line with on each side of the clipping path:

\clip (-.5\pgflinewidth,-.5\pgflinewidth) rectangle ([shift={(.5\pgflinewidth,.5\pgflinewidth)}]1,1);
share|improve this answer
2  
Also the upcoming version of standalone will allow you to use \includestandalone{file} and decide if you want to use the .tex of the PDF version, and even build the PDF if it is missing. –  Martin Scharrer Jul 20 '11 at 9:02
    
It seems to be the best solution, but when I try out your code, the result is without the upper&right edges of the blue rectangle. I actually noticed that I have the same problem, I somehow missed it. –  Dror Jul 20 '11 at 9:17
    
@Dror: I get the full blue rectangle, but it is thinner than the red lines due to the clipping. You need to add .5\pgflinewidth on each side of the clipping path. This is not an issue of standalone or other solution shown here. –  Martin Scharrer Jul 20 '11 at 9:46
1  
@Dror: See my update. –  Martin Scharrer Jul 20 '11 at 9:54
    
I think I have a viewer problem. Namely, when I try to print-preview I can see a correct and full result (when I remove the clipping). I'm using preview of mac os. –  Dror Jul 20 '11 at 12:48
show 2 more comments

I would recommend using Tikz' external library for this. It saves all TikZ figures in a document in individual cropped PDFs (and includes them in subsequent runs instead of regenerating the figures). The files will be called <jobname>-figure<number>.pdf.

\documentclass{article}
\usepackage{tikz}

% Load the library
\usetikzlibrary{external}

% Activate it
\tikzexternalize

% You can uncomment the following to force a remake of all figures
% \tikzset{external/force remake=true} 

\begin{document}
\begin{center}
\begin{tikzpicture}
\clip (0,0) rectangle (1,1);
\draw[red] (0,0) -- (1,1);
\draw[red] (1,0) -- (0,1);
\draw[blue] (0,0) rectangle (1,1);
\end{tikzpicture}
\end{center}
\end{document}
share|improve this answer
    
Externalizing and already external image is kind of overkill, isn't it? Ok, I'm biased. :-) –  Martin Scharrer Jul 20 '11 at 9:12
    
You're right, it sounds a bit nonsensical, but I guess it doesn't introduce a dramatic overhead? Furthermore, it has the advantage that you can keep several tikzpictures in the same .tex file, e.g. if they use common styles, but still end up with separate PDFs. –  Jake Jul 20 '11 at 9:16
    
Looks great, although maybe not the best solution for this setting. However, I get the same problem as with my example, and with Martin's example - namely missing upper&right edges of the rectangle... –  Dror Jul 20 '11 at 9:18
    
@Dror: All the edges are drawn correctly on my system, however your \clip action halves the line width of the blue rectangle. If I remove it, everything looks as expected. –  Jake Jul 20 '11 at 9:21
1  
It's important to point out that one must compile with pdflatex -shell-scape, if you're using pdflatex –  leo Feb 15 '12 at 4:40
show 1 more comment

You could use pdfcrop to automatically remove the white space. So you get a small pdf image which you could include later.

share|improve this answer
    
Thanks! This was the most helpful answer for me, allowing me to automate pdf generation of figures using R and tikzDevice. –  Nathan Jan 18 '13 at 17:32
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.