Main Content

Insert Equations into the Live Editor

To describe a mathematical process or method used in your code, insert equations into your live script or function. Only text lines can contain equations. If you insert an equation into a code line, MATLAB® places the equation into a new text line directly under the selected code line.

Live script with text and two equations that describe the calculations being performed in the code below

There are two ways to insert an equation into a live script or function.

  • Insert an equation interactively — You can build an equation interactively by selecting from a graphical display of symbols and structures.

  • Insert a LaTeX equation — You can enter LaTeX commands and the Live Editor inserts the corresponding equation.

Insert Equation Interactively

To insert an equation interactively:

  1. Go to the Insert tab and click Equation.

    A blank equation appears.

    Live script in the Live Editor with a blank code line and a text box prompting you to enter your equation

  2. Build your equation by selecting symbols, structures, and matrices from the options displayed in the Equation tab. View additional options by clicking the drop-down arrow to the right of each section.

    When adding or editing a matrix, a context menu appears, which you can use to delete and insert rows and columns. You also can use the context menu to change or remove matrix delimiters.

  3. Format your equation using the options available in the Text section. Formatting is only available for text within the equation. Numbers and symbols cannot be formatted. The formatting option is disabled unless the cursor is placed within text that can be formatted.

Keyboard Shortcuts for Equation Editing

The equation editor provides a few shortcuts for adding elements to your equation:

  • To insert symbols, structures, and matrices, type a backslash followed by the name of the symbol. For example, type \pi to insert a π symbol into the equation. To discover the name of a symbol or structure, hover over the corresponding button in the Equation tab. You can also type backslash in the equation editor to bring up a completion menu of all supported names.

    Completion menu showing the list of supported names that appears after typing a backslash

    Note

    Although the \name syntax closely resembles LaTeX command syntax, entering full LaTeX expressions is not supported when inserting equations interactively.

  • To insert subscripts, superscripts, and fractions, use the symbols ‘_’, ‘^’ or ‘/’. For example:

    • Type x_2 to insert x2 into the equation.

    • Type x^2 to insert x2 into the equation.

    • Type x/2 to insert x2 into the equation.

  • To insert a new column into a matrix, type a ‘,’ at the end of the last cell in a matrix row. To insert a new row, type a semicolon ‘;’ at the end of the last cell in a matrix column.

  • To insert the common symbols listed in this table, type a combination of other symbols.

    Keyboard InputSymbolKeyboard InputSymbolKeyboard InputSymbol
    | |=>!=
    |=<-->!<
    |–<->!>
    –|<=!<=
    ->>=!>=
    <-<>  
    <--~=  

Insert LaTeX Equation

To insert a LaTeX equation:

  1. Go to the Insert tab, click Equation, and select LaTeX Equation.

  2. Enter a LaTeX expression in the dialog box that appears. For example, you can enter \sin(x) = \sum_{n=0}^{\infty}{\frac{(-1)^n x^{2n+1}}{(2n+1)!}}.

    The preview pane shows a preview of equation as it would appear in the live script.

    Edit Equation dialog box with LaTeX equation code, its corresponding preview, and a description of the equation in the Alt Text field

  3. To include a description of the LaTeX equation when exporting the live script to HTML, add text to the Alt Text field. For example, you can enter the text Maclaurin series for sin(x).

    The description specifies alternative text for the equation and is saved as an alt attribute in the HTML document. It is used to provide additional information for the equation if, for example, a user is using a screen reader.

  4. Press OK to insert the equation into your live script.

LaTeX expressions describe a wide range of equations. This table shows several examples of LaTeX expressions and their appearance when inserted into a live script.

LaTeX ExpressionEquation in Live Script
a^2 + b^2 = c^2a2+b2=c2
\int_{0}^{2} x^2\sin(x) dx02x2sin(x)dx
\sin(x) = \sum_{n=0}^{\infty}{\frac{(-1)^n x^{2n+1}}{(2n+1)!}}sin(x)=n=0(1)nx2n+1(2n+1)!
{a,b,c} \neq \{a,b,c\}a,b,c{a,b,c}
x^{2} \geq 0\qquad \text{for all}x\in\mathbf{R}x20for all xR
\matrix{a & b \cr c & d}abcd

