Method to obtain pdf with tight bounding box from Matlab figure

Matlab is very valuable for research scholars to carry out simulations and obtain results. These results, in form of figure(.fig file), have to be included in thesis, research paper etc.  As we can’t insert a .fig file directly in other documents, we have to first convert a .fig file to other formats (preferably pdf in research community). The native support by Matlab IDE to save as a .fig file to pdf results in a one page pdf which is not useful while including in other document.  In this article, I will describe a method to obtain pdf with tight bounding box from Matlab figure.

Native IDE Support

Before describing the method, I will first show the issue with the native support for conversion from .fig to pdf. A .fig file obtained from Matlab is shown below.

matlab .fig file conversion to pdf
Sample Matlab .fig file to show conversion to pdf

If we save as the above file in pdf format, then we obtain the output shown below.

Matlab .fig converstion to pdf
Pdf obtained after saving the .fig file as pdf using Matlab IDE

We can see that the pdf is one page and manually cropping is not a good solution.

Method to obtain Tight Bounding Box Pdf

Before describing the method, I will list out the required software tools.

  1. MiKTeX: MiKTeX is an implementation of LaTeX document preparing system. LaTeX is widely used by research scholars, students etc. to prepare documents.
  2. Texmaker: An IDE to prepare LaTeX documents.

I will now describe the steps to obtain tight bounding box pdf from Matlab .fig file.

Step 1: Save as .eps file

As a first step, save a .fig file as .eps file using Matlab IDE as shown below.

tight bounding box pdf from matlab figure
Step 1: .fig to .eps

Step 2: .eps to .dvi file

We will be using following LaTeX code to obtain .dvi file from .eps file.

\documentclass{article}
\usepackage{graphicx}
\pagestyle{empty}
\begin{document}
\begin{figure}
\includegraphics{step1-eps-file.eps}
\end{figure}
\clearpage
\end{document}

In the above code, we have used \pagestyle{empty} command to avoid page number at the bottom of the .dvi file. We will build the code using Texmaker with option ‘Latex’ to get .dvi file.

tight bounding box pdf from matlab figure
Step 2: .eps file to .dvi file conversion

Step 3: .dvi to .ps file

We use Texmaker again to get postscript (.ps file) from .dvi file.

tight bounding box pdf from matlab figure file
Step 3: .ps file from .dvi file

Step 4: .ps to epsi file

We will use ps2epsi tool to get epsi file from .ps file. You may be wondering why we have taken this circuitous route to get epsi file. This tool ps2epsi actually adjust the bounding box and make it tight. It requires a .ps file and due to non-availability of directly .fig to .ps conversion, we had to take such a long route. From where to get ps2epsi tool? Don’t worry! It comes shipped with MikTex and after installing MikTex, we can use it with the following command line:

ps2epsi infile.ps outfile.epi

Step 5: epsi file to pdf

The final step is to get pdf from epsi file. For that, we will use tool epstopdf (shipped with MikTeX) to perform the conversion. The command line is as follows for the conversion:

epstopdf outfile.epi

With the help of above steps, we will be able to get a pdf with tight bounding box from a Matlab figure. If you have any query/remarks/suggestions etc., then feel free to comment below. I will be more than happy to receive the feedback. All the best!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments