[Tex/LaTex] Include a single PDF image that is on an otherwise empty page

cropgraphicspdf

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.

Best Answer

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 file.

    viewport The viewport key takes four arguments, just like bb. However in this case the values are taken relative to the origin specified by the bounding box in the file. So to 'view' the 1in square in the bottom left hand corner of the area specified 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.
Related Question