為淺色字體定義新的 \XXseries 宏

為淺色字體定義新的 \XXseries 宏

是否可以定義一個新系列fontspec?我嘗試了以下方法,但是…

  1. 如果字體沒有淺色版本,它會拋出錯誤,而不是像\bfseries那樣替換字體。換句話說,沒有後退的餘地。
  2. 我的\ltseries未被 停用\mdseries
\documentclass{article}

\usepackage{fontspec}
\setmainfont{Source Sans Pro}
   % http://www.google.com/fonts/specimen/Source+Sans+Pro

\newcommand{\ltseries}{%
   \addfontfeatures{UprightFont={* Light},ItalicFont={* Light Italic}}%
}

\begin{document}
\ltseries
Light

\mdseries
Regular

\bfseries
Bold
\end{document}

如果發現\fontseries 和 \fontshape 的可能值但它沒有告訴我們如何定義一個新系列。此外,這是針對 LaTeX 的,它是 NFSS,而我將 XeTeX 與fontspec.

答案1

沒有官方介面。你可以這樣做(這是 lualatex-syntax,用於 xelatex 語法檢查,例如 eu1lmr.fd)。您應該意識到,它不能與本地字體更改\addfontfeatures或本地字體一起使用\fontspec,因為這通常會創建一個新系列。

\documentclass{article}

\usepackage{fontspec}
\setmainfont{Source Sans Pro}

\normalfont
\makeatletter
\DeclareFontShape{\f@encoding}{\f@family}{l}{n}%
     {<->    name:sourcesanspro-light:script=latn;+trep;+tlig;
      }{}

\DeclareFontShape{\f@encoding}{\f@family}{l}{it}%
     {<->    name:sourcesanspro-lightit:script=latn;+trep;+tlig;
      }{}
\makeatother

\DeclareRobustCommand{\ltseries}{%
\fontseries{l}\selectfont}

\begin{document}
\ltseries
Light

\mdseries
Regular

\bfseries
Bold

 \normalfont
 {\addfontfeatures{Ligatures=NoCommon} abc \ltseries abc}

\end{document}

答案2

在最新版本fontspec(撰寫本文時為 v2.5a)中,有一個官方介面聲明新的字體形狀:可以使用該FontFace選項來定義新的 NFSS 字體:

\documentclass{article}

\usepackage{fontspec}
\setsansfont[
   FontFace = {lt}{n}{SourceSansPro-Light},
   FontFace = {lt}{it}{SourceSansPro-LightIt},
]{Source Sans Pro}

\DeclareRobustCommand{\ltseries}{\fontseries{lt}\selectfont}
\DeclareTextFontCommand{\textsi}{\sishape}

\begin{document}
\ltseries
Light \textit{Italic} (not available) 

\mdseries
Regular \textit{Italic}

\bfseries
Bold \textit{Italic}

\sffamily
\ltseries
Light \textit{Italic}

\mdseries
Regular \textit{Italic}

\bfseries
Bold \textit{Italic}
\end{document}

這比使用舒服多了\DeclareFontShape。在範例中,我還添加了\textlt其他答案中缺少的定義。

答案3

Ulrikes 的答案在 XeTeX 方式更改後工作正常\DeclareFontShape,如下所示:

\documentclass{article}

\usepackage{fontspec}
\setsansfont{Source Sans Pro}
   % http://www.google.com/fonts/specimen/Source+Sans+Pro

\begingroup % \DeclareFontShape acts globally
\makeatletter
\sffamily

\DeclareFontShape{\f@encoding}{\f@family}{l}{n}%
     {<->    "[SourceSansPro-Light]:mapping=tex-text"
      }{}
\DeclareFontShape{\f@encoding}{\f@family}{l}{it}%
     {<->    "[SourceSansPro-LightIt]:mapping=tex-text"
      }{}
\endgroup % removes the effects of \sffamily and \makeatletter

\DeclareRobustCommand{\ltseries}{%
  \fontseries{l}\selectfont}

\makeatother

\begin{document}
\ltseries
Light (not available)

\mdseries
Regular

\bfseries
Bold

\sffamily
\ltseries
Light

\mdseries
Regular

\bfseries
Bold
\end{document}

之前\sffamily\DeclareFontShape有必要讓\f@family有正確的姓氏。在文件開始處有一個\normalfont所以\sffamily不要更改此處文檔的字體......

相關內容