Greek Letters and Special Characters in Chart Text
You can add text to a chart that includes Greek letters and special characters using TeX markup. You also can use TeX markup to add superscripts, subscripts, and modify the text type and color. By default, MATLAB® supports a subset of TeX markup. To use additional special characters, such as integral and summation symbols, you can use LaTeX markup instead. This example shows how to insert Greek letters, superscripts, and annotations into chart text and explains other available TeX options.
Include Greek Letters
Create a simple line plot and add a title. Include the Greek letter in the title by using the TeX markup \pi
.
x = linspace(0,2*pi);
y = sin(x);
plot(x,y)
title('x ranges from 0 to 2\pi')
Include Superscripts and Annotations
Create a line plot and add a title and axis labels to the chart. Display a superscript in the title using the ^
character. The ^
character modifies the character immediately following it. Include multiple characters in the superscript by enclosing them in curly braces {}
. Include the Greek letters and in the text using the TeX markups \alpha
and \mu
, respectively.
t = 1:900; y = 0.25*exp(-0.005*t); figure plot(t,y) title('Ae^{\alphat} for A = 0.25 and \alpha = -0.0005') xlabel('Time') ylabel('Amplitude')
Add text at the data point where t = 300
. Use the TeX markup \bullet
to add a marker to the specified point and use \leftarrow
to include an arrow pointing to the left. By default, the specified data point is to the left of the text.
txt = '\bullet \leftarrow 0.25t e^{-0.005t} at t = 300';
text(t(300),y(300),txt)
TeX Markup Options
MATLAB supports a subset of TeX markup. Use TeX markup to add superscripts
and subscripts, modify the text type and color, and include special characters.
MATLAB interprets the TeX markup as long as the
Interpreter
property of the text object is set to
'tex'
(the default).
Modifiers remain in effect until the end of the text.
Superscripts and subscripts are an exception because they modify only the next character or the
characters within the curly braces. When you set the interpreter to 'tex'
,
the supported modifiers are as follows.
Modifier | Description | Example |
---|---|---|
^{ } | Superscript | 'text^{superscript}' |
_{ } | Subscript | 'text_{subscript}' |
\bf | Bold font | '\bf text' |
\it | Italic font | '\it text' |
\sl | Oblique font (usually the same as italic font) | '\sl text' |
\rm | Normal font | '\rm text' |
\fontname{ | Font name — Replace
with the name of
a font family. You can use this in combination with other modifiers. | '\fontname{Courier} text' |
\fontsize{ | Font size —Replace
with a numeric
scalar value in point units. | '\fontsize{15} text' |
\color{ | Font color — Replace
with one of
these colors: red , green ,
yellow , magenta ,
blue , black ,
white , gray ,
darkGreen , orange , or
lightBlue . | '\color{magenta} text' |
\color[rgb]{specifier} | Custom font color — Replace
with a
three-element RGB triplet. | '\color[rgb]{0,0.5,0.5} text' |
This table lists the supported special characters for the
'tex'
interpreter.
Character Sequence | Symbol | Character Sequence | Symbol | Character Sequence | Symbol |
---|---|---|---|---|---|
| α |
| υ |
| ~ |
| ∠ |
| ϕ |
| ≤ |
|
|
| χ |
| ∞ |
| β |
| ψ |
| ♣ |
| γ |
| ω |
| ♦ |
| δ |
| Γ |
| ♥ |
| ϵ |
| Δ |
| ♠ |
| ζ |
| Θ |
| ↔ |
| η |
| Λ |
| ← |
| θ |
| Ξ |
| ⇐ |
| ϑ |
| Π |
| ↑ |
| ι |
| Σ |
| → |
| κ |
| ϒ |
| ⇒ |
| λ |
| Φ |
| ↓ |
| µ |
| Ψ |
| º |
| ν |
| Ω |
| ± |
| ξ |
| ∀ |
| ≥ |
| π |
| ∃ |
| ∝ |
| ρ |
| ∍ |
| ∂ |
| σ |
| ≅ |
| • |
| ς |
| ≈ |
| ÷ |
| τ |
| ℜ |
| ≠ |
| ≡ |
| ⊕ |
| ℵ |
| ℑ |
| ∪ |
| ℘ |
| ⊗ |
| ⊆ |
| ∅ |
| ∩ |
| ∈ |
| ⊇ |
| ⊃ |
| ⌈ |
| ⊂ |
| ∫ |
| · |
| ο |
| ⌋ |
| ¬ |
| ∇ |
| ⌊ |
| x |
| ... |
| ⊥ |
| √ |
| ´ |
| ∧ |
| ϖ |
| ∅ |
| ⌉ |
| 〉 |
| | |
| ∨ |
| 〈 |
| © |
Create Text with LaTeX
By default, MATLAB interprets text using TeX markup. However, for more formatting options, you can use LaTeX markup instead.
For example, plot and draw a vertical line at . Add text to the graph that contains an integral expression using LaTeX markup. To show the expression in display mode, surround the markup with double dollar signs ($$
). When you call the text
function, set the Interpreter
property to 'latex'
.
x = linspace(0,3); y = x.^2.*sin(x); plot(x,y) line([2,2],[0,2^2*sin(2)]) str = '$$ \int_{0}^{2} x^2\sin(x) dx $$'; text(1.1,0.5,str,'Interpreter','latex')
Create Plot Titles, Tick Labels, and Legends with LaTeX
You can use LaTeX markup in plot titles, tick labels, and legends. For example, create a plot of a sine wave and a cosine wave.
x = -10:0.1:10; y = [sin(x); cos(x)]; plot(x,y)
Set the x-axis tick values to be multiples of pi
by calling the xticks
function. Then, call the gca
function to get the current axes, and set the TicklabelInterpreter
property to 'latex'
. Specify the tick labels using LaTeX markup. For inline expressions, surround the markup with single dollar signs ($
).
xticks([-3*pi -2*pi -pi 0 pi 2*pi 3*pi]) ax = gca; ax.TickLabelInterpreter = 'latex'; xticklabels({'$-3\pi$','$-2\pi$','$-\pi$','0', '$\pi$','$2\pi$','$3\pi$'});
Add a title that includes LaTeX markup by calling the title
function and setting the Interpreter
property to 'latex'
. Similarly, create a legend with labels that include LaTeX markup.
% Add title str = 'Estimates $\hat{\psi_1}$ and $\hat{\psi_2}$'; title(str,'Interpreter','latex') % Add legend label1 = '$\hat{\psi_1}$'; label2 = '$\hat{\psi_2}$'; legend(label1,label2,'Interpreter','latex')
See Also
text
| plot
| title
| xlabel
| ylabel