使用 glossaries-extra 和 bib2gls 更改 longtab 词汇表中的标题文本?

使用 glossaries-extra 和 bib2gls 更改 longtab 词汇表中的标题文本?

我一直试图让我的符号列表按照我想要的方式显示,但尽管遵循了一些 其他问题/答案 在这个论坛上,并阅读额外词汇表bib2gls 文档,我在尝试重命名标题时总是遇到困难。

我有以下工作文件(使用 TeXLive 2020,LuaLaTeX 文档类、glossaries-extra v1.45、bib2gls v2.5、siunitx v2.8b):

\documentclass[draft,titles,a4paper]{LuaUUThesis}
\usepackage[english]{babel}
\usepackage[utf8]{luainputenc}
\usepackage{fontspec}
\usepackage{siunitx}

\usepackage[record,abbreviations,symbols,stylemods=longextra,nomain]{glossaries-extra}
\GlsXtrLoadResources[%
   src={acronyms},% 
   type=abbreviations]
\GlsXtrLoadResources[%
   src={symbols},% 
   type=symbols]

\begin{document}
\printunsrtglossary[%
   type=symbols,%
   style=long-name-sym-desc,%
   title={List of symbols}]
\printunsrtglossary[%
   type=abbreviations,%
   title={List of abbreviations}]

Example of symbols \gls{wavelength}, \gls{speed_of_light}.

Abbreviations: \gls{FTO}, \gls{TCO}.

\end{document}

以及缩写和符号 bib-files:

=== symbols.bib
@entry{speed_of_light,
  name = {\ensuremath{c}},
  short = {speed of light},
  description = {speed of light},
  text = {speed of light},
  long = {speed of light},
  symbol={\si{\metre\per\second}},
  first = {speed of light, \ensuremath{c}}
}
@entry{wavelength,
  name = {\ensuremath{\lambda}},
  description = {wavelength of electromagnetic radiation},
  text = {wavelength},
  long = {monochromatic wavelength},
  symbol={\si{\nano\metre}},
  first = {wavelength, \ensuremath{\lambda}}
}

=== abbreviations.bib
@entry{FTO,
  see = {TCO},
  name = {FTO},
  description = {fluoride-doped tin oxide, a very common optically transparent and electrically conducting oxide},
  text = {FTO},
  long = {fluoride-doped tin oxide},
  first = {fluoride-doped tin oxide (FTO)}
}
@entry{TCO,
  name = {TCO},
  description = {optically transparent and electrically conducting oxide},
  text = {TCO},
  long = {transparent conducting oxide},
  first = {transparent conducting oxide (TCO)}
}

这会生成一个漂亮的符号表和一个简单的缩写列表。 词汇表的屏幕截图

我的问题是我不知道如何更改符号标题中的文本。我应该注意,我希望继续使用长标签,因为我的词汇表可能会跨越多页。

我尝试了两种方法(下面我仅展示与上面相比有所改变的代码部分,希望不会造成混淆):

\renewcommand{\entryname}{Symbol}
\renewcommand{\symbolname}{Units}

但这并没有造成任何变化。不过,文档编译时没有错误。添加前缀\usepackage{glossary-longextra}没有任何区别。

接下来,我尝试定义一种新的词汇表样式,首先逐字复制代码其他答案,而当这些因微小变化而产生错误时:

\newglossarystyle{symbunitlong}{%
   \renewenvironment{theglossary}%
      {\begin{longtable}{lcl}}%
      {\end{longtable}}%
   \renewcommand*{\glossaryheader}{%
   \bfseries Symbol & \bfseries Units & \bfseries Description\\\hline}%
   \renewcommand{\glossentry}[2]{%
      \glsentryitem{##1}\glstarget{##1}{\glossentryname{##1}} &
      \glossentrysymbol{##1} &
      \glossentrydesc{##1}\\%
   }%
   \ifglsnogroupskip
     \renewcommand*{\glsgroupskip}{}%
   \else
     \renewcommand*{\glsgroupskip}{ & & & \tabularnewline}%
   \fi
}
\begin{document}
\printunsrtglossary[%
   type=symbols,%
   style=symbunitlong,%
   title={List of symbols}]

但这总是无法编译,并出现一个典型错误:

./thesis.tex:686: Extra alignment tab has been changed to \cr.
<recently read> \endtemplate              
l.686    title={List of symbols}] 

我仔细检查了代码,没有发现任何语法错误(我显然没有发现),我也尝试过删除\hline,或者\\\tabularnewline和 等替换,但还是没能解决问题(尽管错误消息extra \noalign有时会变为)。

为什么第一个更改标题文本的简单方法不起作用?我认为这种方法是由glossaries-extra?提出的。

是否有人对 glossarystyle 定义有建议、指示甚至修复,或者在使用 glossaries-extra 和 bib2gls 时有任何其他方法来编辑标题?如果我们可以用 booktabs 规则替换 header hlines,则可以获得加分。

为了完整起见,以防有人真的想重新编译这个 MWE :-),这里是.latexmkrc使用 LuaLaTeX 和 bib2gls 编译所需的文件和构建命令:

# https://www.ctan.org/tex-archive/support/latexmk/example_rcfiles
$pdflatex = 'lualatex -file-line-error %O %S';
$pdf_mode = 1;
$postscript_mode = $dvi_mode = 0;
# the following code based the example rcfile bib2gls
# http://mirror.ctan.org/support/latexmk/example_rcfiles/bib2gls_latexmkrc
# https://tex.stackexchange.com/a/401979/10824
push @generated_exts, 'glstex', 'glg';
add_cus_dep('aux', 'glstex', 0, 'run_bib2gls');
sub run_bib2gls {
   if ( $silent ) {
      my $ret = system "bib2gls --silent --group '$_[0]'";
   } else {
      my $ret = system "bib2gls --group '$_[0]'";
   };
    
   my ($base, $path) = fileparse( $_[0] );
   if ($path && -e "$base.glstex") {
      rename "$base.glstex", "$path$base.glstex";
   }

   # Analyze log file.
   local *LOG;
   $LOG = "$_[0].glg";
   if (!$ret && -e $LOG) {
      open LOG, "<$LOG";
       while (<LOG>) {
         if (/^Reading (.*\.bib)\s$/) {
            rdb_ensure_file( $rule, $1 );
         }
       }
       close LOG;
   }
   return $ret;
}
=== and compile the document with:
latexmk -r .latexmkrc -pdf -bibtex thesis.tex

答案1

我设法创建了一个可以解决我的问题的词汇表样式。bib2gls 示例目录Dickimaw 的词汇表库

除了弄清楚前导码之外,我还通过创建正确标记的“单位”字段使词汇表条目本身更易于使用。发布问题有时可以极大地激发人们解决问题的动力 :-)

