![使用自定义环境时出现“\begin{align}ended by \end{eq}”错误](https://linux22.com/image/431193/%E4%BD%BF%E7%94%A8%E8%87%AA%E5%AE%9A%E4%B9%89%E7%8E%AF%E5%A2%83%E6%97%B6%E5%87%BA%E7%8E%B0%E2%80%9C%5Cbegin%7Balign%7Dended%20by%20%5Cend%7Beq%7D%E2%80%9D%E9%94%99%E8%AF%AF.png)
为什么此代码会出错?我怀疑这是环境的一些怪癖,有一个简单的解决方案,但我无法在线找到解决方案。可能我只是不知道要使用什么搜索词。
代码
请注意,我的真实定义eq
包含更多内容;这是最小限度的复制。还请注意,用 替换align
会使equation
代码编译,但当然,它不允许使用&
。
\documentclass{article}
\usepackage{amsmath}
\newenvironment{eq}
{\begin{align}}
{\end{align}}
\begin{document}
\begin{eq}
x + 1 = 2
\end{eq}
\end{document}
错误
! LaTeX Error: \begin{align} on input line 11 ended by \end{eq}.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.13 \end{eq}
我也尝试过使用
\documentclass{article}
\usepackage{amsmath}
\newenvironment{eq}
{\begin{equation}\begin{aligned}}
{\end{aligned}\end{equation}\ignorespacesafterend}
\begin{document}
Begin
\begin{eq}
x + 1 = 2
\end{eq}
End1 \\
End2
\end{document}
但我想知道为什么平原align
不起作用。
答案1
尝试这样做:
\documentclass{article}
\usepackage{amsmath}
\newenvironment{eq}
{\align}
{\endalign}
\begin{document}
\begin{eq}
x + 1 = 2
\end{eq}
\end{document}
大多数情况下,命令或环境定义中的环境必须像我上述的方式一样进行定义。