9

I want to include a figure from another PDF.

There is the quick way, where I take a screen shot, save as my desired format and the \includegraphics[scale=xx]{<file path>} but this is messing up the scaling, want the text size in the image to match the text size of my document.

I'm condering using \includepdf[pages=x,trim= l r b t,clip]{<file path>}.

I'd like to know, in general, if there is a smarter way.

Example

I want to include a chart from a user manuel usemanual.pdf showing relations between variables when defining a input grid for a computational model, in my document Thesis.tex

  • You take a screenshot from the monitor? That will never have satisfactory resolution to be printable. Trust me, I've rejected many such figures as a Graphic Editor of a journal ;) – yo' Nov 20 '13 at 12:56
  • @tohecz yes, that's why I'm asking for a solution, not doing high school reports anymore :) – Malthe Eisum Nov 20 '13 at 13:01
  • Why do you printscreen anything, cannot you just convert it to PDF directly? And what are you printscreening? – yo' Nov 20 '13 at 13:07
  • Do you want only a figure from that pdf or also some text to match the font size? Print that page in pdf using a pdf printer and then include it using \includegraphics. Use the trim option to clip it. – Sigur Nov 20 '13 at 13:15
7

graphicx is able to add only one page from a PDF file and to trim it.

\documentclass{article}
\usepackage{graphicx}
\begin{document}
Some text

\includegraphics[
  page=15,
  trim=14.8cm 5cm 1cm 21cm,
  clip
]{../calcolo/trigonometria/trigonometria.pdf}

Some other text

\end{document}

enter image description here

This is the original page:

enter image description here

Some trial and error in determining the amount of trimming will solve the problem.

  • I am surprised why graphicx/grfguide does not include this page=<xx> option, since it's missing in documentation. – texenthusiast Nov 20 '13 at 17:54
  • @texenthusiast there was no page option at the time the manual was written, it is added by the pdftex specific back end definition file. – David Carlisle Nov 20 '13 at 18:38
  • @DavidCarlisle Is there something that prevents from updating to next version with minor doc tweaks ? Because the power of graphicx is so enormous – texenthusiast Nov 20 '13 at 18:41
  • @texenthusiast other than inertia on the part of the author? graphicx is written such that arbitrary back end files can be added by anyone and they can if necessary add additional keys. Of course in practice I know something about what pdftex.def is doing but in principle I need not, and certainly I don't get notified when it is updated, and I don't know in detail what the other currently important back ends eg xetex's support. So the central graphicx documentation isn't really the right place (I claim:-) – David Carlisle Nov 20 '13 at 19:43
  • @DavidCarlisle Not to blame you for sure, any technical hurdle i mean. I can understand in latex ecosystem it so complicated to achieve 100 % harmony, if one tries to set fix one end of thread knot, he would be making another knot unknowingly :) Thanks as always for your insights. – texenthusiast Nov 20 '13 at 19:58
4

You can use the following code to determine the viewport to trim anything outside.

% cropper.tex
\documentclass[preview,border=12pt]{standalone}% remove border option to get a tight output
\usepackage{pstricks}

\def\M{10}% columns
\def\N{10}% rows
\def\scale{1}% scale
\def\filename{example-image-a.pdf}% filename
\def\page{1}% page

\usepackage{graphicx}
\newsavebox\IBox
\savebox\IBox{\includegraphics[scale=\scale]{\filename}}

\addtopsstyle{gridstyle}
{
    gridcolor=yellow,
    subgridcolor=gray,
    subgriddiv=10,
    griddots=0,
    subgriddots=5,
    gridwidth=0.4pt,
    subgridwidth=0.2pt,
}

\psset
{
   xunit=\dimexpr\wd\IBox/\M,
   yunit=\dimexpr\ht\IBox/\N,
}


\def\Navigator{%
\begin{pspicture}[showgrid=top](\M,\N)
    \rput[bl](0,0){\usebox\IBox}
\end{pspicture}}

