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.
If we save as the above file in pdf format, then we obtain the output shown below.
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.
- MiKTeX: MiKTeX is an implementation of LaTeX document preparing system. LaTeX is widely used by research scholars, students etc. to prepare documents.
- 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.
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.
Step 3: .dvi to .ps file
We use Texmaker again to get postscript (.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!