\documentclass[draft,titles,a4paper]{LuaUUThesis}
\usepackage[english]{babel}
\usepackage[utf8]{luainputenc}
\usepackage{fontspec}
\usepackage{siunitx}

\usepackage[record,abbreviations,symbols,stylemods={longbooktabs},nomain]{glossaries-extra}
\GlsXtrLoadResources[%
   src={acronyms},% 
   type=abbreviations]
\GlsXtrLoadResources[%
   src={symbols},% 
   field-aliases={%
      unit=user1,%
      name=symbol},% because my doc is full of legacy \glssymbol
   % symbol sort should be defined *after* field-aliases
   symbol-sort-fallback={symbol},% sort by name field (alias symbol)
   type=symbols]

\setlength{\glsdescwidth}{.7\textwidth}
% define custom glossary style for symbols
\newglossarystyle{symbolswithunits}{% 
   \renewenvironment{theglossary}%
   {\begin{longtable}{lcp{\glsdescwidth}}}%
   {\end{longtable}}%
   \renewcommand*{\glossaryheader}{%
      \toprule 
      \bfseries Symbol &
      \bfseries Unit &
      \bfseries Description
      \tabularnewline\midrule\endhead
      \bottomrule\endfoot}%
   \renewcommand{\glossentry}[2]{%
      % glossary field: symbol (alias for name)
      \glsentryitem{##1}\glstarget{##1}{\glossentrysymbol{##1}} &
      % this picks up the unit field (which was aliased as user1 above)
      % glsletentryfield fetches the value associated with the field useri
      % and stores the result in the control sequence \thisunit
      \ifglshasfield{useri}{##1}{\glsletentryfield{\thisunit}{##1}{useri}\thisunit}{} &
      % glossary field: description
      \glossentrydesc{##1}\glspostdescription\tabularnewline
   }%
   % gap between letter groups (governed by nogroupskip package option):
   \ifglsnogroupskip
      \renewcommand*{\glsgroupskip}{}%
   \else
      \renewcommand*{\glsgroupskip}{ & & \tabularnewline}%
   \fi
}

\begin{document}

\printunsrtglossary[%
   type=symbols,%
   style=symbolswithunits,%
   title={List of symbols}]
\printunsrtglossary[%
   type=abbreviations,%
   title={List of abbreviations}]

Example of symbols: \gls{wavelength}, \gls{speed_of_light}.

Abbreviations: \gls{FTO}, \gls{TCO}.

\end{document}

这样就为符号列表实现了漂亮且描述性的标题(突出显示),同时保持了缩写列表的外观:

在此处输入图片描述

不过,我还是不明白为什么使用\renewcommand{\entryname}{Symbol}没有效果。如果有人想澄清这一点,我很乐意标记他们的答案而不是我的答案。

下面是符号条目现在的样子。我认为,这样就简洁多了,也更容易操作了:

@symbol{speed_of_light,
  name = {\ensuremath{c}},
  description = {speed of light},
  text = {speed of light},
  unit = {\si{\metre\per\second}},
  first = {speed of light, \ensuremath{c}}
}
@symbol{wavelength,
  name = {\ensuremath{\lambda}},
  description = {wavelength of electromagnetic radiation},
  text = {wavelength},
  unit = {\si{\nano\metre}},
  first = {wavelength, \ensuremath{\lambda}}
}

答案2

我遇到了同样的问题,但在软件包文档或这里找不到任何可行的解决方案。该问题是 babel 软件包的一个众所周知的问题。解决方案(我不明白,所以不要问我怎么做)是使用此行:\addto{\captionsenglish}{\renewcommand{\symbolname}{Symbol}}而不是简单的行来重新定义标题。希望这能有所帮助。

相关内容