我想在感叹号、问号、冒号、分号等符号前添加额外的字距调整,并在短划线和长划线前后添加额外的字距调整。我当前的代码基于这回答。
唯一的问题是,它全局应用于所有字体系列,包括打字机字体系列,这并不理想。我如何才能将这种额外的字距调整应用于所有字体系列除了打字机家族?
这是我目前的代码:
\documentclass{book}
\usepackage{luacode, babel, fontspec, url}
\babelprovide[main, import]{english}
\babelfont[english]{tt}{Iosevka Slab}
\babelfont[arabic]{tt}{Amiri Typewriter}
\begin{luacode}
kval = "\\kern 0.05em"
function kern_dashes ( s )
s = string.gsub ( s , "([A-Za-z0-9])%-%-" , "%1"..kval.."--" )
s = string.gsub ( s , "%-%-([A-Za-z0-9])" , "--"..kval.."%1" )
s = unicode.utf8.gsub ( s, "([A-Za-z0-9])–" , "%1"..kval.."–" )
s = unicode.utf8.gsub ( s, "–([A-Za-z0-9])" , "–"..kval.."%1" )
s = unicode.utf8.gsub ( s, "([A-Za-z0-9])—" , "%1"..kval.."—" )
s = unicode.utf8.gsub ( s, "—([A-Za-z0-9])" , "—"..kval.."%1" )
s = unicode.utf8.gsub ( s, "([A-Za-z0-9])([!?:;])" , "%1"..kval.."%2" )
return s
end
\end{luacode}
\newcommand{\extrakernOn}{\directlua{
luatexbase.add_to_callback (
"process_input_buffer" , kern_dashes , "kern_dashes" )}}
\newcommand{\extrakernOff}{\directlua{
luatexbase.remove_from_callback (
"process_input_buffer" , "kern_dashes" )}}
\AtBeginDocument{\extrakernOn} % switch function on by default
% Macro to set the amount of extra kerning
\newcommand\kset[1]{\directlua{kval=\luastring{#1}}}
\begin{document}
Colon:
\url{https://tex.stackexchange.com}
\end{document}
该命令\extrakernOff
关闭了额外的字距调整。我尝试\ttfamily
使用以下代码重新定义该命令,以运行该命令来禁用字距调整。
\let\oldttfamily\ttfamily
\renewcommand{\ttfamily}{\oldttfamily\extrakernOff}
但它导致了以下错误信息。
Module luatexbase Error: No callback list for `process_input_buffer'
(luatexbase) on input line 3
stack traceback:
[C]: in function 'error'
/usr/share/texmf-dist/tex/latex/base/ltluatex.lua:110: in upvalue 'module_error
'
/usr/share/texmf-dist/tex/latex/base/ltluatex.lua:117: in upvalue 'luatexbase_e
rror'
/usr/share/texmf-dist/tex/latex/base/ltluatex.lua:675: in field 'remove_from_ca
llback'
[\directlua]:1: in main chunk.
\extrakernOff ..._input_buffer" , "kern_dashes" )}
l.3 \@nameuse{bbl@beforestart}
?
如果我使用\setmonofont
代替\babelfont[english]{tt}
并删除\babelfont[arabic]{tt}
,我将获得预期的结果。但我不想这样做。
答案1
编辑。我编辑了答案,以便更好地反映原帖者想要的内容。并修复了一个错误。
一个选项babel
是使用变换,特别是使用\babelprehyphenation
。这是一个 MWE:
\documentclass{book}
\usepackage{babel, url}
\babelprovide[main, import]{english}
\babelprehyphenation[label=punc-space]{english}{ [A-Za-z0-9] [!?:;] }
{ {}, % Keep the letter/digit
{ insert, penalty = 10000 }, % Insert a penalty
{ insert, space = .05 0 0, data = 1 }, % And a space
{} % Keep the punctuation
}
\begin{document}
Colon: \url{https://tex.stackexchange.com}
\disablelocaletransform{punc-space}
Colon: \url{https://tex.stackexchange.com}
\enablelocaletransform{punc-space}
Colon: \url{https://tex.stackexchange.com}
\end{document}
这space
是以 em 为单位的空格(取自第一个匹配的字符,这就是其data = 1
含义):第一个数字是正常值,第二个是plus
,第三个是minus
(它们是必需的,但可以设置为 0)。惩罚可防止换行。
它还展示了如何启用和禁用转换,但您需要最新版本babel
(>=3.85)。
其余规则可以类似地定义。
答案2
您可以使用 microtype 包来添加额外的字距。
\documentclass{book}
\usepackage{url}
\usepackage{microtype}
\newcommand{\applykerning}{%
\SetExtraKerning
{ encoding = *, % apply to all font encodings
family = *, % apply to all font families except "cmtt"
series = *, % apply to all font series
size = * % apply to all font sizes
}
{ ! = {0,.05em},
? = {0,.05em},
: = {0,.05em},
; = {0,.05em},
— = {0,.05em} } % adjust kerning values as needed
}
\newcommand{\resetkerning}{%
\SetExtraKerning
{ encoding = *, % apply to all font encodings
family = *, % apply to all font families except "cmtt"
series = *, % apply to all font series
size = * % apply to all font sizes
}
{ ! = {0,0},
? = {0,0},
: = {0,0},
; = {0,0},
— = {0,0} } % reset kerning values to default
}
\begin{document}
\applykerning
Colon: \url{https://tex.stackexchange.com}
\resetkerning
Colon: \url{https://tex.stackexchange.com}
\applykerning
Colon: \url{https://tex.stackexchange.com}
\texttt{Colon: \url{https://tex.stackexchange.com}}
\end{document}