%% Using Mathematical Expressions to Label and Annotate Plots
% This example shows how you can display mathematical expressions in your MATLAB® plots.
%
%% Creating a Plot

theta = 0:0.1:3*pi;
y1 = sin(theta);
y2 = cos(theta);

figure
plot(theta,y1)
hold on
plot(theta,y2)

%
%% Using Mathematical Expressions in Titles and Legends
% 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 \theta.

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

%
%% Using Mathematical Expressions In Axis and Tick Labels
% 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

%
%% Using More Elaborate Mathematical Expressions
% 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')

%
%% Displaying Text Literally
% 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')

%
% Copyright 2014 The MathWorks, Inc.