Supported LaTeX Commands

MATLAB supports most standard LaTeX math mode commands. These tables show a list of supported LaTeX commands.

Non-ASCII Letters

SymbolLaTeX CommandSymbolLaTeX CommandSymbolLaTeX Command
øoœoeåaa
ØOŒOEÅAA
łlæaeßss
ŁLÆAE  

Greek/Hebrew Letters

SymbolLaTeX CommandSymbolLaTeX CommandSymbolLaTeX Command
αalphaνnuξxi
βbetaωomega ζzeta
χchiоomicronɛvarepsilon
δdeltaϕphiφvarphi
εepsilonπpi ϖvarpi
ηetaψpsiϱvarrho
vgamma ρrhoςvarsigma
ιiotaσsigma ϑvartheta
κkappaτtauאaleph
λlambda θtheta   
μmuυupsilon  
ΔDelta ΦPhiΘTheta
ΓGammaΠPi ϒUpsilon
ΛLambda ΨPsiΞXi
ΩOmega ΣSigma   

Operator Symbols

SymbolLaTeX CommandSymbolLaTeX CommandSymbolLaTeX Command
*ast±pmwr
starmpcap
·cdot⨿amalgcup
circodot uplus
bulletominus sqcap
diamondoplus sqcup
setminusoslash wedge, land
×timesotimesvee, lor
÷divbigoplustriangleleft
bot, perpbigotimestriangleright
topbigodotbigtriangleup
prodbiguplusbigtriangledown
coprodbigcapbigvee
sumbigcupbigwedge
int, intopointbigsqcup
intbar    

Relation Symbols

SymbolLaTeX CommandSymbolLaTeX CommandSymbolLaTeX Command
equiv <lt>gt
congle, leq ge, geq
neq, ne, not=not<not>
simprecsucc
simeqpreceqsucceq
approxllgg
asympsubsetsupset
doteqsubseteqsupseteq
proptosqsubseteqsqsupseteq
modelsmidin
bowtieparallelnotin
vdashiffni, owns
dashv    

Note

Some commands can be combined with the not command to create the negated version of the symbol. For example, \not\leq creates the symbol . The commands that can be combined include leq, geq, equiv, cong, approx, sim, simeq, models, ni, parallel, succ, succeq, prec, preceq, subset, supset, subseteq, supseteq, sqsubseteq, and sqsupseteq.

Arrows

SymbolLaTeX CommandSymbolLaTeX CommandSymbolLaTeX Command
leftarrowrightarrowuparrow
LeftarrowRightarrowUparrow
longleftarrowlongrightarrowdownarrow
LongleftarrowLongrightarrowDownarrow
hookleftarrowhookrightarrowupdownarrow
leftharpoondownrightharpoondownUpdownarrow
leftharpoonuprightharpoonupleftrightarrow
swarrownearrowLeftrightarrow
nwarrowsearrowlongleftrightarrow
mapstolongmapstoLongleftrightarrow

Brackets

SymbolLaTeX CommandSymbolLaTeX CommandSymbolLaTeX Command
{lbrace}rbrace|vert
[lbrack]rbrackVert
langlerangle\backslash
lceilrceil  
lfloorrfloor  
llbracketrrbracket  
SampleLaTeX CommandSampleLaTeX CommandSampleLaTeX Command
Sample big left bracebig, bigl, bigr, bigmSample bigg left bracebigg, biggl, biggr, biggm{abc}brace
Sample Big left braceBig, Bigl, Bigr, BigmSample Bigg left braceBigg, Biggl, Biggr, Biggm[abc]brack

Misc Symbols

