Lua Latex:如何在内联模式下打破集合中一长行元素?

Lua Latex:如何在内联模式下打破集合中一长行元素?

我有一个很长的 2-uple 元素列表,例如$A = \{ (1,2), (1,3), (1,5), (2,1), (2,2), (2,3), (1,2), (1,3), (1,5), (2,1), (2,2), (2,3),(1,2), (1,3), (1,5), (2,1), (2,2), (2,3),(1,2), (1,3), (1,5), (2,1), (2,2), (2,3),(1,2), (1,3), (1,5), (2,1), (2,2), (2,3), \}$使用内联数学模式。LuaLaTeX 不会分成多行。我如何才能强制它在不在元组内的逗号处分行,否则集合元素会超出页面?

提出的解决方案在内联数学模式中允许在“,”处换行吗?没用。似乎破坏了多语性。

答案1

这里有一种可能性,即使用名为 的命令,\mySpecialCommas仅在使用该命令时,逗号才会在数学模式下处于活动状态。当\mySpecialCommas在数学公式中使用时,逗号后允许换行,当且仅当逗号后跟有空格标记(通常可以从行尾获取)。因此,请确保元组中的逗号后没有空格。请注意示例中的最后一个逗号,除非您同意在行首出现右括号。这应该适用于任何引擎。

\documentclass{article}
\usepackage{lipsum}

\makeatletter
\newcommand*{\my@specialComma}{%
  \futurelet\next\my@specialComma@next
}

\newcommand*{\my@specialComma@next}{%
  \mathchar "613B             % the normal mathematical comma
  \ifx\next\@sptoken
    \penalty 0                % higher values make a line break less desirable
    \mskip 0mu plus 2mu\relax % add stretchability for better line breaks
  \fi
}

{\catcode`\,=\active
 \gdef,{\my@specialComma}%
}

\newcommand*{\mySpecialCommas}{\mathcode`\,="8000 } %space intended
\makeatother

\begin{document}

\lipsum*[1][1-3]
This is a long equation: $\mySpecialCommas
A = \{ (1,2), (1,3), (1,5), (2,1), (2,2), (2,3), (1,2), (1,3), (1,5), (2,1),
(2,2), (2,3),(1,2), (1,3), (1,5), (2,1), (2,2), (2,3),(1,2), (1,3), (1,5),
(2,1), (2,2), (2,3),(1,2), (1,3), (1,5), (2,1), (2,2), (2,3) \}$.

\medskip
This is a long equation: $\mySpecialCommas
A = \{ (1,2), (1,3), (1,5), (2,1), (2,2), (2,3), (1,2), (1,3), (1,5), (2,1),
(2,2), (2,3),(1,2), (1,3), (1,5), (2,1), (2,2), (2,3),(1,2), (1,3), (1,5),
(2,1), (2,2), (2,3),(1,2), (1,3), (1,5), (2,1), (2,2), (2,3),\}$.

\medskip
Normal text can have commas, no problem.

\end{document}

在此处输入图片描述

答案2

既然您提到您使用 LuaLaTeX,那么这里有一个解决方案,其工作原理是设置(a)一个 Lua 函数,\allowbreak如果逗号跟在)字符后面,则插入指令,以及(b)一个名为 的 LaTeX 前端宏\AllowBreaks

在此处输入图片描述

上图的垂直线表示文本块的边缘。

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{showframe}
\usepackage{luacode} % for '\luaexec' and '\luastring' macros
\luaexec{
function AllowBreaks ( s )
  tex.sprint ( ( s:gsub ( "\%)\%s-," , "),\\hspace{1sp}\\allowbreak" ) )  )
end
}
\newcommand\AllowBreaks[1]{\luaexec{AllowBreaks(\luastring{#1})}}

\newcommand\LongString{test test test $A = \{ (1,2), (1,3), (1,5), (2,1), (2,2), (2,3), (1,2), (1,3), (1,5), (2,1), (2,2), (2,3),(1,2), (1,3), (1,5), (2,1), (2,2), (2,3), (1,2), (1,3), (1,5), (2,1), (2,2), (2,3),(1,2), (1,3), (1,5), (2,1), (2,2), (2,3) \}$ test test test test}

\begin{document}
\LongString

\bigskip
\AllowBreaks{\LongString}
\end{document}

答案3

这个答案确实不是require lualatex,而是依靠 tokcycle包来遍历环境内容的标记流,并\allowbreak在括号组之外的逗号后添加。此外,为了模拟\sloppy输入的某些内容,它会添加一些带有空格的额外胶水以允许拉伸/压缩(您可以调整数量)。

\documentclass{article}
\usepackage[showframe,pass]{geometry}
\usepackage{tokcycle}
\Characterdirective{\ifx(#1\def\commaaction{}\else
  \ifx)#1\def\commaaction{\allowbreak}\fi\fi
  \addcytoks{#1}\ifx,#1\addcytoks[1]{\commaaction}\fi}
\Spacedirective{\addcytoks{\hspace{0pt plus 2pt minus 1pt}#1}}
\begin{document}
\tokencyclexpress
$A = \{ (1,2), (1,3), (1,5), (2,1), (2,2), (2,3), (1,2), (1,3), (1,5), (2,1), (2,2), (2,3),(1,2), (1,3), (1,5), (2,1), (2,2), (2,3),(1,2), (1,3), (1,5), (2,1), (2,2), (2,3),(1,2), (1,3), (1,5), (2,1), (2,2), (2,3), \}$
\endtokencyclexpress
\end{document}

在此处输入图片描述

相关内容