我正在尝试定义一个新环境myeq
,它可以是equation
或multline
。我的用例是,在序言中,我将定义为复制或myeq
的功能,然后实际文档将仅包含环境,这样通过更改序言中的定义,就可以获得 或的间距特征。equation
multline
myeq
mutline
equation
具体来说,这要求我必须定义myeq
简单地复制multline
,如下所示:
\newenvironment{myeq}{\begin{multline}}{\end{multline}}
但是,这会产生以下错误(请参阅下面的 MWE)
输入行 8 上的 \begin{multline} 以 \end{myeq} 结束。
multline
如果我用 替换定义中的myeq
,错误就会消失equation
。
梅威瑟:
\documentclass{article}
\usepackage{amsmath}
\newenvironment{myeq}{\begin{multline}}{\end{multline}}
\begin{document}
\begin{myeq}
t
\end{myeq}
\end{document}
答案1
失败有一个技术原因,在texdoc technote
,第 6 节。
我建议类似
\iftrue
\newenvironment{myeq}{\equation}{\endequation}
\else
\newenvironment{\myeq}
{\def\eqbreak{\\}\multline}
{\endmultline}
\fi
\newcommand{\eqbreak}{}
用\eqbreak
而不是\\
(这实际上并没有被忽略equation
) 在 里面myeq
。
如果您需要两列文档,请将其更改\iftrue
为。\iffalse
multline
A possibly better implementation:
\documentclass[
% twocolumn
]{article}
\usepackage{amsmath,amssymb}
\makeatletter
\newenvironment{multiequation}
{%
\if@twocolumn
\def\eqbreak{\\}%
\expandafter\multline
\else
\def\eqbreak{}%
\expandafter\equation
\fi
}
{%
\if@twocolumn
\expandafter\endmultline
\else
\expandafter\endequation
\fi
}
\makeatother
\begin{document}
Some text before the equation to see what happens
and embed the equation into a real paragraph
which should break nicely into a few lines
\begin{multiequation}
\biggl(\int_{-\infty}^{\infty}e^{-x^{2}}\,dx\biggr)^{\!2}
\eqbreak
=\biggl(\int_{-\infty}^{\infty}e^{-x^{2}}\,dx\biggr)
\biggl(\int_{-\infty}^{\infty}e^{-y^{2}}\,dy\biggr)
\eqbreak
=\int\limits_{\mathbb{R}^{2}}e^{-x^{2}-y^{2}}\,dx\,dy
\end{multiequation}
and some text after the equation to see what happens
and embed the equation into a real paragraph
which should break nicely into a few lines
\end{document}
twocolumn
注释掉的输出
有源twocolumn
输出
除了本应用程序之外,multline
切勿将其用于单行显示。也许gather
,
\newenvironment{myeq}{\gather}{\endgather}
但你会面临如下风险:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
A paragraph that ends with a short line preceding a display,
let's see if we can
\begin{equation}
(a+b)^2=a^2+2ab+b^2
\end{equation}
A paragraph that ends with a short line preceding a display,
let's see if we can
\begin{gather}
(a+b)^2=a^2+2ab+b^2
\end{gather}
A paragraph that ends with a short line preceding a display,
let's see if we can
\end{document}
毫无疑问第二个很丑。
答案2
这是一个基于 LuaLaTeX 的方法,它充当预处理器:前TeX 的眼睛开始转动,Lua 函数switch_eq
扫描所有输入行,并根据布尔变量是真还是假,\begin{myeq}
用\begin{equation}
或替换所有 的实例。所有 的实例也是如此。\begin{gather}
Gather
\end{myeq}
该代码还提供了两个 LaTeX 宏,名为\GatherTrue
和\GatherFalse
,它们将布尔变量的值设置Gather
为 true 或 false。
在下面的截图中,请注意方程式 (1) 和 (3) 间隔很紧密,而 (2)——由gather
而不是由生成的方程式equation
——间隔很大。
% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{amsmath} % for "gather" environment
%% Lua-side code
\usepackage{luacode}
\begin{luacode}
Gather = false -- default value of "Gather" is "false"
function switch_eq ( buff )
if string.find ( buff , "\\begin{myeq}" ) then
if Gather == true then
return ( string.gsub ( buff, "\\begin{myeq}", "\\begin{gather}" ) )
else
return ( string.gsub ( buff, "\\begin{myeq}", "\\begin{equation}" ) )
end
elseif string.find ( buff , "\\end{myeq}" ) then
if Gather == true then
return ( string.gsub ( buff, "\\end{myeq}", "\\end{gather}" ) )
else
return ( string.gsub ( buff, "\\end{myeq}", "\\end{equation}" ) )
end
end
end
luatexbase.add_to_callback ( "process_input_buffer" , switch_eq , "switch_eq" )
\end{luacode}
%% TeX-side code
\newcommand\GatherTrue{\directlua{Gather = true}}
\newcommand\GatherFalse{\directlua{Gather = false}}
\setlength\textwidth{8cm} % just for this example
\begin{document}
----------------------------
\begin{myeq}
1+1=2
\end{myeq}
----------------------------
\GatherTrue
\begin{myeq}
1+1=2
\end{myeq}
----------------------------
\GatherFalse
\begin{myeq}
1+1=2
\end{myeq}
----------------------------
\end{document}