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.

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.

  2. Build your equation by selecting symbols, structures, and matrices from the options displayed in the Equation tab. View additional options by clicking the 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.

    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.

  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.

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±pmcap
starmpcup
·cdotamalguplus
circodot sqcap
bulletominus sqcup
diamondoplus wedge, land
setminusoslash vee, lor
×timesotimestriangleleft
÷divdaggertriangleright
bot, perpddaggerbigtriangleup
topwrbigtriangledown
sumprodint, intop
biguplusbigoplusbigvee
bigcapbigotimesbigwedge
bigcupbigodotbigsqcup

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

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

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  
SampleLaTeX CommandSampleLaTeX Command  
big, bigl, bigr, bigm{abc}brace  
Big, Bigl, Bigr, Bigm[abc]brack  
bigg, biggl, biggr, biggm(abc)choose  
Bigg, Biggl, Biggr, Biggm    

Misc Symbols

SymbolLaTeX CommandSymbolLaTeX CommandSymbolLaTeX Command
infty forallwp
nablaexistsangle
partialemptysettriangle
Imiħhbar
Rejprime
ellimath¬lnot, neg
dots, ldotsjmathsurd
cdots:colongets
ddotscdotpto
vdots.ldotp  

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 ∄.

Accents

SymbolLaTeX CommandSymbolLaTeX CommandSymbolLaTeX Command
áacutea¨ddota˜tilde
a¯bara˙dotavec
abreveàgrave  
aˇchecka^hat  

Functions

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

Math Constructs

SampleLaTeX CommandSampleLaTeX CommandSampleLaTeX Command
abcxyzfracaboverbastackrel, overset
abcsqrt[ab]overwithdelimsbaunderset
modabmodabcoverleftarrow(abcd)pmatrix
(moda)pmodabcoverrightarrowabcdmatrix
abc^widehatabcoverleftrightarrowabcdbegin{array}
abc˜widetildeablimits{abcdbegin{cases}
 left rightab¯cd¯hline

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}.

White Space

SampleLaTeX CommandSampleLaTeX CommandSampleLaTeX Command
negthinspaceabcmathorda[bmathopen
abthinspaceabmathopa]bmathclose
abenspacea+bmathbina|bmathinner
abquada=bmathrelabkern, mkern
abqquada,bmathpunct  

Text Styling

SampleLaTeX CommandSampleLaTeX CommandSampleLaTeX Command

displaystyleABCDEtext, textnormalABCDEtexttt

textstyleABCDEbf, textbf, mathbfABCDEtextsf

scriptstyleABCDEit, textit, mathitACDcal, mathcal

scriptscriptstyleABCDErm, textrm, mathrmABCDEhbox, mbox

Related Topics

External Websites