SymbolLaTeX CommandSymbolLaTeX CommandSymbolLaTeX Command
infty forallwp
nablaexistsangle
partialemptysettriangle
Imıilozenge
Reȷjhbar
ellıimathprime
dots, ldots, hellipȷjmath¬lnot, neg
cdots:colonsurd
ddotscdotpgets
vdots.ldotpto
becausethereforevarpropto
°degreeclubsuitvarclubsuit
dag, daggerheartsuitvarheartsuit
ddag, ddaggerdiamondsuitvardiamondsuit
ldshspadesuitvarspadesuit
§mathsection    

Note

The exists command can be combined with the not command to create the negated version of the symbol. For example, \not\exists creates the symbol ∄.

Text Symbols

SampleLaTeX CommandSampleLaTeX CommandSampleLaTeX Command
£pounds, textsterling¿textquestiondownquotedblbase
¢textcent¡textexclamdownbackprime
texteuro«flqq, guillemotleft space
¥yen»frqq, guillemotrightpermil
®textregistered§S\backslash
trademark, texttrademarkP¦brokenvert
©copyright    

Accents

SymbolLaTeX CommandSymbolLaTeX CommandSymbolLaTeX Command
áacutea˙dota˜tilde
a¯bara¨ddotavec
abreveadddotàgrave
aˇcheckaddddota^hat

Functions

SampleLaTeX CommandSampleLaTeX CommandSampleLaTeX Command
arccosarccosdetdetlnln
arcsinarcsindimdimloglog
arctanarctanexpexpmaxmax
argarggcdgcdminmin
coscoshomhomPrPr
coshcoshkerkersecsec
cotcotlglgsinsin
cothcothlimlimsinhsinh
csccscliminfliminfsupsup
degdeglimsuplimsuptantan

Math Constructs

SampleLaTeX CommandSampleLaTeX CommandSampleLaTeX Command
abcxyzfracbastackrel, oversetabover
abcsqrtbaunderset[ab]overwithdelims
modabmod(ab)binom, chooseabcoverleftarrow
(moda)pmod(abcd)pmatrixabcoverrightarrow
abc^widehatabcdmatrixabcoverleftrightarrow
abc˜widetildeabcdbegin{array}ab¯cd¯hline
abc|bra{abcdbegin{cases}i=010limits
|abcket{a2|b2}left, middle, righti=010nolimits
ab|cdbraket    

Note

To create a matrix using the matrix and pmatrix commands, use the & symbol to separate columns, and \cr to separate rows. For example, to create a 2–by–2 matrix, use the expression \matrix{a & b \cr c & d}.

For large operators, MATLAB automatically adjusts the position of limits depending on whether or not there is text inline with the equation. To force the display of the limits above and below the operator, use the limits command. To force the display of the limits adjacent to the operator, use the \nolimits command. For example, the expression \sum\limits_{i=0}^{10} displays the limits of the summation operator above and below the operator. The expression \sum\nolimits_{i=0}^{10} displays the limits of the summation operator adjacent to the operator.

i=010i=010

White Space

SampleLaTeX CommandSampleLaTeX CommandSampleLaTeX Command
abnegthinspaceabcmathorda[bmathopen
abthinspaceabmathopa]bmathclose
abenspacea+bmathbina|bmathinner
abhspacea=bmathrelabquad
abkern, mkerna,bmathpunctabqquad

Text Styling

SampleLaTeX CommandSampleLaTeX CommandSampleLaTeX Command

Sample sum symbol in display style

displaystyleABCDEtext, textnormal, textupABCDEtexttt

Sample sum symbol in text style

textstyleABCDEbf, textbf, mathbfABCDEtextsf

Sample sum symbol in script style

scriptstyleABCDEit, textit, mathitACDcal, mathcal

Sample sum symbol in scriptscript style

scriptscriptstyleABCDErm, textrm, mathrmABDmathbb
  ABCDEhbox, mboxABDmathfrak

Related Topics

External Websites