2

I want to trim my figure in LaTeX code. Unfortunately, it is an SVG figure, so I import it with:

    \begin{figure}[h]
    \centering
    \def\svgwidth{0.8\columnwidth}
    \input{MessAbsch.eps_tex}  
    \caption{Caption}
    \label{fig:measRanEst}
    \end{figure}

For normal \includegraphics you can use: trim=1cm 2cm 3cm 4cm, is there an equivalent for the \input macro?

Ingmar
  • 6,690
  • 5
  • 26
  • 47
  • 2
    When importing SVG's I would generally advise you to use the svg package and using the \includesvg command. Here, you can add several parameters for the display of your SVG. Unfortunately I could not find an option to trim the images in the documentation. Is there a specific reason to why you cannot trim your image directly in inkscape? – pbaer Mar 17 '20 at 07:40
  • https://tex.stackexchange.com/q/122871/108724 –  Mar 17 '20 at 10:04

2 Answers2

3

For importing SVG drawings, it is better to use the svg package than to manually \input them. (See this answer for details on how to set it up.) This package does not provide a trim option, but the adjustbox package lets you trim (and clip) anything just like with \includegraphics.

So, after loading the packages

\usepackage[inkscapearea=page]{svg}
\usepackage{adjustbox}

you can trim your drawing

\adjustbox{trim=1cm 2cm 3cm 4cm}{%
  \includesvg{MessAbsch}%
}

Note that \includegraphics always trims first and scales after, but with adjustbox you will trim last. Thus, if you want your final drawing to have a width of 6cm, you will have to include it too large before trimming.

\adjustbox{trim=1cm 2cm 3cm 4cm}{%
  \includesvg[width=9cm]{MessAbsch}%
}

This approach also works with \input, of course, but using svg comes with several advantages that make using it worthwhile.

schtandard
  • 14,892
1

I generally draw on Google slides and download the SVG files. I tried \adjustbox method mentioned by schtandard. It didn't work as expected.

Initially, i wanted to crop out the shaded area

enter image description here

I used

\lipsum[2-2]
\begin{figure}[h]
    \centering
    \adjustbox{trim=1cm 2cm 3cm 4cm}{
        \frame{\includesvg[width=0.90\textwidth]{Diagrams4.svg}}
    }
    \caption{\textbf{Overall ------------------------}}
    \label{fig:arch}
    \centering
\end{figure}

Result was as follows

enter image description here

I started reading the code of SVG files and found a way to manually crop SVG files. The following image illustrates my findings. You can imagine that robot is holding a knife.

enter image description here

A major drawback in above approach is that one has to try different values manually to get the perfect cropping window. I discovered a simpler way. We can simply crop SVG files using Libre Draw.

Insert image --> Crop --> Export as SVG

Result was as follows:

enter image description here

Ritwik
  • 111