\def\viewport(#1,#2)(#3,#4){\includegraphics[page=\page,scale=\scale,viewport=#1 #2 #3 #4,clip]{\filename}}

\def\ViewPort(#1,#2)(#3,#4){\viewport(\the\dimexpr#1\psxunit,\the\dimexpr#2\psyunit)(\the\dimexpr#3\psxunit,\the\dimexpr#4\psyunit)}

\parindent=0pt\relax

\begin{document}
\begin{center}
\Navigator% disable it after using

\vspace{1cm}
\ViewPort(3.5,2.6)(6.5,7.3)
\end{center}
\end{document}

enter image description here

Compile it with

xelatex cropper.tex

You might need multiple compilation to get better viewport values. And the final compilation should turn the navigator off to get a tight cropped output in PDF format.

Once you have a tight PDF output, you are ready to import it from within your main input file using includegraphics[<any options>]{cropper}. The compilation should be done by pdflatex.

Batch processing

Cropping a bunch of images taken from a single PDF (based on @TeXenthusiast's comment). status-lua.pdf is the only testable multiple page PDF exists on all computers with TeX installed (I think).

% cropper.tex
\documentclass[preview,border=12pt,multi]{standalone}% remove border option to get a tight output
\usepackage{pstricks}

\def\M{10}% columns
\def\N{10}% rows
\def\scale{1}% scale
\def\filename{status-lua.pdf}% filename
\def\page{1}% page

\usepackage{graphicx}
\newsavebox\IBox
\savebox\IBox{\includegraphics[scale=\scale]{\filename}}

\addtopsstyle{gridstyle}
{
    gridcolor=yellow,
    subgridcolor=gray,
    subgriddiv=10,
    griddots=0,
    subgriddots=5,
    gridwidth=0.4pt,
    subgridwidth=0.2pt,
}

\psset
{
   xunit=\dimexpr\wd\IBox/\M,
   yunit=\dimexpr\ht\IBox/\N,
}


\def\Navigator{%
\begin{pspicture}[showgrid=top](\M,\N)
    \rput[bl](0,0){\usebox\IBox}
\end{pspicture}}

\def\viewport(#1,#2)(#3,#4){\includegraphics[page=\page,scale=\scale,viewport=#1 #2 #3 #4,clip]{\filename}}

\def\ViewPort(#1,#2)(#3,#4){\viewport(\the\dimexpr#1\psxunit,\the\dimexpr#2\psyunit)(\the\dimexpr#3\psxunit,\the\dimexpr#4\psyunit)}

\parindent=0pt\relax

\begin{document}
%\Navigator% disable it after using
\foreach \x in {1,...,4}
{
    \def\page{\x}
    \begin{preview}
        \fbox{\ViewPort(3.5,2.6)(6.5,7.3)}
    \end{preview}
}
\end{document}

enter image description here

  • You are good in CropTricks :) Does this crop multiple selective pages also ? – texenthusiast Nov 20 '13 at 17:25
  • @texenthusiast: It can be used to automatically crop a bunch of images. Even it knows the viewport of the target intelligently. :-) – kiss my armpit Nov 20 '13 at 17:35
  • Let's assume those bunch of images are in one single pdf file. can we select only page 5,15 images to be cropped and included in main doc. – texenthusiast Nov 20 '13 at 17:41
  • 2
    @texenthusiast: It is very easy, for a single invocation of \includegraphics you are allowed only to specify one page, so page=5 or page=15 will work but not page={5,15}. It is totally different from pages={5,15} in \includepdf from pdfpages. – kiss my armpit Nov 20 '13 at 18:02
2

I have a different method using "Ghostscript" and "GeoGebra"

Extract pages file in two different formats (pages 5 to 10):

gs -sDEVICE=jpeg -dNOPAUSE -dBATCH -dFirstPage=5 -dLastPage=10 -sOutputFile=figure-%d.jpg input.pdf
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dFirstPage=5 -dLastPage=10 -sOutputFile=figure-%d.pdf input.pdf

we have the files:

figure-1.jpg,figure-2.jpg,figure-3.jpg,figure-4.jpg,figure-5.jpg,figure-6.jpg

and

figure-1.pdf, figure-2.pdf, figure-3.pdf, figure-4.pdf, figure-5.pdf, figure-6.pdf

Open GeoGebra and create tree points

O=(0,0)
P=(21.6,0)
Q=(0,27.9)

Insert image .jpg (>ABC icon >second choice), press escape, left botom mouse,
set image to backgroud, and position

Corner1= O
Corner2= P
Corner3= Q

Draw a polygon ABCD for your "trim" area Point A should be in the lower left corner,the rest point counterclockwise. Copy and paste in input bar

trim={x(A),y(B),x(P)-x(B),y(Q)-y(C)}

the "trim" parametres are show in left side (Algebra).

trim={value1,value2,value3,value4}

Now, File > Export > PGF/Tikz, remove the code we do not need, and add:

    \documentclass[10pt]{article}
    \usepackage{graphicx}
    \usepackage{tikz}
    \pagestyle{empty}
    \begin{document}
    \includegraphics[trim=value1cm value2cm value3cm value4cm,clip]{figure-1.pdf}
%\includegraphics[page=5,trim=value1cm value2cm value3cm value4cm,clip]{input.pdf}
    \end{document}

save as cropper.tex, compile

pdflatex croped.tex

and croped

pdfcrop croped.pdf figure1-crop.pdf

And repeat the process, at least it works for me. Salu2

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.