Matlab Figures. Remus M. Prunescu. LATEX Support Group. October 5, 2011. 1
Introduction. This tutorial explains how to import figures from Matlab using the ...
Matlab Figures Remus M. Prunescu LATEX Support Group October 5, 2011
1
Introduction
This tutorial explains how to import figures from Matlab using the matlabfrag function in conjunction with the pstool latex package. There are numerous solutions to achieve this, like printing to pdf from Matlab or using another dedicated function i.e. savefig but they lack in flexibility in terms of the font type and font size. More than that, if you wish to resize the figures in your document you will notice that all the text will modify its font size and sometimes the labels are difficult to read. Matlab function called matlabfrag in conjunction with the latex package pstool handles such issues and creates figures that use the same font type and size as in the master tex document even if the plots are resized afterwards.
2
Tutorial 1. Prerequisites: a) Download matlabfrag.m from: http://www.mathworks.com/matlabcentral/fileexchange/21286-matlabfrag b) Unzip the package to any destination folder e.g. SpecialFunctions. 2. In Matlab, capture the figures using the new installed function as in the below example: 1 2
clc, clear, close all %% Matlabfrag Example
3 4 5
% Add the special functions folder to Matlab's path addpath /home/remusmp/Documents/MATLAB/SpecialFunctions
6 7 8 9 10 11 12 13 14 15 16
t = 0:0.1:100; % A time vector Y1 = randn(length(t),1); % Dummy output 1 Y2 = 6 + randn(length(t),1); % Dummy output 2 figY = figure; % Figure handle plot(t, Y1, 'b', t, Y2, 'r', 'LineWidth', 2), grid; % Plot the outputs xlabel('Time [s]'); ylabel('Y [V]'); title('Dummy Outputs $Y 1$ and $Y 2$'); legend('$Y 1$', '$Y 2$'); % The '$' signs are required becasue the text will be typesetted by LaTeX.
17 18 19 20 21
U1 = randn(length(t),1); % Dummy output 1 U2 = 6 + randn(length(t),1); % Dummy output 2 figU = figure; % Figure handle plot(t, U1, 'b', t, U2, 'r', 'LineWidth', 2), grid; % Plot the outputs
1
2
2. TUTORIAL 22 23 24 25 26
xlabel('Time [s]'); ylabel('U [V]'); title('Dummy Outputs $U 1$ and $U 2$'); legend('$U 1$', '$U 2$'); ylim([-15 20]);
27 28 29 30
% Frag the figures and obtain an .eps and a .tex file for each one of them matlabfrag('NoisyOutput', 'handle', figY); matlabfrag('NoisyInput', 'handle', figU);
3. Add \usepackage{pstool} to your preamble. 4. Add the figures to your LATEX document like below: \begin{figure}[htbp] \centering \psfragfig[width=\textwidth]{matlab/NoisyOutput}{} \caption{Dummy Simulation} \label{fig:NoisyOutput} \end{figure} 5. You can also create subfigures with the subfigure package: \begin{figure}[htbp] \centering \subfigure[Noisy Inputs]{ \psfragfig[width=0.45\textwidth]{matlab/NoisyInput}{} \label{fig:NoisyInput1} } \subfigure[Noisy Inputs Clone]{ \psfragfig[width=0.45\textwidth]{matlab/NoisyInput}{} \label{fig:NoisyInput2} } \caption{Dummy Simulation} \label{fig:NoisyInput12} \end{figure} 6. Or with the memoir document class: \begin{figure}[htbp] \centering \subbottom[Noisy Inputs]{ \psfragfig[width=0.49\textwidth]{matlab/NoisyInput}{} \label{fig:NoisyInput1} } \subbottom[Noisy Inputs Clone]{ \psfragfig[width=0.49\textwidth]{matlab/NoisyInput}{} \label{fig:NoisyInput2} } \caption{Dummy Simulation} \label{fig:NoisyInput12} \end{figure}
3 The first plot is shown in figure 1 while the example with two figures next to each other is displayed in 2.
Dummy Outputs Y1 and Y2 10 Y1 Y2
8
6
Y [V]
4
2
0
−2
−4
0
10
20
30
40
50
60
70
80
90
100
Time [s] Figure 1: Dummy Simulation 7. The last thing to do is to add an option to the builder. On Windows machines you have to activate: --enable-write18
or --shell-escape
for Mac or Linux like systems.
3
Comments 1. Notice that the font size in figure 2 is the same as in figure 1 although the figures were scaled. If you wish to adjust the font size then it is recommended to do it in Matlab like in the below source code:
4
3. COMMENTS
20
Dummy Outputs U1 and U2 U1 U2
15 10
10
5
5
0
Dummy Outputs U1 and U2 U1 U2
15
U [V]
U [V]
20
0
−5
−5
−10
−10
−15 −15 0 10 20 30 40 50 60 70 80 90 100 0 10 20 30 40 50 60 70 80 90 100 Time [s] Time [s] (a) Noisy Inputs
(b) Noisy Inputs Clone
Figure 2: Dummy Simulation
xlabel(’Time[s]’, ’FontSize’, 8); ylabel(’Y[V]’, ’FontSize’, 8); set(gca, ’FontSize’, 8); The alternative would be to edit the .tex file generated alongside with the .eps file. The first line of the .tex file is shown next: \providecommand\matlabtextA{\color[rgb]{0.000,0.000,0.000}... ...\fontsize{10}{10}\selectfont\strut}
Change the font size to a smaller number if you wish to decrease the font size. Please edit the Matlab in case you wish to change the font size because, if you need to redo a figure, then the .tex file would be recreated and your font size changes from the .tex file will be erased. 2. It would be a good idea to have all your figures cropped in order to take advantage of all the whitespace in the width length of the text area. pstool uses by default crop=preview which is not removing necessarily the entire whitespace but the package can be loaded with the following option for a proper crop of the figure: \usepackage[crop=pdfcrop]{pstool} pdfcrop is another tool that needs to be installed first. On Windows platforms download and install the following additional software: a) Perl: http://www.activestate.com/activeperl/downloads b) Ghostscript: http://pages.cs.wisc.edu/~ghost/ Restart your Windows machine and compile the document. Latex should install pdfcrop automatically. 3. pstool is smart enough to figure out whether to process an image or not by looking at the creation date of the file. If the .eps and the .tex files are newer than the one that is added to the document (e.g. the matlab script was run meanwhile and the figure was updated) then the
5 tool knows that it has to process once again the .eps and the .tex files generated by matlabfrag. If you wish to override this behavior there are two more versions of psfragfig, one with a *, which forces the figure to be recreated and one with a ! that tells psfragfig to skip that figure: \psfragfig*[width=0.49\textwidth]{matlab/NoisyInput}{} % Remake figure \psfragfig![width=0.49\textwidth]{matlab/NoisyInput}{} % Skip figure 4. Another known issue is a conflict between pstool and siunitx. If you use siunitx package then make sure your preamble looks like below: \usepackage[crop=pdfcrop]{pstool}\EndPreamble \usepackage{siunitx} The \EndPreamble command tells pstool to stop reading the preamble till that spot.