我正在使用以下代码来重新定义短字母,以便与我必须使用的特定 TrueType 字体(Elegant Garamond)一起使用,并且特定代码片段会生成错误(但仍然有效)并且在排版文档上生成输出。
\documentclass[10pt,a4paper,openany]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[romanian]{babel}
\usepackage{eg} %this is the definition for the font I use
\pdfmapfile{+eg.map}
\newcommand\chara{a}
\makeatletter
\def\kernbrevea#1{\kern#1\dimexpr0.47em-\strip@pt\fontdimen1\font\dimexpr-0.2em\relax\relax}
\makeatother
\begin{document} %begining here avoids one error that \catcode line below generates
\catcode`\ă=\active
\def ă{\raisebox{.0em}\u\kernbrevea{-}{\chara}}%
text with ă character
\end{document}
编译该文件时出现两个错误:
! Missing number, treated as zero.
<to be read again>
\protect
l.17 \catcode`\ă
=\active
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)
! Package inputenc Error: Keyboard character used is undefined
(inputenc) in inputencoding `utf8'.
See the inputenc package documentation for explanation.
Type H <return> for immediate help.
...
l.17 \catcode`\ă
=\active
You need to provide a definition with \DeclareInputText
or \DeclareInputMath before using this key.
输出是预期的“带有 ă 字符的文本”,但在此之前,在第一行,有一个字符串:“=,”。如屏幕截图所示:
有什么办法可以抑制它或者解决我遇到的错误吗?
我读过 TeXbook 中建议的部分,但我找不到其中的逻辑(好吧,我几乎不懂 TeX/LaTeX)。显然我的代码有问题,但我找不到问题出在哪里,也不知道我的字体是否存在问题……
答案1
您需要一个不同的结构来使用 pdftex 处理 UTF-8(但请参阅下面的不同解决方案):
\documentclass[10pt,a4paper,openany]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[romanian]{babel}
%\usepackage{eg} %this is the definition for the font I use
%\pdfmapfile{+eg.map}
\newcommand\chara{a}
\makeatletter
\def\kernbrevea#1{\kern#1\dimexpr0.47em-\strip@pt\fontdimen1\font\dimexpr-0.2em\relax\relax}
\makeatother
\DeclareUnicodeCharacter{0103}{\raisebox{.0em}\u\kernbrevea{-}{\chara}}
\begin{document}
text with ă character
\end{document}
然而,上述方法实际上仍然不是正确的方法:它将输入编码绑定到特定的字体编码,并且如您所见,如果文件编码为 latin1 或 latin2 而不是 UTF8,则会中断。此外,它没有解决字符的标准 latex 标记,\u{a}
更好的是只是声明\u{a}
为 raisebox 构造。然后\u{a}
将使用您的定义并ă
扩展为,\u{a}
这样将自动获得新定义,因此不需要上一个问题的 catcode 技巧\DeclareUnicodeCharacter
。
\documentclass[10pt,a4paper,openany]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[romanian]{babel}
%\usepackage{eg} %this is the definition for the font I use
%\pdfmapfile{+eg.map}
\makeatletter
\def\kernbrevea#1{\kern#1\dimexpr0.47em-\strip@pt\fontdimen1\font\dimexpr-0.2em\relax\relax}
\makeatother
%\DeclareTextComposite{\u}{T1}{a}{160}% T1 default
\DeclareTextCompositeCommand{\u}{T1}{a}{\raisebox{.0em}\u\kernbrevea{-}{a}}
\begin{document}
text with ă character and \u{a}
\end{document}