0

I have already found and read some questions related to the issue of cropping a tikz picture.

The problem here is that I have a tikz code coming from R that is then imported in a new document.

Example

\documentclass[12pt, a4]{article}

\usepackage{tikz}

\begin{document}

\section{The First Section}
\label{S:1}

Maecenas fermentum urna ac sapien tincidunt lobortis. Nunc feugiat
faucibus varius. Ut sed purus nunc. Ut eget eros quis lectus mollis
pharetra ut in tellus. Pellentesque ultricies velit sed orci pharetra
et fermentum lacus imperdiet. Class aptent taciti sociosqu ad litora
torquent per conubia nostra, per inceptos himenaeos. Suspendisse
commodo ultrices mauris, condimentum hendrerit lorem condimentum et.
Pellentesque urna augue, semper et rutrum ac, consequat id quam. Proin
lacinia aliquet justo, ut suscipit massa commodo sit amet. Proin
vehicula nibh nec mauris tempor interdum. Donec orci ante, tempor a
viverra vel, volutpat sed orci.

\begin{figure}[!h]
\centering
\include{Figure1}
\setlength{\abovecaptionskip}{0cm}
\caption{Integer risus dui, condimentum et gravida vitae, adipiscing et 
enim. Aliquam erat volutpat. Pellentesque diam sapien, egestas eget 
gravida  ut, tempor eu nulla.\label{fig:1}}
\end{figure}

\end{document}

Is there a way to simply crop this graph? In my paper, there will be many, so doing so with the grid solution, could be very long.

Is there also a way to set the graph width equal to textwidth after having deleted the white space around it?

At this link you can download Figure1: goo.gl/aauPoE

  • You are combining multiple questions into one. But first of all: you want a TikZ solution or a pure LaTeX solution? – Symbol 1 Nov 8 '16 at 4:34
2

I don't know R, so I don't know if it's possible to change these white margins around the figure. But I think that the best solution would be to try it.

An alternative and more expensive solution consists in:

  1. Process each figure in a standalone document
  2. Crop the result with pdfcrop
  3. Insert the result in main document with \includegraphics[width=\linewidth]{...}

As an example let's apply this process to your figure:

The standalone document looks like:

\documentclass[tikz]{standalone} 

\begin{document}
\input{Figure1.tex}
\end{document}

and produces and figure with some white space around it (This result shows that the white space is already defined in TiKZ code):

enter image description here

If the result is figure1.pdf, it can be cropped with pdfcrop. The command is:

pdfcrop figure1.pdf

The result will be figure1-crop.pdf which looks like:

enter image description here

Finally the main document file will include the TikZ figure as an image with includegraphics:

\documentclass[12pt, a4]{article}

\usepackage{graphicx}

\begin{document}

\section{The First Section}
\label{S:1}

Maecenas fermentum urna ac sapien tincidunt lobortis. Nunc feugiat
faucibus varius. Ut sed purus nunc. Ut eget eros quis lectus mollis
pharetra ut in tellus. Pellentesque ultricies velit sed orci pharetra
et fermentum lacus imperdiet. Class aptent taciti sociosqu ad litora
torquent per conubia nostra, per inceptos himenaeos. Suspendisse
commodo ultrices mauris, condimentum hendrerit lorem condimentum et.
Pellentesque urna augue, semper et rutrum ac, consequat id quam. Proin
lacinia aliquet justo, ut suscipit massa commodo sit amet. Proin
vehicula nibh nec mauris tempor interdum. Donec orci ante, tempor a
viverra vel, volutpat sed orci.

\begin{figure}[!h]
\centering
\includegraphics[width=\linewidth]{Figure1-crop}
\setlength{\abovecaptionskip}{0cm}
\caption{Integer risus dui, condimentum et gravida vitae, adipiscing et 
enim. Aliquam erat volutpat. Pellentesque diam sapien, egestas eget 
gravida  ut, tempor eu nulla.\label{fig:1}}
\end{figure}

\end{document}

enter image description here

Steps 1 and 2 can be included into an script which can process all figures.

A windows batch file which can crop the file Figure1.tex could be:

rem CropRFig <R-Figure-name-without-tex-extension>
@echo \documentclass[tikz]{standalone}      >  temptex.tex
@echo \begin{document}                  >> temptex.tex
@echo \input{%1.tex}                    >> temptex.tex
@echo \end{document}                    >> temptex.tex
pdflatex temptex.tex
pdfcrop temptex.pdf 
copy temptex-crop.pdf %1.pdf /Y 

Command CropRFile Figure1 will produce a cropped figure in file Figure1.pdf.

  • Cristal clear! I have one doubt about the pdf cropping command. Where and how do I have to insert it? How can I write the script command you are referring to? I think I haven't the skills to create one. Having a lot of graphs, a script code could help me write down faster my work. Moreover, I need also to transform the pdf image in eps image, hence maybe a 3rd step in the script would help. – Francesco Bianchi Nov 8 '16 at 16:47
  • @FrancescoBianchi pdfcrop has to be used from command line or into an script. Where do you work, over windows or linux? – Ignasi Nov 9 '16 at 9:58
  • @FrancescoBianchi Why do you need to convert to eps? – Ignasi Nov 9 '16 at 9:59
  • I am working on OS X (with a MacBook). A colleague of mine, working with me on the paper, asked for it. I know that a tikz image is already in vectorial format. – Francesco Bianchi Nov 9 '16 at 14:31
  • @FrancescoBianchi I don't know OS X, so I've added a windows batch file to my answer. Hope you can translate. If you want to obtain an eps result, take a look at: tex.stackexchange.com/questions/20883/how-to-convert-pdf-to-eps – Ignasi Nov 9 '16 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.