Often I include graphics with the trim option. Everytime I have to start a trial and error search for the right values. Is it possible to do it more straight forward?

  • I know, but I want to tell this to the audience. The question is for some improvements on my script. – Dirk Dec 10 '12 at 12:16
  • 3
    May I suggest you take a look at How to share a LaTeX “recipe”? to see how to handle this best? I think you can go one of two ways here: If you really primarily want to share your idea, make a "real" question out of this ("I need to clip an image, but don't want to use trial-and-error. How can I do that?"), mentioning that you already have a solution, and then post your solution as an answer. Or if you really want improvements, rephrase your question to make that the focal point. What kinds of improvements are you thinking about? – Jake Dec 10 '12 at 12:48
  • First of all, I´d like to share this idea. So I will edit the post to a real question and answer it myselt. In the second point I´d like to get some improvements. I don´t know which ones, because the scripts works well for me. – Dirk Dec 10 '12 at 13:21

Sometimes the grid in the corner is to far away, when cropping to axis labels. So I made an update, where I add rectangles over the whole image. And I add an optional parameter to control how deep the rectangles are drawn.

The thick lines have a distance of 10mm, the thin ones of 2mm. This is independent of the image if no width or height argument is passed to the image.


% Linen über Graphiken
        \begin{tikzpicture}[inner sep=0]
            % Bild laden
            \node[anchor=south west] (image) at (0, 0) {#3};
            % Linien einfügen
                % Äußere Schleife für dicke Rechtecke
                \foreach \iThick in {0, ..., \griddepth} {%
                    \path (image.north east) ++(-\iThick, -\iThick) coordinate(topright);
                    \draw[semithick] (\iThick, \iThick) rectangle (topright);
                    % Zwischen den Linien auffüllen
                        % dünne Rechtecke
                        \foreach \iThin in {1, ..., 4} {%
                            \path (image.north east) ++(-\iThick, -\iThick) ++(-\iThin/5, -\iThin/5) coordinate(topright);
                            \draw[very thin] (\iThick, \iThick) ++(\iThin/5, \iThin/5) rectangle (topright);

Call the macro with


On images it is still necessary to put the width from the image to the first argument of \showgrid.

\showgrid[1]{0.8\linewidth}{\includegraphics[clip, trim=20mm 34mm 8mm 16mm]{Test.pdf}}
  • Since you are talking about "axis labels" and you requested "best-practices", you may be interested to consider pgfplots combined with the trim axis left, trim axis right options. This would automatically exclude axis labels from the bounding box. pgfplots can also generate axes on top of pictures if needed. – Christian Feuersänger Dec 15 '12 at 17:36
  • I tried \begin{tikzpicture}[trim axis left,trim axis right] with \includegraphics inside. That does not work. This is only for graphics created in tikz, isn´t it? – Dirk Dec 17 '12 at 15:12

To speed up the trimming process when including an image I build a little script with the help of TikZ:

% Gitter über Graphik
        \begin{tikzpicture}[inner sep=0]
            % Bild laden
            \node[anchor=south west] (image) at (0, 0) {#2};
            % Koordinaten fast oben rechts
            \path (image.north east) -- ++(-\gridlen, -\gridlen) coordinate (obenrechts);

                % Gitter unten links
                \draw[xstep=.2, ystep=.2, very thin] (0, 0) grid (\gridlen, \gridlen);
                \draw[xstep=1, ystep=1, semithick] (0, 0) grid (\gridlen, \gridlen);
                % Gitter oben rechts
                \draw[xstep=.2, ystep=.2, shift={(obenrechts)}, very thin] (0, 0) grid (\gridlen, \gridlen);
                \draw[xstep=1, ystep=1, shift={(obenrechts)}, semithick] (0, 0) grid (\gridlen, \gridlen);

                % Rahmen
                \draw (0, 0) rectangle (image.north east);

One can call the script by



\showgrid{0.8\linewidth}{\includegraphics[clip, trim=31mm 58mm 102mm 31mm]{Test.pdf}}

In the second call, one has to shift the width from includegraphics to the first argument of \showgrid.

The thick lines in the grids stand for 1cm, the thin ones for 2mm. These units are independent from the width of the graphic, because it is scaled after the grids are added. This is the reason why the argument of width=... must be shifted from includegraphics to the first argument of \showgrid.


With PSTricks.

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

\def\M{10}% columns
\def\N{10}% rows
\def\scale{1}% scale
\def\filename{shaolin}% filename





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


%\Navigator% disable it after using

How to use:

  1. \Nagivator will show the grid from which we determine the viewport coordinates.

    enter image description here

  2. \ViewPort is used to clip everything outside viewport.

    enter image description here

  3. Remove border option in the document class to get a tight output.

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.