How to Use Tex (latex Math Mode) Symbols in Legends and Labels in MATLAB Figures?
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
x = linspace(-3,9,10000);
hold on
plot(sin(x))
plot(cos(x))
hold off
legend( '$Sin(\delta)$' , '$Cos(\delta)$' , 'Interpreter' , 'latex' )
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
ax1=axes( 'Position' ,[.1 .1 .4 .4]);
ax2=axes( 'Position' ,[.53 .53 .4 .4]);
x = linspace(-3,3,10000);
plot(ax1,x,sin(x))
legend(ax1, '$Sin(\delta)$' , 'Interpreter' , 'latex' )
xlabel(ax1, '$\delta$' , 'Interpreter' , 'latex' )
ylabel(ax1, '$f(\delta)$' , 'Interpreter' , 'latex' )
plot(ax2,x,cos(x))
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
x = linspace(-3,3,10000);
plot(x,atan(x))
legend( '$tan^{-1}(\theta)$' , 'Interpreter' , 'latex' )
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
f = uifigure;
lab = uilabel(f, 'Position' ,[123 123 123 123]);
lab.Text = ('$$\frac{\partial^2 u}{{\partial}x^2} +
\frac{\partial^2 u}{{\partial}y^2} = 0$$');
lab.Interpreter= "Latex" ;
|
Output:
Last Updated :
09 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...