有些字体包含某些字形的风格变化。但似乎只有全局开关可以切换这些替代项,如手册中的以下示例所示fontspec
:
\fontspec{Linux Libertine}
\textsc{a} \& h \\
\addfontfeature{Alternate=0}
\textsc{a} \& h
是否可以仅对个别字形使用风格替代,例如仅对 '&' 字形使用?
答案1
对于您想到的具体示例,最简单的解决方案就是将其重新定义\&
为替代版本。对于 尤其如此\&
,因为它已经是一个宏。对于偶尔出现的其他字符,此方法也可能是最佳选择,即只需定义一个宏来引入替代版本。
有一个更复杂的版本,使用\XeTeXintercharclass
它可能有助于为更多字符选择替代字符。基本技术在 XeTeX 文档 ( texdoc xetex
) 中进行了解释。请注意,此解决方案将不是适用于 LuaLaTeX,它不支持这些 interchar 类。
我在以下示例文档中运用了这两种方法:
% !TEX TS-program = XeLaTeX
\documentclass{article}
\usepackage{fontspec}
\setmainfont[]{Linux Libertine O}
\begin{document}
This is an \& not alternate
% First the simple version
\let\oldand\&
\renewcommand{\&}{\begingroup\addfontfeature{Alternate=0}\oldand\endgroup}
This is the alternate \& after redefinition.
\renewcommand\&{\oldand}
This is back to the old \&
% Now the more complicated version:
\XeTeXinterchartokenstate=1 % enable character classes
\newXeTeXintercharclass\myalt % create a new class
\XeTeXcharclass `\& \myalt % add & to the class
% between any character of class 0 and \myalt add the alternate feature
\XeTeXinterchartoks 0 \myalt = {\begingroup\addfontfeature{Alternate=0}}
% between \myalt and any character end the group
\XeTeXinterchartoks \myalt 0 = {\endgroup}
% between a word boundary and \myalt add the alternate feature
\XeTeXinterchartoks 255 \myalt = {\begingroup\addfontfeature{Alternate=0}}
% between \myalt and the word boundary end the group
\XeTeXinterchartoks \myalt 255 = {\endgroup}
This is an \& alternate. And one in the middle of a word: A\&B.
\end{document}