添加

添加

如何防止以下文本超出文档的边距?

\documentclass{article}

\begin{document}

We first left the DC loop open and inputted AC at frequencies of $\{50, 51, 67, 95, 102, 127, 147, 150\}$ Hz into the AC loop. Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text.

\end{document}

输出:

在此处输入图片描述

答案1

您可以定义如下命令:

\newcommand\mycomma{,\allowbreak}

然后写

We first left the DC loop open and inputted AC at a
frequencies of $\{50\mycomma 51\mycomma 67\mycomma 95\mycomma 102\mycomma 127\mycomma 147\mycomma 150\}$ Hz
into the AC loop.

这是一个使用逗号 catcode 的版本:

\begingroup
  \catcode`\,=\active
  \gdef,{\normalcomma\formattingcode}%
\endgroup

\newcommand\myspecialcommagroup[1]{%%
  \begingroup
   \let\normalcomma=,
   \def\formattingcode{\allowbreak}%%
   \catcode`\,=\active
   \scantokens{#1}%%
  \endgroup}

然后

We first left the DC loop open and inputted AC at a
frequencies of  \myspecialcommagroup{$\{50, 51, 67, 95, 102, 127, 147, 150\}$}

或者

We first left the DC loop open and inputted AC at a
frequencies of  $\{\myspecialcommagroup{50, 51, 67, 95, 102, 127, 147, 150}\}$

将产生

在此处输入图片描述

答案2

如果您接受需要手动调整的快速解决方案,则可以使用以下简单技巧:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\newcommand*{\allowinlinmathbreak}{\penalty \exhyphenpenalty}



\begin{document}

We first left the DC loop open and inputted AC at a frequencies of $\{50, 51,
67,\allowinlinmathbreak 95, 102, 127, 147, 150\}$ Hz into the AC loop.

\end{document}

更好的解决方案是在数学模式下使逗号处于活动状态并将其定义为在项目后插入逗号\penalty,但我现在没有时间写它(明天之前肯定会有人写的!)。


添加

好吧,最后我终于找到时间来发展我的想法:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\newcommand*{\allowinlinemathbreak}{\penalty \exhyphenpenalty}
\newcommand*{\allowinlinemathbreakatcommas}{\mathcode`\,="8000}
\newcommand*{\oldcomma}{} % to reserve the name
\edef\oldcomma{\the\mathcode`,}
\begingroup
\lccode`\~ = `\,
\lowercase{\endgroup
    \def~{\mathchar\oldcomma\allowinlinemathbreak}}



\begin{document}

We first left the DC loop open and inputted AC at a frequencies of
$\allowinlinemathbreakatcommas \{50, 51, 67, 95, 102, 127, 147, 150\}$
Hz into the AC loop.

The meaning of the comma is changed only locally:

We first left the DC loop open and inputted AC at a frequencies of
$\{50, 51, 67,\allowinlinemathbreak 95, 102, 127, 147, 150\}$
Hz into the AC loop.

\end{document}

无论如何,虽然我不得不承认你的问题确实是一个有趣的谜题,但我不能完全理解为什么你坚持在数学模式下将数字放在一个集合内,而不是简单地在普通文本中用逗号和空格分隔数字:这是一种表示测量单位“Hz”应该分布在它们之间的符号吗?

相关内容