在使用lineno
宏包来显示文档的行号时,会发现默认情况下,gather
align
等公式环境中的公式不会显示行号。如下所示:
\documentclass{ctexart}
\usepackage{amsmath}
\usepackage[mathlines]{lineno}
\usepackage{lipsum}
\linenumbers
\title{My Document}
\author{author}
\begin{document}
\maketitle
一段文字,下面是公式
\begin{align}
x & = 2\\
x & = 3 \quad \text{我是align环境,我没有行号}
\end{align}
这是一段文字
这是一段文字
这是一段文字
\begin{equation}
x=4 \quad \text{我是equation环境,我有行号}
\end{equation}
这是一段文字
这是一段文字
\begin{gather}
x = 2\\
x = 5 \quad \text{我是gather环境,我没有行号}
\end{gather}
eiusmod tempor incididunt ut labore et dolore
\end{document}
可以通过重新定义命令使这两个宏包进行patch,修改后的代码如下:
\documentclass{ctexart}
\usepackage{amsmath}
\usepackage[mathlines]{lineno}
%%%%%%%%%%%%%%%%%%%%
\usepackage{etoolbox} %% <- for \pretocmd, \apptocmd and \patchcmd
%% Patch \'normal\' math environment: (currently unused, but good to have)
% \newcommand*\linenomathpatch[1]{%
% \expandafter\pretocmd\csname #1\endcsname {\linenomath}{}{}%
% \expandafter\pretocmd\csname #1*\endcsname{\linenomath}{}{}%
% \expandafter\apptocmd\csname end#1\endcsname {\endlinenomath}{}{}%
% \expandafter\apptocmd\csname end#1*\endcsname{\endlinenomath}{}{}%
% }
%% Patch AMS math environment:
\newcommand*\linenomathpatchAMS[1]{%
\expandafter\pretocmd\csname #1\endcsname {\linenomathAMS}{}{}%
\expandafter\pretocmd\csname #1*\endcsname{\linenomathAMS}{}{}%
\expandafter\apptocmd\csname end#1\endcsname {\endlinenomath}{}{}%
\expandafter\apptocmd\csname end#1*\endcsname{\endlinenomath}{}{}%
}
%% Definition of \linenomathAMS depends on whether the mathlines option is provided
\expandafter\ifx\linenomath\linenomathWithnumbers
\let\linenomathAMS\linenomathWithnumbers
%% The following line gets rid of an extra line numbers at the bottom:
\patchcmd\linenomathAMS{\advance\postdisplaypenalty\linenopenalty}{}{}{}
\else
\let\linenomathAMS\linenomathNonumbers
\fi
% \linenomathpatch{equation} %% <- unnecessary, equation is already patched
\linenomathpatchAMS{gather}
\linenomathpatchAMS{multline}
\linenomathpatchAMS{align}
\linenomathpatchAMS{alignat}
\linenomathpatchAMS{flalign}
%%%%%%%%%%%%%%%%%%%%
\linenumbers
\title{My Document}
\author{author}
\begin{document}
\maketitle
一段文字,下面是公式
\begin{align}
x & = 2\\
x & = 3 \quad \text{我是align环境,看!我有行号了}
\end{align}
这是一段文字
这是一段文字
这是一段文字
\begin{equation}
x=4 \quad \text{我是equation环境,我有行号}
\end{equation}
这是一段文字
这是一段文字
\begin{gather}
x = 2\\
x = 5 \quad \text{我是gather环境,看!我有行号了}
\end{gather}
eiusmod tempor incididunt ut labore et dolore
\end{document}
参考链接:https://tex.stackexchange.com/questions/461186/how-to-use-lineno-with-amsmath-align