如何让所有冠词和后面的单词连在一起?

如何让所有冠词和后面的单词连在一起?

有没有办法自动使每个冠词与下一个单词连在一起,并且不被行分隔?除了每次手动执行之外,或者养成总是打字s/a /a~/gs/an /an~/g不是只是打字的习惯s/the /the~/a~a

答案1

这是一个基于 LuaLaTeX 的解决方案。它设置了一个 Lua 函数,该函数会在很早的阶段扫描输入,查找 (a) AAnThe后跟一个或多个空格字符和一个字母;(b) aanthe位于行首,后跟一个或多个空格字符和一个字母;以及 (c) 空格字符,后跟aanthe,后跟一个或多个空格字符和一个字母。每当出现匹配项时,空格就会被替换为“平局”,即字符~

请注意,此设置无法处理以下情况:冠词(aanthe)出现在一行的末尾,而相关名词出现在下一行的开头。

在此处输入图片描述

\documentclass{article}
\usepackage{luacode}
%% Lua-side code
\begin{luacode}
function auto_tie ( s )
  -- article begins with uppercase letter
  s = s:gsub ( "(The)%s+(%a)"   , "%1~%2" )
  s = s:gsub ( "(An?)%s+(%a)"   , "%1~%2" )
  -- article begins with lowercase letter, at beginning of line
  s = s:gsub ( "^(the)%s+(%a)"  , "%1~%2" )
  s = s:gsub ( "^(an?)%s+(%a)"  , "%1~%2" )
  -- article begins with lowercase letter & is preceded by space
  s = s:gsub ( "%s(the)%s+(%a)" , " %1~%2" )
  s = s:gsub ( "%s(an?)%s+(%a)" , " %1~%2" )  
  return s
end
\end{luacode}
%% LaTeX-side code: assign the Lua function to the 'process_input_buffer'
%% callback, so that it acts as a preprocessor
\AtBeginDocument{\directlua{luatexbase.add_to_callback (
   "process_input_buffer" , auto_tie , "autotie" )}}

\setlength\parindent{0pt}
\setlength\textwidth{1sp} % a rather minuscule line width, force line break whenever possible

\begin{document}
\obeylines% just for this example
A bit
An ant
The stop
\smallskip
a bit
an ant
the stop
\smallskip
  a bit
an ant
 the stop

\medskip
%% no ties in the any of the following cases:
Mega bit
Meghan Anderson
man ant
Berthe stop

\end{document}

相关内容