我通常会定义一些这样的命令
\newcommand{\paren}[#1]{\left(#1\right)}
使括号的使用更加灵活。现在,如果我在文档正文中这样做
\begin{align}
z = \paren{y\\+z}
\end{align}
我在环境的最后一行收到了一长串的抱怨align
,而我期望输出与以下代码相同,
\begin{align}
z = \left(y\\+z\right)
\end{align}
谁能告诉我如何使这种括号命令在换行符下保持强大?
\documentclass{article}
\usepackage{amsmath}
\newcommand{\paren}[1]{\left(#1\right)}
\begin{document}
% This works, but I don't like it.
\begin{align}
z = \left(y\right.\\
\left.+z\right)
\end{align}
% This is lovely, but does not work.
%\begin{align}
%z = \paren{y\\+z}
%\end{align}
% No complaints, of course, if the line break is removed
\begin{align}
z = \paren{y+z}
\end{align}
\end{document}
答案1
正如评论中所提到的,我们不推荐这种解决方案,而且这种解决方案会使代码的可读性大大降低。
话虽如此,但这是可以做到的。这里使用 Sebastien Gouezel 建议的技巧,引自mathtools
手册
\documentclass[a4paper]{memoir}
\usepackage{amsmath}
\newcommand\MTkillspecial[1]{% helper macro
\bgroup
\catcode`\&=9
\let\\\relax%
\scantokens{#1}%
\egroup
}
\newcommand\paren[1]{
\left(\vphantom{\MTkillspecial{#1}}\kern-\nulldelimiterspace\right.
#1
\left.\kern-\nulldelimiterspace\vphantom{\MTkillspecial{#1}}\right)
}
\begin{document}
\begin{align*}
A = {} & \paren{ \frac12 \\ & +3 }
\end{align*}
\end{document}
在这种情况下,我仍然会手动爬过栅栏,比如
\begin{align*}
A = {} & \biggl( \frac12
\\
& +3 \biggr)
\end{align*}
我认为这更具可读性。
答案2
该包提供了一个要放入序言中的mathtools
命令。\DeclarePairedDelimiter
\documentclass[a4paper]{minimal}
\usepackage{amsmath}
\usepackage{mathtools}
\DeclarePairedDelimiter{\paren}{\lparen}{\rparen}
\begin{document}
\begin{equation}
\begin{aligned}
\paren{a+\\
b = 3
}
\end{aligned}
\end{equation}
However, one needs to manually resize the delimiter.
\begin{equation}
\begin{aligned}
\paren[\bigg]{a+\\
b = 3
}
\end{aligned}
\end{equation}
\end{document}