Lets take this string:


that generates this figure:

enter image description here

Now lets say that I need to crop the blank margins of the figure keeping the figure's size.

I though I could do it with something like:

\boxed{\includegraphics[trim = L B R T, clip, scale=x]{FIGURE.pdf}}

Where L, B, R, and T are respectively the left, bottom, right and top trimming values and x is the scaling value.

The following example should better explain my question:

\boxed{\includegraphics[width=0.4\textwidth]{FIGURE.pdf}}\quad %

\boxed{\includegraphics[trim = 65 20 150 20, clip, width=0.4\textwidth]{FIGURE.pdf}}\quad %
\boxed{\includegraphics[trim = 65 20 150 20, clip, scale=0.238]{FIGURE.pdf}}

The previus code generates the following layout:

enter image description here

As you can see, using scale value instead of width lets me crop the figure keeping its size.

My question is: how can I get the exact (more or less) value of the scale parameter without go by trial and error? (My idea is to write some Emacs Lisp code to change the LaTeX code accordingly.)

I am open to any idea or suggestion.

Note. In most cases I use external software to crop my images (pdfcrop, briss etc.) but, sometimes, as in this case, they don't work so I need to trim manually.

  • The redefinition of \Gscale@box in this answer does what you want. – Phelype Oleinik May 22 '19 at 20:49
  • @PhelypeOleinik Thank you but I can't see how the redefinition of \Gscale@box should help me. – Gabriele Nicolardi May 22 '19 at 21:13

All graphics are written to the PDF with a scale factor. When you specify a size the package just converts it to an appropriate scale factor. You can make \Gscale@box remember that scale factor for you (source). You can use the same \thelastscalefactor as many times as you need:




% Measuring the image without actually using it

\noindent % Using with the scale only

  \makebox[0.4\textwidth][l]{% <-- trick to align without a tabular
    \includegraphics[trim = 65 20 150 20, clip, scale=\thelastscalefactor]{example-image}%
\fbox{\includegraphics[trim = 65 20 150 20, clip, scale=\thelastscalefactor]{example-image}}



enter image description here

| improve this answer | |
  • Please see that I need the figure to be trimmed but not resized. In your scale=\thelastscalefactor example the figure is resized too. Moreover I need to "phantom" in some way the figure expressed by width because I don't want it to be printed in my PDF. – Gabriele Nicolardi May 23 '19 at 4:51
  • @GabrieleNicolardi The figure was resized because I used width=0.4\textwidth in a narrower image, thus it was adjusted to fit the constraint of width=0.4\textwidth. You can use scale=\thelastscalefactor as many times as you want after you measured it, then the images will obey the constraint of scale, which does not resize them. Sorry, but I don't understand what you mean by “‘phantom’ the figure”. Can you please explain? – Phelype Oleinik May 23 '19 at 10:28
  • Ok, I understood how to mesure the scale factor but in my layout I need to print only one figure, the scaled ones. So I need to "hide" the code from which I get the scale amount. Only one figure has to be printed in my output. – Gabriele Nicolardi May 23 '19 at 11:22
  • @GabrieleNicolardi You can't measure the figure without including it somehow. However you can include the figure inside a box an simply not use it. You can do \sbox0{\includegraphics[...]{...}}, and from this point on \thelastscalefactor is known and you can use it at will. I edited in an example. – Phelype Oleinik May 23 '19 at 13:57
  • Thank you! This is what I needed! – Gabriele Nicolardi May 23 '19 at 15:36

You can keep the width= on the graphics and trim/clip separately so not affecting the scaling, see the last example here:

enter image description here




\includegraphics[clip,trim=1cm .5cm .7cm .1cm, width=.4\textwidth]{example-image}


\adjustbox{clip,trim=1cm .5cm .7cm .1cm}{\includegraphics[width=.4\textwidth]{example-image}}

| improve this answer | |
  • Thank you! I'll use your solution in most cases but I accepted the Phelype Oleinik's solution because it answers better to my question. – Gabriele Nicolardi May 23 '19 at 15:39
  • @GabrieleNicolardi that's OK I'll survive without the tick:-) – David Carlisle May 23 '19 at 15:40

I never liked the idea of scaling and clipping at the same time, not knowing which comes first (keyword order means nothing). This solution clips first then scales by using \savebox and \adjustbox.




\savebox{\tempbox}{\includegraphics[trim = 65 20 150 20, clip]{example-image}}%

| improve this answer | |

I'm not quite sure this is what you are looking for, but here is a way to compute, for instance, 1000 times the desired scale factor, rounded to the nearest integer:



\myscale=\numexpr 400*\textwidth / \wd0 \relax % 400 = 0.4×1000
\showthe\myscale                % displays 430 on the terminal


\includegraphics[scale=0.43]{example-image} % same width as above

| improve this answer | |

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.