Open In App

How to Use Tex (latex Math Mode) Symbols in Legends and Labels in MATLAB Figures?

Improve
Improve
Like Article
Like
Save
Share
Report

MATLAB provides options to add legends to plots and labels to figures with simple functions. Now, there are many cases when a user needs to display Latex symbols in the legends and label plots and figures. In this article, we shall see how to use Latex symbols in Legends and Labels in MATLAB figures. 

Latex Symbol in Legends of Figures/Plots:

See this article to learn how to add legends to axes in MATLAB. Now, to add Latex symbols in the legend, you need to add the latex code in $$ and then set the interpreter to latex.  

Syntax:

plot()

legend(‘$<curve-1>$,’$<curve-2$’,…,”$<curve-n>$’, ‘Interpreter’, ‘latex’)

See the following examples to understand it better. Let us plot a graph that calculates the sin and cos of a variable δ and add the same to legend and x, y labels.

Example 1:

Matlab




% MATLAB code for add Latex symbol 
% Horizontal axis range
x = linspace(-3,9,10000);    
% Plotting sin and cos on same graph
hold on
plot(sin(x))
plot(cos(x))
hold off
  
% Adding legend with latex symbols
legend('$Sin(\delta)$','$Cos(\delta)$','Interpreter','latex')
  
% Adding axes labels with latex symbols
xlabel('$\delta$','Interpreter','latex')
ylabel('$f(\delta)$','Interpreter','latex')


Output:

Legend with Latex symbol ‘δ’

We can add Latex legends with latex symbols to multiple axes as well. See the following implementation.

Example 2:

Matlab




% MATLAB code for add Latex legends 
% with latex symbols to multiple axes
% Defining multiple axes
ax1=axes('Position',[.1 .1 .4 .4]);
ax2=axes('Position',[.53 .53 .4 .4]);
  
% Creating horizontal range
x = linspace(-3,3,10000); 
  
% Plotting in axes 1
plot(ax1,x,sin(x))
  
% Adding latex symbols in legend in axes 1
legend(ax1,'$Sin(\delta)$','Interpreter','latex')
xlabel(ax1,'$\delta$','Interpreter','latex')
ylabel(ax1,'$f(\delta)$','Interpreter','latex')
  
% Plotting in axes 2
plot(ax2,x,cos(x))
  
% Adding latex symbols in legend in axes 1
legend(ax2,'$Cos(\delta)$','Interpreter','latex')
xlabel('$\delta$','Interpreter','latex')
ylabel('$f(\delta)$','Interpreter','latex')


Output:

 

Latex Symbols in Labels of Figures/Plots in MATLAB:

As seen in the above examples, we can add latex symbols in the legends. Now, we shall see how to add latex symbols in labels of plots and figure components with an example each. We shall first see how to add latex symbols to x and y labels in a graph figure.

Syntax:

xlabel(‘$<latex_label>$’, ‘Interpreter’, ‘latex’)

ylabel(‘$<latex_label>$’, ‘Interpreter’, ‘latex’)

We will now add the θ symbol in x label.

Example 3:

Matlab




% Latex Symbols in Labels of Figures/Plots in MATLAB
x = linspace(-3,3,10000); 
  
% Plotting inverse tangent function
plot(x,atan(x))
legend('$tan^{-1}(\theta)$','Interpreter','latex')
  
% Adding x and y labels with theta
xlabel('$\theta$','Interpreter','latex')
ylabel('$f(\theta)$','Interpreter','latex')


Output:

 

 

Now, we will add a label to a figure component with latex symbols included. For the presentation, we shall display the Laplace Equation on the label.

Example 4:

Matlab




% MATLAB code for label to a figure 
% component with latex symbols
% Creating an empty uifigure
f = uifigure;    
  
% Creating a label with position and size of 123x123
lab = uilabel(f,'Position',[123 123 123 123]);    
  
% Adding latex text to label
lab.Text = ('$$\frac{\partial^2 u}{{\partial}x^2} +
\frac{\partial^2 u}{{\partial}y^2} = 0$$');
  
% Changing the uilabel interpreter to latex
lab.Interpreter="Latex";


Output:

 



Last Updated : 09 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads