处理非对称规则的另一种方法

处理非对称规则的另一种方法

--通过某种机制,将其转换为 en-rule。在 Lua(La)TeX 中,是否可以使用相同的机制将--(左侧空格) 转换为–~(左侧空格),--将 (右侧空格) 转换为~–(右侧空格)?

目标~应为不间断空格,而不是波浪号。源应为任意空白(空格、制表符、换行符)。

这样做的目的是为了更合理地打破规则,使得规则插入(这种)不会脱离规则本身。

最终的样式可能不太寻常,但我确信答案可以相当容易地改变,以获得两侧都有空格的正常 en 规则或两侧都没有空格的 em 规则。对我来说,关键是避免在规则和插入的短语之间发生中断。在源代码中使用不对称规则是必要的,以指示哪条规则在左边,哪条规则在右边。

我希望这很清楚;如果没有,请看这个问题:

左右换行规则

答案1

在此处输入图片描述

这增加了可见[]空间以及不可破坏的空间,只是为了突出显示插入内容,调整宏以添加您需要的任何内容。

\documentclass{article}
\makeatletter
\directlua{
function dashes(s)
 return string.gsub(string.gsub(string.gsub(string.gsub(s,
  '^\@percentchar-\@percentchar-', ' --'),
  '\@percentchar-\@percentchar-$', '-- '),
   '(\@percentchar s+)\@percentchar-\@percentchar-','\@percentchar 1--\string\\leftdashspace '),
   '\@percentchar-\@percentchar-(\@percentchar s+)','\string\\rightdashspace --\@percentchar 1')
end
}
\makeatother
\def\startscan{%
\directlua{luatexbase.add_to_callback("process_input_buffer", dashes, "dashes")}}
\def\endscan{%
\directlua{luatexbase.remove_from_callback("process_input_buffer", "dashes")}}

\setlength\textwidth{300pt}
\begin{document}


The purpose of this would be to get more sensible breaking around
--rules-- such that rule insertions --this kind-- would not be separated
from their rules
The purpose of this would be to get --more sensible-- breaking around
rules, such that rule insertions --this kind-- would --not be separated--
from their rules
The purpose of this would be to get more sensible breaking around
rules, such that rule insertions --this kind-- would not be separated
from their rules

\bigskip

\newcommand\leftdashspace{\nolinebreak\hspace{3pt}{[}}
\newcommand\rightdashspace{{]}\nolinebreak\hspace{3pt}}

\startscan

The purpose of this would be to get more sensible breaking around
--rules-- such that rule insertions --this kind-- would not be separated
from their rules
The purpose of this would be to get --more sensible-- breaking around
rules, such that rule insertions --this kind-- would --not be separated--
from their rules
The purpose of this would be to get more sensible breaking around
rules, such that rule insertions --this kind-- would not be separated
from their rules

\endscan

\end{document}

相关内容