![如何将显示方程式(周围留有一些空间)放入内联?](https://linux22.com/image/468351/%E5%A6%82%E4%BD%95%E5%B0%86%E6%98%BE%E7%A4%BA%E6%96%B9%E7%A8%8B%E5%BC%8F%EF%BC%88%E5%91%A8%E5%9B%B4%E7%95%99%E6%9C%89%E4%B8%80%E4%BA%9B%E7%A9%BA%E9%97%B4%EF%BC%89%E6%94%BE%E5%85%A5%E5%86%85%E8%81%94%EF%BC%9F.png)
我想写一个方程,其行为类似于 inline-math,相对于水平对齐(即,方程的左边缘是它之前的文本结束的地方;而它之后的文本缩进,因此它从方程后面开始),但类似于 display-math,相对于垂直对齐(方程位于两行文本之间,数学是\displaystyle
)
我模糊地想要的是:
... in the middle of some paragraph explaining whatever maths is
happening so we get
T = EQUATION
such that this line continues
where we left off...
(我真的不知道该如何表达,希望大家还是能理解的)
答案1
这是一个令人惊讶的复杂问题,但却是可行的,并且解决方案将很好地展示 TeX 的显示机制。
这是该解决方案的整体概述:
- 首先,方程式预先组合在一个单独的框中,以便我们可以确定它的维度。
- 然后,我们尝试通过操作来确保方程有足够的空间
\parfillskip
。 - 我们将预先组合好的盒子放在普通的显示器中,并使用和参数手动设置其尺寸和
\displayindent
定位\displaywidth
。 - 一旦我们进入显示屏,就可以找到等式前面的线的长度
\predisplaysize
。 - 最后,我们以适当的缩进开始下一行。
\parfillskip
和 之间的相互作用\predisplaysize
尤其微妙:
- 首先,我们设置
\parfillskip
为等式的宽度,加上无限拉伸。这确保我们有足够的空间,但可能会导致线条过满。 - 为此,我们添加一个收缩等于等式的宽度。这可以防止行过满,但又会使我们无法保证有足够的空间。
- 现在,奇妙的是,当且仅当 中没有应用无限拉伸时, TeX 才会设置
\predisplaysize
为。因此,通过这个值,我们现在可以知道是否已经留出了足够的空间。\maxdimen
\parfillskip
完整的解决方案涉及更多的边缘情况,并仔细处理方程周围的间距:
\documentclass{article}
\usepackage{polyglossia, unicode-math}
\begin{document}
% extra horizontal and vertical spacing
\newskip \inlineEqHskip
\inlineEqHskip = 4pt plus 2pt
\newskip \inlineEqVskip
\inlineEqVskip = 4pt plus 2pt
% registers for storing intermediate results
\newdimen \inlineEqWidth
\newbox \inlineEqBox
\newenvironment{inline equation}
%
% code for the \begin of the environment
%
{\ifvmode % special case: no paragraph text preceding the equation
% compensate for the following \strut
\kern -\ht\strutbox
\kern -\dp\strutbox
% start a paragraph
\noindent
\kern -\inlineEqHskip
\fi
% spacing before the equation
\unskip \nobreak % prevent line breaks here
\hskip\inlineEqHskip % horizontal space
\strut % make previous spaces undiscardable
% store the equation in a box
\setbox\inlineEqBox = \hbox\bgroup
$\displaystyle} % start of box contents
%
% code for the \end of the environment
%
{$\egroup % close the box we opened above
\begingroup % assignments will be local by default
% adjust vertical spacing (note that we are inside a group)
\abovedisplayskip = \inlineEqVskip
\belowdisplayskip = \inlineEqVskip
% ensure there is enough space left for this equation:
% the ‘minus’ part prevents overfull line messages
\parfillskip = \wd\inlineEqBox plus 1 fill minus \wd\inlineEqBox
$$ % open the real equation so we can change the display spacing variables
\displaywidth = \wd\inlineEqBox % otherwise equal to the line width
\ifdim \predisplaysize < \maxdimen % true if we have enough room
\advance \predisplaysize by -2em % tex adds 2em by itself
\advance \displayindent by \predisplaysize
\else % edge case: the equation cannot fit on the line
% this causes the \parfillskip to have no infinite stretch applied,
% which makes TeX set \predisplaysize to \maxdimen
\predisplaysize = 0pt
\fi
% calculate the offset of the next line
\global \inlineEqWidth = \dimexpr
\wd\inlineEqBox + \predisplaysize + \inlineEqHskip \relax
% add the box we made and close the equation
\box \inlineEqBox $$
% start the next line
\strut \hskip \inlineEqWidth % horizontal space
\allowbreak % the equation may end a line
\endgroup \ignorespaces}
% example paragraphs
\hrule
\noindent
An example,
\begin{inline equation}
1 + 1 + 1 = 3,
\end{inline equation}
of an inline display equation.
\hrule
\noindent
At the end of a line, however, or with a very long equation,
\begin{inline equation}
1 + 1 + 1 + 1 + 1 = 5,
\end{inline equation}
you may run into trouble and, though we can detect this situation, it will not
look very nice.
\hrule
\begin{inline equation}
1 + 1 = 2
\end{inline equation}
Starting a paragraph with an inline display is also detectable, but hard to
find an appropriate solution for.
\hrule
\end{document}