带换行数学的左对齐文本

带换行数学的左对齐文本

我希望文本左对齐,内嵌数学公式,其换行方式与全对齐文本类似。LaTeX 似乎会在“合理”点(例如,在某些符号之间)换行,但当文本左对齐时,这种情况会发生变化。

左栏显示了我尝试的flushleft。右栏显示了所需的换行行为(如果我省略左对齐,就会发生这种情况)。我使用环境multicol只是为了使列宽更小。

我原本以为问题与列表环境有关,所以示例的第一行使用了一个itemize环境。下面的第二行表明,在没有列表环境的情况下,包装行为也发生了相同的变化。

\begin{document}
\begin{multicols}{2}
  \begin{flushleft}
    \begin{itemize}
    \item If $A \in \{B \times C \times G\}$ then $D \times E \times F =
      M + N + O$
    \end{itemize}
  \end{flushleft}
    \begin{itemize}
    \item If $A \in \{B \times C \times G\}$ then $D \times E \times F =
      M + N + O$
    \end{itemize}
\end{multicols}

\begin{multicols}{2}
  \begin{flushleft}
    If $A \in \{B \times C \times G\}$ then $D \times E \times F = M + N
    + O$
  \end{flushleft}
    If $A \in \{B \times C \times G\}$ then $D \times E \times F = M + N
    + O$
\end{multicols}
\end{document}

在此处输入图片描述

答案1

事实上,\raggedright在 LaTeX 中实现的方式(使用\@flushglue)在实践中永远不会允许在公式中间换行,因为这种实现告诉底层 TeX 引擎始终倾向于在两个单词之间换行独立距离右边距有多远。这种行为确实值得怀疑,因为短行实际上应该被视为“不好的”,而且在许多情况下,显然最好在某个合理的位置中断较长的行内公式,正如问题所建议的那样,而不是将该公式移到新行,在前一行中留下一个尴尬的空白间隙。另请参阅第 101 页关于如何设置“良好”的右侧不齐设置的讨论TeXbook,紧接着练习 14.17,以及\raggedright纯 TeX 中的定义(参见第 356 页)。

但是,定义一个“缓解”版本来\raggedright纠正这个缺陷很容易:以下代码将其定义为。请注意,和\mathmitigatedraggedright的值可能需要在“战场”上进行一些试验后进行微调;您可能还希望将(和)设置为,甚至设置为,而不是。调整这三个量的数值,直到您满意为止。\relpenalty\binoppenalty\@rightskip\rightskip\z@ \@plus 3em\z@ \@plus 2em\z@ \@plus 4em

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{lipsum}

\makeatletter

% This time, we make it independent:
\newcommand*\mathmitigatedraggedright{%
    \relpenalty   50 % adjust until satisfied
    \binoppenalty 70 % as above
    \@rightskip \z@ \@plus 4em \relax % on second thought, 4em should be enough
    \rightskip \@rightskip
    \spaceskip .33333em \xspaceskip .5em
    % Other settings from LaTeX's "\raggedright":
    \let\\\@centercr
    \leftskip\z@skip
    \parindent\z@
}

\makeatother

\begin{document}

To show the prevailing margins, we typeset a little of ``Lorem ipsum''.

\lipsum[1]

% No need for an environment: just limit the ragged-right setting within the 
% scope of a group...
\begingroup
    \mathmitigatedraggedright
    % \tracingparagraphs = 1 % uncomment to see line break calculations
    Text text text text text text text text text text
    text text text text text text text text text text.
    Some other words, in order to obtain a different line length,
    thus showing that a ragged-right setting is actually in force
    in this paragraph.
    Text text text text text text text text text text
    %text %text % uncomment last two words to see changes
    \( a+b+c+d+e = f+g+h \).\par % <- ... but do not forget to say "\par" 
                                 % **before** closing the group!
\endgroup

\end{document}

所有注释均已就位的输出为:

修改后的代码输出

添加

我认为flushleft也可以期望相应的环境变体;这更容易实现:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{lipsum}

\makeatletter

% This time, we make it independent:
\newcommand*\mathmitigatedraggedright{%
    \relpenalty   50 % adjust until satisfied
    \binoppenalty 70 % as above
    \@rightskip \z@ \@plus 4em \relax % on second thought, 4em should be enough
    \rightskip \@rightskip
    \spaceskip .33333em
    \xspaceskip .5em % questionable: what if "\frenchspacing" is desired?
    % Other settings from LaTeX's "\raggedright":
    \let\\\@centercr
    \leftskip\z@skip
    \parindent\z@
}

\makeatother

% Define the "environmental" version too, cf. flushleft:
\newenvironment*{mathmitigatedflushleft}{%
    \trivlist \mathmitigatedraggedright \item \relax
}{\endtrivlist}



\begin{document}

To show the prevailing margins, we typeset a little of ``Lorem ipsum''.

\lipsum[1]

% No need for an environment: just limit the ragged-right setting within the 
% scope of a group...
\begingroup
    \mathmitigatedraggedright
    % \tracingparagraphs = 1 % uncomment to see line break calculations
    Text text text text text text text text text text
    text text text text text text text text text text.
    Some other words, in order to obtain a different line length,
    thus showing that a ragged-right setting is actually in force
    in this paragraph.
    Text text text text text text text text text text
    %text %text % uncomment last two words to see changes
    \( a+b+c+d+e = f+g+h \).\par % <- ... but do not forget to say "\par" 
                                 % **before** closing the group!
\endgroup

% The mathmitigatedflushleft environment, however, also provides the usual 
% vertical spacing (and penalties!):

\begin{mathmitigatedflushleft}
    % \tracingparagraphs = 1 % uncomment to see line break calculations
    Text text text text text text text text text text
    text text text text text text text text text text.
    Some other words, in order to obtain a different line length,
    thus showing that a ragged-right setting is actually in force
    in this paragraph.
    Text text text text text text text text text text
    %text %text % uncomment last two words to see changes
    \( a+b+c+d+e = f+g+h \). 
\end{mathmitigatedflushleft}

\end{document}

相应的输出如下:

flushleft 代码的输出

相关内容