# How to crop background from eps plot automatically?

For images I found the solution provided in this Q&A (How to crop background from image automatically?) very helpful and easy to use. Now I also want to use this for my from Matlab exported plots in the eps format. How could this be done? The code to crop images automatically looks like this (copied from the link above):

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

\section*{Original image}

\fbox{\includegraphics[width=\linewidth]{image.png}}

\section*{Trimmed and clipped image}

\fbox{\includegraphics[width=\linewidth,trim=6.5cm 6cm 6.5cm 4cm,clip]{image.png}}

\section*{Automatic crop}
Note: compile with \verb|--shell-escape|\\

\newcommand\cropped[1]{%
\immediate\write18{convert -trim #1.png #1cropped.png}%
\includegraphics[width=\linewidth]{#1cropped.png}}

\fbox{\cropped{image}}

\end{document}

• I can't compile the MWE. I use pdflatex --shell-escape file.tex, is it right? Error ... parameter - image.png. – Bobyandbob Apr 22 '17 at 9:23
• I guess that you need a dummy picture in order to provide a code example for others -- have a look here tex.stackexchange.com/questions/231738 – Dr. Manuel Kuehner Apr 22 '17 at 10:01
• It'll be much easier if you avoid the MATLAB export menu and use the print command instead. Something like print('-depsc2','filename.eps'). The print command should automatically crop the file to the contents. – Mike Renfro Apr 22 '17 at 17:02
• This indeed works very well, still a small white border but thats totally fine in comparison! Just one thing: I was used to the 16:9 ratio from the matlab export and now I get a 4:3 eps exported, do you know how to change this? I don´t see an option for the print command, maybe I need to change the standard ratio for opening the figure window in Matlab? Or I fix this in the latex include (height=0.5625\linewidth,width=\linewidth), but than the plot is distorted... – NN123 Apr 23 '17 at 14:40
• I can use mwe example pictures for the first and the second include, but for the third there need to be a picture in the same folder and I can't figure out how mwe can help me with this issue; I hoped that it was clear that you will need to copy and rename any image in order to compile this – NN123 Apr 24 '17 at 5:54