Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

This example shows how you can display mathematical expressions in your MATLAB® plots.

```
theta = 0:0.1:3*pi;
y1 = sin(theta);
y2 = cos(theta);
figure
plot(theta,y1)
hold on
plot(theta,y2)
```

You can use plain TeX math syntax to create math expressions to display in plot titles and legends. To indicate the start of a TeX expression use a backslash (\) such as '\theta' for the symbol .

legend({'sin(\theta)','cos(\theta)'}) % add legend title('Plot comparing sin(\theta) and cos(\theta)') % add title

You can use mathematical expressions in axis labels.

xlabel('\theta') % add label on x-axis ylabel('sin(\theta), cos(\theta)') % add label on y-axis

You can also use mathematical expressions to label individual ticks on an axis.

ax = gca; % get current axis ax.XTick = [0 pi 2*pi 3*pi]; % set x-axis ticks ax.XTickLabel = {'0','\pi','2\pi','3\pi'}; % set x-axis labels

MATLAB supports a basic subset of the LaTeX markup language. You can select which mathematical syntax you want to use by setting the `Interpreter`

property of a text object. Use `$$`

around the expression to indicate LaTeX *display math* mode.

theta = 0:0.01:2*pi; a = 1; b = 0.05; x = (a + b)*cos(theta) - b*cos((a + b)/b*theta); y = (a + b)*sin(theta) - b*sin((a + b)/b*theta); % figure clf plot(x,y) ylabel('$$y(\theta)=(a + b) sin(\theta) - b\,sin({a + b\over b }\theta)$$', ... 'Interpreter', 'Latex') xlabel('$$x$$','Interpreter', 'Latex')

LaTeX uses certain characters as operators. For example, the underscore (_) character is used to create a subscript. If you want to display text literally (no TeX evaluation), then you can set the `Interpreter`

property to 'none'.

clf text(0.5,0.5,'sin(\theta)', 'Interpreter', 'none')

If you want to display only part of the text literally, you can use backslash (\) to indicate characters that you do not want to have interpreted.

`text(0.5,0.25,'a_b vs a\_b')`

Was this topic helpful?