因为后续在写数学公式的时候遇到了几个新问题,所以增补了这篇文章。
输入中文
我在整理财务公式时遇到了很多带中文的公式,所以公式里输入中文的问题就必须解决了。在公式中直接使用中文是会编译出稀奇古怪的符号的,如以下LaTex代码:
\documentclass[a4paper,UTF8]{article}
\usepackage{ctex}
\begin{document}
\begin{equation}
产权比率=\frac{净资产}{总负债}
\end{equation}
\end{document}
结果是这个样子的:
解决办法不是没有,只需要将中文用\mbox包起来就可以了,代码改一下:
\documentclass[a4paper,UTF8]{article}
\usepackage{ctex}
\begin{document}
\begin{equation}
\mbox{产权比率}=\frac{\mbox{总负债}}{\mbox{净资产}}
\end{equation}
\end{document}
公式就正常显示了:
无法换行
使用equation环境是无法换行的,如以下代码:
\documentclass[UTF8, a4paper,12pt]{article}
\usepackage{amsmath}
\begin{document}
\begin{equation*}
\lfloor\frac{21}{5}\rfloor=4\\
\newline
3\times5=15
\end{equation*}
\end{document}
虽然代码中使用了\\和\newline进行了换行,但是实际编译后公式并没有换行:
怎么办呢?使用amsmath包下的gather环境就好了,如以下代码:
\documentclass[UTF8, a4paper,12pt]{article}
\usepackage{amsmath}
\begin{document}
\begin{gather*}
\lfloor\frac{21}{5}\rfloor=4\\
\newline
3\times5=15
\end{gather*}
\end{document}
成功换行了:
水平对齐
上述例子里的gather是水平居中对齐,如果要靠左呢?这就需要用到align了。以下是居中对齐与靠左对齐的对比:
\documentclass[UTF8, a4paper,12pt]{article}
\usepackage{amsmath,ctex}
\begin{document}
一组等式:
\begin{align}
3\times5=15\\
3\times5+1=16
\end{align}
另一组等式:
\begin{gather}
3\times5=15\\
3\times5+1=16
\end{gather}
\end{document}
编译效果如下:
中括号语法
使用中括号语法,效果和两个$符号效果差不多,也是不能换行的。如下面的换行例子:
\documentclass[a4paper,UTF8]{article}
\usepackage{ctex,amsmath}
\begin{document}
公式例子:
\[
y=x^2+1\\
y=x^3+x^2+1
\]
\end{document}
编译后,并没有换行,所以这个简便的语法只能用来写一行公式:
特殊字符
小波浪线~在Latex数学公式中是用来做空格的,所以直接输入~是不会显示小波浪线的。这个时候只需要用\sim指令就可以了。
分段函数
在LaTex中使用cases环境来写分段函数,以下是一个例子:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation*}
f(x)=
\begin{cases}
x & x \ge 0\\
x^2 & x < 0
\end{cases}
\end{equation*}
\end{document}
效果如下:
f
(
x
)
=
{
x
x
≥
0
x
2
x
<
0
f(x)= \begin{cases} x & x \ge 0\\ x^2 & x < 0 \end{cases}
f(x)={xx2x≥0x<0