主要内容

插入公式s 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.

插入公式Interactively

To insert an equation interactively:

  1. Go to theInserttab and clickEquation.

    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 theEquationtab. View additional options by clicking the drop-down arrowto 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 theTextsection. 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\pito insert a π symbol into the equation. To discover the name of a symbol or structure, hover over the corresponding button in theEquationtab. 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\namesyntax 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:

    • Typex_2to insert x 2 into the equation.

    • Typex^2to insert x 2 into the equation.

    • Typex/2to insert x 2 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 Input Symbol Keyboard Input Symbol Keyboard Input Symbol
    | | => !=
    | = <--> !<
    |– <-> !>
    –| <= !<=
    -> >= ! > =
    <- <>
    <-- ~=

Insert LaTeX Equation

To insert a LaTeX equation:

  1. Go to theInserttab, clickEquation, and selectLaTeX 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 theAlt Textfield. For example, you can enter the textMaclaurin series for sin(x).

    The description specifies alternative text for the equation and is saved as analtattribute 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. PressOKto 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 Expression Equation in Live Script
a^2 + b^2 = c^2 a 2 + b 2 = c 2
\int_{0}^{2} x^2\sin(x) dx 0 2 x 2 sin ( x ) d x
\sin(x) = \sum_{n=0}^{\infty}{\frac{(-1)^n x^{2n+1}}{(2n+1)!}} sin ( x ) = n = 0 ( 1 ) n x 2 n + 1 ( 2 n + 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} x 2 0 for all x R
\matrix{a & b \cr c & d} a b c d

Supported LaTeX Commands

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

Non-ASCII Letters

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

Greek/Hebrew Letters

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

Operator Symbols

Symbol LaTeX Command Symbol LaTeX Command Symbol LaTeX Command
* ast ± pm wr
star mp cap
· cdot ⨿ amalg cup
circ odot uplus
bullet ominus sqcap
diamond oplus sqcup
setminus oslash wedge,land
× times otimes vee,lor
÷ div bigoplus triangleleft
bot,perp bigotimes triangleright
top bigodot bigtriangleup
prod biguplus bigtriangledown
coprod bigcap bigvee
int,intop bigcup bigwedge
intbar sum bigsqcup
oint

Relation Symbols

Symbol LaTeX Command Symbol LaTeX Command Symbol LaTeX Command
equiv < lt > gt
cong le,leq ge,geq
neq,ne,not= not< not>
sim prec succ
simeq preceq succeq
approx ll gg
asymp subset supset
doteq subseteq supseteq
propto sqsubseteq sqsupseteq
models mid in
bowtie parallel notin
vdash iff ni,owns
dashv

Note

Some commands can be combined with thenotcommand to create the negated version of the symbol. For example,\not\leqcreates the symbol.

The commands that can be combined includeleq,geq,equiv,cong,approx,sim,simeq,models,ni,parallel,succ,succeq,prec,preceq,subset,supset,subseteq,supseteq,sqsubseteq, andsqsupseteq.

Arrows

Symbol LaTeX Command Symbol LaTeX Command Symbol LaTeX Command
leftarrow rightarrow uparrow
Leftarrow Rightarrow Uparrow
longleftarrow longrightarrow downarrow
Longleftarrow Longrightarrow Downarrow
hookleftarrow hookrightarrow updownarrow
leftharpoondown rightharpoondown Updownarrow
leftharpoonup rightharpoonup leftrightarrow
swarrow nearrow Leftrightarrow
nwarrow searrow longleftrightarrow
mapsto longmapsto Longleftrightarrow

Brackets

Symbol LaTeX Command Symbol LaTeX Command Symbol LaTeX Command
{ lbrace } rbrace | vert
[ lbrack ] rbrack Vert
langle rangle \ backslash
lceil rceil
lfloor rfloor
llbracket rrbracket
Sample LaTeX Command Sample LaTeX Command Sample LaTeX Command
Sample big left brace big,bigl,bigr,bigm Sample bigg left brace bigg,biggl,biggr,biggm { a b c } brace
Sample Big left brace Big,Bigl,Bigr,Bigm Sample Bigg left brace Bigg,Biggl,Biggr,Biggm [ a b c ] brack

Misc Symbols

Symbol LaTeX Command Symbol LaTeX Command Symbol LaTeX Command
infty forall wp
nabla exists angle
partial emptyset triangle
Im ı i lozenge
Re ȷ j hbar
ell ı imath prime
dots,ldots,hellip ȷ jmath ¬ lnot,neg
cdots : colon surd
ddots cdotp gets
vdots . ldotp to
because therefore varpropto
° degree clubsuit varclubsuit
dag,dagger heartsuit varheartsuit
ddag,ddagger diamondsuit vardiamondsuit
ldsh spadesuit varspadesuit
§ mathsection

Note

Theexistscommand can be combined with thenotcommand to create the negated version of the symbol. For example,\not\existscreates the symbol ∄.

Text Symbols

Sample LaTeX Command Sample LaTeX Command Sample LaTeX Command
£ pounds,textsterling ¿ textquestiondown quotedblbase
¢ textcent ¡ textexclamdown backprime
texteuro « flqq,guillemotleft space
¥ yen » frqq,guillemotright permil
® textregistered § S \ backslash
trademark,texttrademark P ¦ brokenvert
© copyright

Accents

Symbol LaTeX Command Symbol LaTeX Command Symbol LaTeX Command
á acute a ˙ dot a ˜ tilde
a ¯ bar a ¨ ddot a vec
a breve a dddot à grave
a ˇ check a ddddot a ^ hat

Functions

Sample LaTeX Command Sample LaTeX Command Sample LaTeX Command
arccos arccos det det ln ln
arcsin arcsin dim dim log log
arctan arctan exp exp max max
arg arg gcd gcd min min
cos cos hom hom Pr Pr
cosh cosh ker ker sec sec
cot cot lg lg sin sin
coth coth lim lim sinh sinh
csc csc lim inf liminf sup sup
deg deg lim sup limsup tan tan

Math Constructs

Sample LaTeX Command Sample LaTeX Command Sample LaTeX Command
a b c x y z frac b a stackrel,overset a b over
a b c sqrt b a underset [ a b ] overwithdelims
mod a bmod ( a b ) binom,choose a b c overleftarrow
( mod a ) pmod ( a b c d ) pmatrix a b c overrightarrow
a b c ^ widehat a b c d matrix a b c overleftrightarrow
a b c ˜ widetilde a b c d begin{array} a b ¯ c d ¯ hline
a b limits { a b c d begin{cases} { a 2 | b 2 } left,middle,right
a b c | bra | a b c ket a b | c d braket

Note

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

White Space

Sample LaTeX Command Sample LaTeX Command Sample LaTeX Command
a b negthinspace a b c mathord a [ b mathopen
a b thinspace a b mathop a ] b mathclose
a b enspace a + b mathbin a | b mathinner
a b quad a = b mathrel a b kern,mkern
a b qquad a , b mathpunct

Text Styling

Sample LaTeX Command Sample LaTeX Command Sample LaTeX Command

Sample sum symbol in display style

displaystyle ABCDE text,textnormal ABCDE texttt

Sample sum symbol in text style

textstyle A B C D E bf,textbf,mathbf A B C D E textsf

Sample sum symbol in script style

scriptstyle A B C D E it,textit,mathit A C D cal,mathcal

Sample sum symbol in scriptscript style

scriptscriptstyle ABCDE rm,textrm,mathrm A B D mathbb
ABCDE hbox,mbox A B D mathfrak

Related Topics

External Websites