Matplotlib 简明教程
Matplotlib - LaTeX for Mathematical Expressions
What is Rendering mathematical expressions?
在 LaTeX 中呈现数学表达式,需要使用 LaTeX 语法来编写数学方程、符号和公式。LaTeX 提供了一套全面的命令和符号,以便以精确和清晰的方式创建复杂的数学表达式。
Rendering mathematical expressions in LaTeX involves using LaTeX syntax to write mathematical equations, symbols and formulas. LaTeX provides a comprehensive set of commands and notation to create complex mathematical expressions with precision and clarity.
Importance of LaTeX for Mathematics
Precision and Clarity - LaTeX 允许准确排版数学符号和字符。
Precision and Clarity − LaTeX allows precise typesetting of mathematical notation and symbols.
Consistency - 维持数学文档排版的一致性。
Consistency − Maintains consistency in formatting across mathematical documents.
Publication-Quality - 产生高质量的数学表达式,适用于学术和科学出版物。
Publication-Quality − Produces high-quality mathematical expressions suitable for academic and scientific publications.
LaTeX’s - 对数学排版的支持,使其成为研究人员、数学家、科学家和学者在编写需要准确且格式良好的数学符号的技术或数学文档时优先考虑的选择。
LaTeX’s support for mathematical typesetting makes it a preferred choice for researchers, mathematicians, scientists and academics when writing technical or mathematical documents that require accurate and well-formatted mathematical notation.
LaTeX for Mathematical Expressions
以下是在数学表达式中 LaTex 的组成部分。
The below are the components of LaTex in Mathematical Expressions.
Inline Math Mode
LaTeX 中的内联数学模式用于在文档文本中包含数学表达式。
Inline math mode in LaTeX is used to include mathematical expressions within the text of a document.
我们可以通过一对单美元符号 $…$ 将数学表达式括起来使用内联数学模式。
We can use inline math mode by enclosing the mathematical expression between a pair of single dollar signs $…$.
Using the inline math mode
在这个例子中,数学表达式 \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
被使用内联数学模式包含在文本内。结果是该数学表达式在文本行内被呈现。
In this example the mathematical expression \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
is included within the text using inline math mode. The result is that the mathematical expression is rendered within the line of text.
The quadratic formula is given by $x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$.
执行上述代码时,您将获得以下输出 -
On executing the above code you will get the following output −
Display Math Mode
LaTeX 中的显示数学模式用于在独立模块中展示数学表达式,居中,并且有别于周围的文本。通常用于在文档中更重要的大型或独立方程。
Display math mode in LaTeX is used to showcase mathematical expressions in a separate block, centered and distinct from the surrounding text. It’s commonly used for larger or standalone equations that deserve prominence in a document.
要在 LaTeX 中使用显示数学模式,我们有若干选项,逐个来了解。
To use display math mode in LaTeX we have several options let’s see them one by one.
Double Dollar Sign …
将数学表达式括在 $$ 符号内,用于显示的方程。
Enclose the mathematical expression between $$ symbols for displayed equations.
在此例中,我们使用 .. 显示给定的输入方程。
In this example we are displaying the given input equation by using the ...
$$
f(x) = \int_{a}^{b} g(x) \, dx
$$
执行上述代码时,您将获得以下输出 -
On executing the above code you will get the following output −
‘equation’ Environment
使用 equation
环境创建编号方程。
Use the equation
environment to create a numbered equation.
\begin{equation}
f(x) = \int_{a}^{b} g(x) \, dx
\end{equation}
执行上述代码时,您将获得以下输出 -
On executing the above code you will get the following output −
Symbols and Operators
在 LaTeX 中,我们可以使用各种符号和运算符,来表示数学符号、表达式和运算。以下是一些常用的符号和运算符以及它们的 LaTeX 命令。
In LaTeX we can use a wide range of symbols and operators to represent mathematical notation, expressions and operations. Here are some commonly used symbols and operators along with their LaTeX commands.
-
Greek Letters − Alpha:
\alpha
, Beta:\beta
, Gamma:\gamma
, Delta:\delta
and so on. -
Arithmetic Operators − Plus:
+
, Minus:-
, Multiplication:\times
or*
, Division:\div
or/
-
Relations and Comparisons − Equals:
=
, Not equals:\neq
, Less than:<
, Greater than:>
and so on. -
Set Theory − Union:
\cup
, Intersection:\cap
, Subset:\subset
, Superset:\supset
and so on -
Calculus and Limits − Integral:
\int
, Summation:\sum
, Limit:\lim
, Derivative:\frac{dy}{dx}
-
Functions − Sine:
\sin
, Cosine:\cos
, Tangent:\tan
, Logarithm:\log
, Exponential:\exp
-
Roots and Exponents − Square root:
\sqrt{x}
, Exponent:x^2
, Subscript:x_1
, Superscript:x^i
-
Other Notations Fractions −
\frac{numerator}{denominator}
Matrices −bmatrix
,pmatrix
,vmatrix
, etc., using theamsmath
package Special Symbols − For example,\infty
for infinity,\emptyset
for an empty set, etc.
Example
在此示例中,我们使用 .. 显示 matplotlib 库 LaTex 中的符号和运算符。
In this example we are using the .., to display the symbols and operators in the LaTex of matplotlib library.
$$(\alpha + \beta = \gamma \times \delta)$$
执行上述代码时,您将获得以下输出 -
On executing the above code you will get the following output −
利用这些用于符号和运算符的 LaTeX 命令,我们可以在 LaTeX 文档中创建具有精准度和清晰度的复杂数学表达式。
By utilizing these LaTeX commands for symbols and operators we can create complex mathematical expressions with precision and clarity in our LaTeX documents.
Fraction and Subscript/Superscript
在 LaTeX 中,我们可以轻松地创建分数、下标和上标以使用特定命令和符号表示数学表达式。
In LaTeX we can easily create fractions, subscripts and superscripts to represent mathematical expressions using specific commands and notation.
我们可使用 \frac{numerator}{denominator}
命令创建分数。
To create fractions we can use the \frac{numerator}{denominator}
command.
在此示例中,我们创建分数 ¾。
In this example we are creating the fraction ¾.
The fraction is $\frac{3}{4}$.
执行上述代码时,您将获得以下输出 -
On executing the above code you will get the following output −
使用下标的“_”和上标的“^”可以添加下标和上标。
Subscripts and superscripts can be added using the ‘_’ for subscripts and ‘^’ for superscripts.
在此示例中,我们显示了一个脚本内容。
In this example we are displaying a script content.
$x_i^2$ denotes $x$ raised to the power of $i$ squared.
执行上述代码时,您将获得以下输出 -
On executing the above code you will get the following output −
我们还可以通过用花括号 {} 括起内容来嵌套下标和上标。
We can also nest subscripts and superscripts by enclosing the content in curly braces {}.
在此示例中,我们显示嵌套的下标。
In this example we are displaying the nested subscripts.
$x_{i_j}^{2k}$ represents a nested subscript and superscript.
执行上述代码时,您将获得以下输出 -
On executing the above code you will get the following output −
Using Commands
对于更复杂的表达式或为了确保一致的格式,我们可以使用 fixltx2e 等软件包提供的命令,如 \subscript{} 和 \superscript{} 。
For more complex expressions or to ensure consistent formatting we can use commands such as \subscript{} and \superscript{} provided by packages like fixltx2e.
在此示例中,我们显示复杂表达式。
In this example we are displaying the complex expressions.
$x \subscript{i} \superscript{2}$
执行上述代码时,您将获得以下输出 -
On executing the above code you will get the following output −
LaTeX 提供了创建分数、下标和上标的简单方法,让我们能够准确有效地表示数学表达式。
LaTeX offers straightforward ways to create fractions, subscripts and superscripts, allowing us to represent mathematical expressions accurately and efficiently.
Matrices and Arrays
LaTeX 中使用矩阵和数组以矩阵形式表示数据或显示方程组。 array 环境是 LaTeX 中创建矩阵和数组的基本结构,而 amsmath 软件包提供的 matrix 环境提供了额外的功能和更简单的矩阵语法。
In LaTeX matrices and arrays are used to represent data in matrix form or to display sets of equations. The array environment is the basic structure for creating matrices and arrays in LaTeX while the matrix environments provided by the amsmath package offer additional functionality and easier syntax for matrices.
Creating Matrices and Arrays
以下是如何使用各自的环境创建数组和矩阵。
Here are we are creating the arrays and matrices using respective environments.
‘array’ 环境允许我们在 LaTeX 中创建矩阵或数组。
The ‘array’ environment allows us to create matrices or arrays in LaTeX.
\[
\begin{array}{ccc}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{array}
\]
执行上述代码时,您将获得以下输出 -
On executing the above code you will get the following output −
Using ‘amsmath’ Package’s ‘matrix’ Environments
amsmath 包提供了 matrix, pmatrix, bmatrix, Bmatrix, vmatrix, Vmatrix 等方便的矩阵环境,简化了矩阵的创建。
The amsmath package provides convenient matrix environments such as matrix, pmatrix, bmatrix, Bmatrix, vmatrix, Vmatrix which simplify the creation of matrices.
\[
\begin{matrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{matrix}
\]
执行上述代码时,您将获得以下输出 -
On executing the above code you will get the following output −
Matrix Formatting
这里将使用 LaTeX 对齐矩阵的列。在矩阵或数组中,我们可以使用 c 表示居中、l 表示左对齐和 r 表示右对齐,在 array 环境中指定列对齐方式。
Here are going to align the columns of the matrix using the LaTex. In matrices or arrays we can specify column alignment using c for centered, l for left-aligned and r for right-aligned columns within the array environment.
下面是应用列对齐到矩阵的示例。
The below is the example of applying the column alignment on a matrix.
\[
\begin{array}{ccc}
1 & 222 & 3 \\
4 & 55555 & 6 \\
7 & 888 & 999999 \\
\end{array}
\]
执行上述代码时,您将获得以下输出 -
On executing the above code you will get the following output −
Additional Notes
-
Matrices and arrays in LaTeX are enclosed within the \[ … \] or equation environment to display them as standalone equations.
-
The & symbol separates elements within a row and \\ starts a new row.
LaTeX 为显示矩阵和数组提供了多功能工具,允许我们以各种对齐方式和配置以矩阵形式表示数学数据或方程。LaTeX 可以创建矩阵和数组以用于数学符号。
LaTeX provides versatile tools for displaying matrices and arrays allowing us to represent mathematical data or equations in matrix form with various alignments and configurations. LaTeX enables the creation of matrices and arrays for mathematical notation.
\begin{bmatrix}
1 & 2 \\
3 & 4
\end{bmatrix}
执行上述代码时,您将获得以下输出 -
On executing the above code you will get the following output −
Special Functions
LaTeX 支持特殊函数的符号,例如三角函数、对数等。
LaTeX supports notation for special functions like trigonometric functions, logarithms, etc.
$$
\sin(\theta), \log(x), \lim_{x \to \infty} f(x)
$$ Example of LaTeX Mathematical Expressions:
执行上述代码时,您将获得以下输出 -
On executing the above code you will get the following output −
Remove random unwanted space in LaTeX-style maths
LaTeX 会忽略您输入的空格,并按照数学文本中的方式使用空格。如果您想要不同的间距样式,可以使用以下四个命令
LaTeX ignores the spaces you type and uses spacing the way it’s done in mathematics texts. You can use the following four commands in case you want a different spacing style
-
\; − thick space
-
\: − medium space
-
\, − a thin space
-
\! − a negative thin space
若要在 matplotlib 图中消除 LaTeX 样式数学中的随机多余空格,可以使用 "\!",这将减少多余的间距。
To remove random unwanted space in LaTeX-style maths in matplotlib plot we can use "\!" which will reduce the extra spacing.
下面是应用列对齐到矩阵的示例。
The below is the example of applying the column alignment on a matrix.
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
plt.subplot(211)
plt.text(0.4, 0.4, r'$\sum_{n=1}^{\infty}\; \frac{-e^{i\pi}}{2^n}!\left[a^2+\delta ^2- \frac{\pi}{2} \right ]$', fontsize=16, color='r')
plt.title("With thick space")
plt.subplot(212)
plt.text(0.4, 0.4, r'$\sum_{n=1}^{\infty}\! \frac{-e^{i\pi}}{2^n}!\left[a^2+\delta ^2- \frac{\pi}{2} \right ]$', fontsize=16, color='r')
plt.title("With thin space")
plt.show()
执行上述代码时,您将获得以下输出 -
On executing the above code you will get the following output −
请注意“Σ(西格玛)”符号后的间距差异。在第一种情况下,我们使用了粗间距 (\;),在第二种情况下,我们使用了细间距 (\!) 来减少多余的间距。
Notice the difference in spacing after the "Σ (sigma)" symbol. In the first case, we have used thick space (\;) and in the second case, we have used the thin space (\!) to reduce extra spacing.