Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

3 Answers

up vote 6 down vote accepted

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.

\usepackage{tikz}

% Linen über Graphiken
\newcommand{\showgrid}[3][5]{%
    \providecommand{\griddepth}{#1}
    \resizebox{#2}{!}{%
        \begin{tikzpicture}[inner sep=0]
            % Bild laden
            \node[anchor=south west] (image) at (0, 0) {#3};
            % Linien einfügen
            \begin{scope}[red]
                % Ä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
                    \ifnum\iThick<\griddepth
                        % 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);
                        }
                    \fi
                }
            \end{scope}
        \end{tikzpicture}
    }
}

Call the macro with

\showgrid[6]{0.9\linewidth}{\color{lightgray}{\rule{20cm}{25cm}}}

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}}
share|improve this answer
    
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
add comment

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

% Gitter über Graphik
\newcommand{\showgrid}[2]{%
    \newcommand{\gridlen}{5}
    \resizebox{#1}{!}{%
        \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);

            \begin{scope}[red]
                % 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);
            \end{scope}
        \end{tikzpicture}%
    }
}

One can call the script by

\showgrid{0.8\linewidth}{\rule{10cm}{10cm}}

or

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

share|improve this answer
add comment

With PSTricks.

\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{shaolin}% filename


\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[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)}


\begin{document}
%\Navigator% disable it after using
\ViewPort(2,3)(6,6)
\end{document}

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.