我正在使用该glossaries
软件包,并在第一次使用时将 修改\glsentryfmt
为斜体术语。但是,这会导致\Gls
和的输出\gls
相同。如果我删除该\renewcommand*{\glsentryfmt}{...}
位,则一切正常。我已阅读用户手册,但我不知道如何修复它。这可以修复吗?
\documentclass[a4paper,12pt]{report}
\usepackage[log-declarations=false]{xparse} % Hide some useless warning messages
\usepackage[quiet]{fontspec}
\defaultfontfeatures{Ligatures=TeX}
\setromanfont{URW Palladio L}
\setsansfont{Droid Sans}
\setmainfont[Mapping=tex-text]{Linux Libertine O}
\newfontface\amysipa{DejaVu Serif}
\usepackage[toc, acronym]{glossaries}
\renewcommand*{\glsentryfmt}{%
\ifglsused{\glslabel}{\glslabel}{\emph{\glslabel}}%
}
\newglossaryentry{wombat}
{
name={wombat},
description={blah blah blah}
}
\makeglossaries
\begin{document}
\gls{wombat} should be all lowercase.
\Gls{wombat} should begin with an uppercase letter, but it doesn't!
Wombat.
\printglossaries
\end{document}
输出如下:
答案1
尼古拉·塔尔博特 (Nicola Talbot) 或许会提供一个更优越的解决方案。(而且很可能是一个更安全的解决方案。)
请注意,我尽量简化您的示例,因为其中的大部分内容看起来并不相关。
\documentclass{report}
\usepackage[toc, acronym]{glossaries}
\renewcommand*{\glsentryfmt}{%
\ifglsused{\glslabel}{\begingroup}{\begingroup\em}\glsgenentryfmt\endgroup
}
\newglossaryentry{wombat}
{
name={wombat},
description={blah blah blah}
}
\makeglossaries
\begin{document}
\gls{wombat} should be all lowercase.
\Gls{wombat} should begin with an uppercase letter, but it doesn't!
Wombat.
\printglossaries
\end{document}
答案2
(继@cfr 的回答。)该\glsentryfmt
命令需要知道条目是如何引用的(是否使用了\gls
、\Gls
、\GLS
、\glspl
、\Glspl
或\GLSpl
) 。占位符命令引用条目的标签。这意味着您的代码\glsdisp
\glslabel
\renewcommand*{\glsentryfmt}{%
\ifglsused{\glslabel}{\glslabel}{\emph{\glslabel}}%
}
将显示标签如果该条目已被使用,并且强调标签第一次使用时。这将忽略有关单数、复数或大小写变化的所有要求。
这其他答案是一种快速的方法,效果很好,但这里有一个更详细的版本,展示了第一次使用时所做\glsgenentryfmt
的修改。(是插入命令的最后一个可选参数中的文本。例如)\emph
\glsinsert
\gls
's
The \gls{wombat}['s] hat.
\documentclass{article}
\usepackage{glossaries}
\renewcommand*{\glsentryfmt}{%
\ifdefempty\glscustomtext
{% not using \glsdisp
\glsifplural
{% using one of the plural forms (\glspl, \Glspl or \GLSpl)
\glscapscase
{% no-case changing (\glspl)
\ifglsused\glslabel
{% entry has been used so we want the "plural" field
\glsentryplural{\glslabel}\glsinsert
}%
{% entry hasn't been used so we want the "firstplural" field
% and add \emph
\emph{\glsentryfirstplural{\glslabel}\glsinsert}%
}%
}%
{% first-letter upper case (\Glspl)
\ifglsused\glslabel
{% entry has been used so we want the "plural" field with the
% first letter converted to upper case.
\Glsentryplural{\glslabel}\glsinsert
}%
{% entry hasn't been used so we want the "firstplural" field with the
% first letter converted to upper case and add \emph.
\emph{\Glsentryfirstplural{\glslabel}\glsinsert}%
}%
}%
{% all upper case (\GLSpl)
\ifglsused\glslabel
{% entry has been used so we want the "plural" field converted to
% upper case
\mfirstucMakeUppercase
{\glsentryplural{\glslabel}\glsinsert}%
}%
{% entry hasn't been used so we want the "firstplural" field
% converted to upper case (and add \emph)
\emph{\mfirstucMakeUppercase
{\glsentryfirstplural{\glslabel}\glsinsert}}%
}%
}%
}%
{% singular form (\gls or \Gls or \GLS)
\glscapscase
{% no case-changing (\gls)
\ifglsused\glslabel
{% entry has been used so we want the "text" field
\glsentrytext{\glslabel}\glsinsert
}%
{% entry hasn't been used so we want the "first" field
% (and add \emph)
\emph{\glsentryfirst{\glslabel}\glsinsert}%
}%
}%
{% first letter upper case (\Gls)
\ifglsused\glslabel
{% entry has been used so use "text" field with the first letter
% converted to upper case
\Glsentrytext{\glslabel}\glsinsert
}%
{% entry hasn't been used so use the "first" field with the first
% letter converted to upper case (and add \emph)
\emph{\Glsentryfirst{\glslabel}\glsinsert}%
}%
}%
{% all upper case (\GLS)
\ifglsused\glslabel
{% entry has been used, so use "text" field and convert to
% upper case
\mfirstucMakeUppercase{\glsentrytext{\glslabel}\glsinsert}%
}%
{% entry hasn't been used, so use "first" field and convert
% to upper case (and add \emph)
\emph{\mfirstucMakeUppercase{\glsentryfirst{\glslabel}\glsinsert}}%
}%
}%
}%
}%
{% \glsdisp was used. The text is supplied in \glscustomtext
\ifglsused\glslabel
{\glscustomtext\glsinsert}%
{\emph{\glscustomtext\glsinsert}}% add \emph on first use
}%
}
\makeglossaries
\newglossaryentry{wombat}
{
name={wombat},
description={blah blah blah}
}
\begin{document}
\gls{wombat} should be all lowercase.
\Gls{wombat} should begin with an uppercase letter, but it doesn't!
Wombat.
\printglossaries
\end{document}
这种格式化条目的复杂方法的主要原因是因为它使大小写转换尽可能接近需要大小写转换的实际文本,而远离在\makefirstuc
或 的参数中使用时可能会中断的变量格式要求\MakeTextUppercase
。
另一种可能性是利用以下事实:所有\gls
-like 和\glstext-like
命令都用于\glstextformat
格式化输入文本,并且占位符在使用\glslabel
时已经设置。这意味着可以重新定义为引用第一次使用并确定是否使用。例如:\glstextformat
\glstextformat
\emph
\documentclass{article}
\usepackage{glossaries}
\makeglossaries
\renewcommand{\glstextformat}[1]{\ifglsused\glslabel{#1}{\emph{#1}}}
\newglossaryentry{wombat}
{
name={wombat},
description={blah blah blah}
}
\begin{document}
\gls{wombat} should be all lowercase.
\Gls{wombat} should begin with an uppercase letter, but it doesn't!
Wombat.
\printglossaries
\end{document}
请注意,在这种情况下,字体更改也将应用于未设置或查询首次使用标志的命令。这意味着在下文中,和\glstext{wombat}
的第一次实例\gls{wombat}
均被强调:
\documentclass{article}
\usepackage{glossaries}
\makeglossaries
\renewcommand{\glstextformat}[1]{\ifglsused\glslabel{#1}{\emph{#1}}}
\newglossaryentry{wombat}
{
name={wombat},
description={blah blah blah}
}
\begin{document}
\glstext{wombat}.
\gls{wombat} should be all lowercase.
\Gls{wombat} should begin with an uppercase letter, but it doesn't!
Wombat.
\printglossaries
\end{document}
使用glossaries-extra
扩展包,可以进行改进\glsentryfmt
,以便只有某些类型的条目在第一次使用时使用特定的字体:
\documentclass{article}
\usepackage{glossaries-extra}
\makeglossaries
\renewcommand*{\glsentryfmt}{%
\ifglshasshort{\glslabel}{\glssetabbrvfmt{\glscategory{\glslabel}}}{}%
\glsifregular{\glslabel}%
{%
\glshasattribute{\glslabel}{firstfont}%
{%
\ifglsused{\glslabel}%
{%
\glsxtrregularfont{\glsgenentryfmt}%
}%
{%
\csname\glsgetattribute{\glslabel}{firstfont}\endcsname
{\glsxtrregularfont{\glsgenentryfmt}}%
}%
}%
{\glsxtrregularfont{\glsgenentryfmt}}%
}%
{%
\ifglshasshort{\glslabel}%
{\glsxtrgenabbrvfmt}%
{%
\glshasattribute{\glslabel}{firstfont}%
{%
\ifglsused{\glslabel}%
{%
\glsxtrregularfont{\glsgenentryfmt}%
}%
{%
\csname\glsgetattribute{\glslabel}{firstfont}\endcsname
{\glsxtrregularfont{\glsgenentryfmt}}%
}%
}%
{\glsxtrregularfont{\glsgenentryfmt}}%
}%
}%
}
\glssetcategoryattribute{animal}{firstfont}{emph}
\newglossaryentry{wombat}
{
name={wombat},
category=animal,
description={blah blah blah}
}
\newglossaryentry{quartz}
{
name={quartz},
category=mineral,
description={blah}
}
\begin{document}
\glstext{wombat}.
\gls{wombat} should be all lowercase.
\Gls{wombat} should begin with an uppercase letter, but it doesn't!
Wombat.
\gls{quartz}
\printglossaries
\end{document}
的重新定义\glsentryfmt
允许首次使用字体由属性提供firstfont
。在上面的例子中,我已将此属性设置emph
为animal
类别的,但不是类别的,因此强调了mineral
首次使用,但不是(也不是)\gls{wombat}
\gls{quartz}
\glstext{wombat}
我注意到您的 MWE 包含acronym
包选项,因此这里对示例进行了修改,以便它包含\emph
第一次使用时也使用的缩写(长格式和短格式)。
\documentclass{article}
\usepackage[acronym]{glossaries-extra}
\makeglossaries
\renewcommand*{\glsentryfmt}{%
\ifglshasshort{\glslabel}{\glssetabbrvfmt{\glscategory{\glslabel}}}{}%
\glsifregular{\glslabel}%
{%
\glshasattribute{\glslabel}{firstfont}%
{%
\ifglsused{\glslabel}%
{%
\glsxtrregularfont{\glsgenentryfmt}%
}%
{%
\csname\glsgetattribute{\glslabel}{firstfont}\endcsname
{\glsxtrregularfont{\glsgenentryfmt}}%
}%
}%
{\glsxtrregularfont{\glsgenentryfmt}}%
}%
{%
\ifglshasshort{\glslabel}%
{\glsxtrgenabbrvfmt}%
{%
\glshasattribute{\glslabel}{firstfont}%
{%
\ifglsused{\glslabel}%
{%
\glsxtrregularfont{\glsgenentryfmt}%
}%
{%
\csname\glsgetattribute{\glslabel}{firstfont}\endcsname
{\glsxtrregularfont{\glsgenentryfmt}}%
}%
}%
{\glsxtrregularfont{\glsgenentryfmt}}%
}%
}%
}
\glssetcategoryattribute{animal}{firstfont}{emph}
\newglossaryentry{wombat}
{
name={wombat},
category=animal,
description={blah blah blah}
}
\newglossaryentry{quartz}
{
name={quartz},
category=mineral,
description={blah}
}
\setabbreviationstyle[acronym]{long-short}
\newacronym{html}{HTML}{hypertext markup language}
\renewcommand{\glsfirstabbrvdefaultfont}[1]{\emph{#1}}
\renewcommand{\glsfirstlongdefaultfont}[1]{\emph{#1}}
\begin{document}
\glstext{wombat}.
\gls{wombat} should be all lowercase.
\Gls{wombat} should begin with an uppercase letter, but it doesn't!
Wombat.
\gls{quartz}
First use abbreviation: \gls{html}.
Next use: \gls{html}.
\printglossaries
\end{document}