默认情况下,内联数学会以这样的方式中断长表达式,即换行符发生后二元运算符,例如
a + b +
c + d
有没有简单的方法可以让 TeX 在二元运算符之前中断,从而得到
a + b
+ c + d
如果我不必手动在每个二元运算符上插入特殊指令那就更好了。
答案1
编辑使其+
仅在数学模式下处于活动状态,使用 egreg 的答案在数学模式下通过宏激活字符
当+
处于活动状态时,我使用\discretionary
来允许在所需的位置设置断点。
MWE 显示
当它到达边距时,它会在正确的位置中断。此外,这些术语正确地嵌套在边距边界上,
它在不休息时提供适当的数学空间
math-active不会搞乱尺寸标注
+
的使用。+
这是 MWE。
\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor,etoolbox}
\makeatletter
\newcommand{\DeclareMathActive}[2]{%
% #1 is the character, #2 is the definition
\expandafter\edef\csname keep@#1@code\endcsname{\mathchar\the\mathcode`#1 }
\begingroup\lccode`~=`#1\relax
\lowercase{\endgroup\def~}{#2}%
\AtBeginDocument{\mathcode`#1="8000 }%
}
\DeclareMathActive{+}{\mathbreak\std{+}}
\newcommand{\std}[1]{\csname keep@#1@code\endcsname}
\patchcmd{\newmcodes@}{\mathcode`\-\relax}{\std@minuscode\relax}{}{\ddt}
\AtBeginDocument{\edef\std@minuscode{\the\mathcode`-}}
\makeatother
\textwidth 1.75cm
\parindent 0pt
\parskip 1ex
\def\mathbreak{\discretionary{}{}{}}
\begin{document}
\hrulefill
\rule{1cm}{1pt}$ a + b + c + d$ \rlap{(broken spacing aligns to margins)}
$+ c$ \rlap{(for left-margin comparison)}
\mbox{\rule{1cm}{1pt}$ a + b + c + d$ \rlap{(unbroken spacing is proper)}}
Did + revert to original definition?\parskip=+30pt\relax
Yes it did
\end{document}
现在我已经对该方法进行了满意的测试,下面是更通用的设置:
\documentclass{article}
\usepackage{amsmath}
\usepackage{etoolbox}
\makeatletter
\newcommand{\DeclareMathActive}[2]{%
% #1 is the character, #2 is the definition
\expandafter\edef\csname keep@#1@code\endcsname{\mathchar\the\mathcode`#1 }
\begingroup\lccode`~=`#1\relax
\lowercase{\endgroup\def~}{#2}%
\AtBeginDocument{\mathcode`#1="8000 }%
}
\DeclareMathActive{+}{\mathbreak\std{+}}
\newcommand{\std}[1]{\csname keep@#1@code\endcsname}
\patchcmd{\newmcodes@}{\mathcode`\-\relax}{\std@minuscode\relax}{}{\ddt}
\AtBeginDocument{\edef\std@minuscode{\the\mathcode`-}}
\makeatother
\def\mathbreak{\discretionary{}{}{}}
\usepackage[nopar]{lipsum}
\begin{document}
\lipsum[4]. And now we will demonstrate the breaking ability
$a + b + c + d$ of the method. And here, $a + b + c + d$ when not broken.
\end{document}