MATLAB: How to apply font options when using the LaTeX interpreter in MATLAB

formathelveticalatexMATLABplot

I am using the LaTeX interpreter to create plot labels in MATLAB. How can I change the font style of these labels?

Best Answer

Note that the LaTeX processor included in MATLAB is primarily designed for creating mathematical expressions, meaning that some functionality related to fonts or document processing is not supported. However, some LaTeX commands can still be used to provide a great deal of customization, as shown in the example below.
Executing the following code will generate the figure shown below:
>> figure
>> strx = ['$t$'];
>> stry = ['$\dot{x}(t)$'];
>> xlabel(strx,'FontSize',30,'Interpreter','LaTeX','Color','r') % Font size/color is set using 'FontSize' and 'Color' name-value pairs
>> ylabel(stry,'FontSize',30,'Interpreter','LaTeX','Color','b')
The font style of the labels can be changed by applying certain LaTeX commands to "strx" and "stry". Note that some commands cannot be applied to the whole string, as the '\dot' character is not compatible. The following code shows how to implement a portion of the available options in MATLAB:
% 1) Serif (default), no italics
strx = ['$\textrm{t}$'];
stry = ['$\dot{\textrm{x}}(\textrm{t})$'];
% 2) Sans-serif, no italics
strx = ['$\textsf{t}$'];
stry = ['$\dot{\textsf{x}}(\textsf{t})$'];
% 3) Bold, no italics
strx = ['$\textbf{t}$'];
stry = ['$\dot{\textbf{x}}(\textbf{t})$'];
% 4) Bold and italic
strx = ['$\textbf{\textit{t}}$'];
stry = ['$\dot{\textbf{\textit{x}}}(\textbf{\textit{t}})$'];
% 5) Underlined, italic
strx = ['$\underline{t}$'];
stry = ['$\underline{\dot{x}(t)}$'];
% 6) Underlined, sans-serif (removes italic)
strx = ['$\underline{\textsf{t}}$'];
stry = ['$\underline{\dot{\textsf{x}}(\textsf{t})}$'];
Note that the following warning (or similar) may be generated when using the sans-serif options:
Warning: Error updating Text.
Font cmss10 is not supported.
This occurs when using the LaTeX interpreter for sans-serif fonts and should not be of any concern. If you wish to suppress the display of this warning, please visit the link below: