# Include a single PDF image that is on an otherwise empty page

I am using Dia to generate a vector based diagram. Then, I export it to PDF, because I want to keep the vector format and want to build my complete document to PDF later on.

When I include my diagram it uses a complete page to include that diagram. How do I import a diagram (PDF) into my document? It only has a single image and I want it to only include that image.

\usepackage{graphicx}
% more bla...

\begin{figure}[H]
\includegraphics[width=1\textwidth]{img/diagram.pdf}
\end{figure}


I was already thinking of generating ps2pdf, but that would still give me a PDF file with the same problems probably.

-
It sounds like one of your issues is you want to crop the PDF to remove the whitespace around it. That is answered in several places on the site, such as here. pdfcop and Briss are 2 good choices. –  DJP Oct 22 '11 at 0:17
Note that you don't need a figure environment to use \includegraphics. You only need one if you want the image to float (which you apparently don't if you use [H]) and/or if you want a \caption (which can be included using other means as well). –  Martin Scharrer Oct 22 '11 at 8:32
Either tell Dia to generate a cropped PDF without any whitespace (i.e. not a full A4 or letter sized PDF) or use pdfcrop to generate such a PDF from it. –  Martin Scharrer Oct 22 '11 at 8:36
@MartinScharrer I haven't found any options in Dia to do that. I found page setup, but it can only create fixed size papers. So that's a shame. –  Marnix Oct 22 '11 at 9:30
@MartinScharrer Where can I find to include graphics without float? I have read the caption documentation, but it doesn't seem to tell me how to do that. And: Why not use float with H? It works fine... –  Marnix Oct 22 '11 at 9:58

You have a couple of options with this "problem".

1. You can use an external approach and trim the whitespace around the image. pdfcrop is capable of doing this and uses the following interface

pdfcrop [options] <input[.pdf]> [output file]


where [ ] denotes optional specifications. If your Dia-exported PDF image, (say) image.pdf, consists of entire blank page with only an image (nothing else on the page like a page number or header/footer), then you can type

pdfcrop image.pdf image.pdf


which will trim the excess whitespace, leaving only the image in the PDF. Then you can include it as usual without having to specify the width parameter to \includegraphics:

\includegraphics{image}

2. Another (somewhat) external approach would be to use \includegraphics is the only component in a standalone document class using the following format:

\documentclass{standalone}
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\begin{document}
\includegraphics{<options>}{<image>}
\end{document}


or

\documentclass{article}
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\usepackage[active,graphics,tightpage]{preview}% http://ctan.org/pkg/preview
\begin{document}
\includegraphics[width=4cm]{tiger}
\end{document}


This should produce a trimmed version of your image that you can incorporate in your document in the usual way. standalone uses the preview package to aid in this functionality, so you could use preview as well, as in the second example.

As commented by @MartinScharrer, this might not work as expected if your bounding box is already a full (whitespace included) page.

3. You can perform the trimming option from within LaTeX using the trim option of \includegraphics. From the graphicx package documentation:

bb The argument should be four dimensions, separated by spaces. These denote the 'Bounding Box' of the printed region within the ﬁle.

viewport The viewport key takes four arguments, just like bb. However in this case the values are taken relative to the origin speciﬁed by the bounding box in the ﬁle. So to 'view' the 1in square in the bottom left hand corner of the area speciﬁed by the bounding box, use the argument viewport=0 0 72 72.

trim Similar to viewport, but here the four lengths specify the amount to remove or add to each side. trim= 1 2 3 4 'crops' the picture by 1bp at the left, 2bp at the bottom, 3bp on the right and 4bp at the top.

However, you'd have to know (or by trial-and-error) what the bounding box of the contained image is in bp measurements.

4. Use the adjustbox package which provides \clipbox. It works in a similar manner to the trim option of \includegraphics. It also provides an export package option which exports functionality to \includegraphics. Read the package documentation for more information on this.

Edit:

• If you export your image from Dia as an EPS and it is already tightly cropped, then epstopdf can convert them to PDF and you're good-to-go. If the bounding box is not tight, you could try using epstool to tighten it.
-
Note that for 2. that standalone (actually the used preview package) doesn't crop white spaces from included images. It simply crops to the TeX bounding box of the image, which makes it useless in this case. Also \pagestyle{empty} is already included for this class (and not even required as long preview is enabled). –  Martin Scharrer Oct 22 '11 at 8:31
Option 1 sounds like a great idea. I will try to install this and see if it runs. Strange that there is no such way for creating cropped PDF's from out Dia. Could I also create an EPS and do something with that instead or is that harder? –  Marnix Oct 22 '11 at 9:38

Have you tried using the standalone document class instead of, say, the article document class? It'll automatically crop off all whitespace.

-
He is talking about an external PDF which didn't got created by LaTeX, so standalone is no option here. –  Martin Scharrer Oct 22 '11 at 8:28

I found out that I can use the tool called eps2pdf. I can let Dia print an EPS file and then let eps2pdf change it to a pdf that has no a4 format. Since this is not the actual question I asked, I will accept the other answer, but this is how I finally did it.

1. Dia: Export to an eps file
2. eps2pdf: Export the file to pdf
3. \includegraphics{mypdf}
-