latex
LaTeX form of symbolic expression
Syntax
Description
Examples
LaTeX Form of Symbolic Expressions
Find the LaTeX form of the symbolic expressions x^2 + 1/x
and sin(pi*x) + phi
.
syms x phi chr = latex(x^2 + 1/x)
chr = '\frac{1}{x}+x^2'
chr = latex(sin(pi*x) + phi)
chr = '\phi +\sin\left(\pi \,x\right)'
LaTeX Form of Symbolic Array
Find the LaTeX form of the symbolic array S
.
syms x
S = [sym(1)/3 x; exp(x) x^2]
S =
chr = latex(S)
chr = '\left(\begin{array}{cc} \frac{1}{3} & x\\ {\mathrm{e}}^x & x^2 \end{array}\right)'
LaTeX Form of Symbolic Matrix Variables
Perform computation using several symbolic matrix variables, and then find their LaTeX forms.
Create 3
-by-3
and 3
-by-1
symbolic matrix variables.
syms A 3 matrix syms X [3 1] matrix
Find the Hessian matrix of . Derived equations involving symbolic matrix variables appear in typeset as they would be in textbooks.
f = X.'*A*X
f =
H = diff(f,X,X.')
H =
Generate the LaTeX forms of the symbolic matrix variables f
and H
.
chrf = latex(f)
chrf = '{\textbf{X}}^{\mathrm{T}}\,\textbf{A}\,\textbf{X}'
chrH = latex(H)
chrH = '{\textbf{A}}^{\mathrm{T}}+\textbf{A}'
LaTeX Form of Symbolic Matrix Functions
Perform computation using symbolic matrix functions, and then find their LaTeX forms.
Create a 3
-by-1
symbolic matrix variable.
syms X [3 1] matrix
Create a symbolic matrix function that represents the formula .
syms f(X) [1 1] matrix keepargs f(X) = X.'*X
f(X) =
Find the derivative of with respect to .
Df = diff(f,X)
Df(X) =
Generate the LaTeX forms of the symbolic matrix functions f
and Df
.
chrf = latex(f)
chrf = '{\textbf{X}}^{\mathrm{T}}\,\textbf{X}'
chrDf = latex(Df)
chrDf = '2\,{\textbf{X}}^{\mathrm{T}}'
Modify Generated LaTeX with Symbolic Preferences
Modify generated LaTeX by setting symbolic preferences using the sympref
function.
Generate the LaTeX form of the expression with the default symbolic preference.
sympref("default");
chr = latex(sym(pi))
chr = '\pi '
Set the "FloatingPointOutput"
preference to true
to return symbolic output in floating-point format. Generate the LaTeX form of in floating-point format.
sympref("FloatingPointOutput",true);
chr = latex(sym(pi))
chr = '3.1416'
Now change the output order of a symbolic polynomial. Create a symbolic polynomial and set the "PolynomialDisplayStyle"
preference to "ascend"
. Generate the LaTeX form of the polynomial sorted in ascending order.
syms x; poly = x^2 - 2*x + 1; sympref("PolynomialDisplayStyle","ascend"); chr = latex(poly)
chr = '1-2\,x+x^2'
The preferences you set using sympref
persist through your current and future MATLAB® sessions. Restore the default values by specifying the "default"
option.
sympref("default");
Use LaTeX to Format Title, Axis Labels, and Ticks
For and from to , plot the 3-D surface . Store the axes object in a
by using gca
. Use latex
interpreter for the tick labels.
Create the -axis ticks by spanning the -axis limits at intervals of pi/2
. Convert the axis limits to precise multiples of pi/2
using round
and get the symbolic tick values in S
. Set the locations of the -axis ticks by using the xticks
function. Create the LaTeX labels for the -axis by using arrayfun
to apply latex
to S
and then concatenating $
. Display the labels by using the xticklabels
function.
Repeat these steps for the -axis. Set the - and -axes labels and the title using the latex
interpreter.
syms x y f = y*sin(x)-x*cos(y); fsurf(f,[-2*pi 2*pi]) a = gca; a.TickLabelInterpreter = "latex"; S = sym(a.XLim(1):pi/2:a.XLim(2)); S = sym(round(S/pi*2)*pi/2); xticks(double(S)); labels = "$" + arrayfun(@latex,S,UniformOutput=false) + "$"; xticklabels(labels); S = sym(a.YLim(1):pi/2:a.YLim(2)); S = sym(round(S/pi*2)*pi/2); yticks(double(S)) labels = "$" + arrayfun(@latex,S,UniformOutput=false) + "$"; yticklabels(labels); xlabel("$x$",Interpreter="latex"); ylabel("$y$",Interpreter="latex"); zlabel("$z$",Interpreter="latex"); titletext = "$" + latex(f) + "$ for $x$ and $y$ in $[-2\pi,2\pi]$"; title(titletext,Interpreter="latex")
Input Arguments
S
— Input
symbolic number | symbolic variable | symbolic vector | symbolic array | symbolic function | symbolic expression | symbolic matrix variable | symbolic matrix function
Input, specified as a symbolic number, variable, vector, array, function, expression, matrix variable, or symbolic matrix function.
Data Types: sym
| symfun
| symmatrix
| symfunmatrix
Version History
Introduced before R2006aR2022a: Generate LaTeX form of symbolic matrix functions
The latex
function accepts an input argument of type
symfunmatrix
. For an example, see LaTeX Form of Symbolic Matrix Functions.
R2021b: Generate LaTeX form of symbolic matrix variables
The latex
function accepts an input argument of type
symmatrix
. For an example, see LaTeX Form of Symbolic Matrix Variables.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)