我正在尝试基于 scrbook 创建自定义 latex 类。我大量使用词汇表来表示我的数学符号,为此创建了 pdftool 提示,当鼠标指针悬停在上面时,会显示描述和单位。
因为我有一个详尽的词汇表条目列表,不仅仅是符号,还有首字母缩略词和常规条目,所以我想将它们存储在 csv 文件中并使用 datatool 包加载它们。
Atom 编辑器(我使用的)允许使用软件包自动完成用户定义的词汇表自动完成词汇表。但这需要创建 jobname.glsdef 文件。因此在文档环境的开头输入{glossary.tex}。
看来我对 \si 命令的扩展有点困惑。要么它只引用最新的 csv 条目,在本例中是克(出于说明目的需要伪造单位)。这可能是由于 \glssetnoexpandfield{unit} 命令造成的。但如果我将其设置为 \glssetexpandfield{unit},siunitx 就会开始出现“未定义的控制序列”的错误。
我猜我需要扩展 \units 命令,但不需要扩展实际的 \si 命令。但我似乎无法让它工作。
任何帮助深表感谢。
\documentclass[twoside,open=any]{scrbook}
\usepackage[automake, docdef=true]{glossaries-extra}
\usepackage{datatool}
\usepackage{pdfcomment}
\usepackage{tabu}
\usepackage{xparse}
\usepackage[
separate-uncertainty=true,
per-mode = symbol-or-fraction,
list-units = brackets,
range-units = brackets,
round-mode=places,
round-precision=1,
round-integer-to-decimal=true,
multi-part-units=single]{siunitx}
\usepackage{filecontents}
% glossary.tex file
\begin{filecontents}{glossary.tex}
\loadglssymbolcsv{symbols.csv}
\end{filecontents}
% Symbols.csv file
\begin{filecontents}{symbols.csv}
key,symbol,units,description
q,q,\si{\watt},heat flow
q_e,q_e,\si{\watt},space heat gain
q_x,q_x,\si{\watt},heat transfer rate
varepsilon_c,\varepsilon_c,,heating efficiency of a Carnot cycle
varepsilon_eff,\varepsilon_{eff},,effective cooling efficiency
varepsilon_th,\varepsilon_{th},,theoretical heating efficiency
varepsilon_w,\varepsilon_w,\si{gram},real heating efficiency
\end{filecontents}
% hooks for the preamble environment
\AtEndPreamble{
\loadglsentries{glossary.tex} % Loads the glossary
\makeglossaries % Makes the glossary
}
\glsdisablehyper % Disable hyperref, Needed to allow for pdf tooltip pop-up
% Add custom unit key
\glsaddkey{unit}{\glsentrytext{\glslabel}}{\glsentryunit}{\GLsentryunit}{\glsunit}{\Glsunit}{\GLSunit}
\newglossary[slg]{symbolslist}{sys}{syo}{Symbolslist}
% When a math symbol is called with \gls{sym-key}, the symbol is displayed in math mode and a pdf tooltip is generated, with a description and the units
\defglsentryfmt[symbolslist]{
\pdftooltip{\ensuremath{\glsgenentryfmt}}{\glsentrydesc{\glslabel}, given in: \glsentryunit{\glslabel}}
}
% Handles the th csv file with the entries
\NewDocumentCommand{\loadglssymbolcsv}{ m }{
\DTLifdbexists{symdata}{}{\DTLloaddb{symdata}{#1}}
\glssetexpandfield{name}
\glssetexpandfield{desc}
\glssetexpandfield{first}
\glssetexpandfield{firstplural}
\glssetexpandfield{text}
\glssetexpandfield{plural}
\glssetexpandfield{descplural}
\glssetnoexpandfield{unit} % Herein lies the complexity
% Itterate through the columns and generate a newglossaryentry
\DTLforeach*{symdata}%
{%
\key=key,%
\symbol=symbol,%
\units=units,%
\description=description%
}{%
\newglossaryentry{sym-\key}{name={\symbol},description={\description},unit={\units},type=symbolslist}%
}
}
%Custom symbolstyle
\newglossarystyle{symbolsliststyle}{
\renewenvironment{theglossary}
{
\tabulinesep=3pt
\noindent
\begin{longtabu}{X[1,c,m] X[6,l,m] X[1.5,c,m] X[2,r,m]}
}
{
\end{longtabu}
}
\renewcommand*{\glossaryheader}{
\rowfont{\sffamily\bfseries}
SYMBOL & DESCRIPTION & UNIT & PAGE \\\endhead
}
\renewcommand*{\glossentry}[2]{
\color{black}\ensuremath{\glossentryname{##1}} & \glossentrydesc{##1} & \ensuremath{\si{\glsentryunit{##1}}} & ##2 \\
}
\renewcommand*{\glsgroupheading}[1]{}
\renewcommand*{\glsgroupskip}{}
}
\begin{document}
\input{glossary.tex} % The glossaries needs to be loaded with in the document environment aas-wellbecause I need to glsdef file to be created for my auto-complete in Atom
% example equation
\begin{equation}
\gls{sym-q_e} = \gls{sym-varepsilon_eff} \times \gls{sym-q_x} + 10.
\end{equation}
% Print the symbol list
\printglossary[type=symbolslist,style=symbolsliststyle]
\end{document}
答案1
这是一个更好的方法:
\NewDocumentCommand{\loadglssymbolcsv}{ m }{
\DTLifdbexists{symdata}{}{\DTLloaddb{symdata}{#1}}%
\glsnoexpandfields
% Iterate through the columns and generate a newglossaryentry
\DTLforeach*{symdata}%
{%
\key=key,%
\symbol=symbol,%
\units=units,%
\description=description%
}{%
\edef\defnewentry{\noexpand\newglossaryentry{sym-\key}%
{name={\expandonce\symbol},
description={\expandonce\description},
unit={\expandonce\units},type=symbolslist}}%
\defnewentry
}%
}
它确保占位符命令只扩展一次。
其他一些小修正:
\makeglossaries
应该放在条目定义之前。- 注意行尾字符。它们可能会导致不必要的空格。您需要将它们注释掉。
完成 MWE:
\documentclass[twoside,open=any]{scrbook}
\usepackage{datatool}
\usepackage{pdfcomment}
\usepackage{tabu}
\usepackage{xparse}
\usepackage[
separate-uncertainty=true,
per-mode = symbol-or-fraction,
list-units = brackets,
range-units = brackets,
round-mode=places,
round-precision=1,
round-integer-to-decimal=true,
multi-part-units=single]{siunitx}
\usepackage[automake,
docdef=atom % see below
]{glossaries-extra}
\usepackage{filecontents}
% glossary.tex file
\begin{filecontents}{glossary.tex}
\loadglssymbolcsv{symbols.csv}
\end{filecontents}
% hooks for the preamble environment
\AtEndPreamble{
\makeglossaries % Opens the glossary files
\loadglsentries{glossary.tex} % Loads the glossary
}
% Symbols.csv file
\begin{filecontents}{symbols.csv}
key,symbol,units,description
q,q,\si{\watt},heat flow
q_e,q_e,\si{\watt},space heat gain
q_x,q_x,\si{\watt},heat transfer rate
varepsilon_c,\varepsilon_c,,heating efficiency of a Carnot cycle
varepsilon_eff,\varepsilon_{eff},,effective cooling efficiency
varepsilon_th,\varepsilon_{th},,theoretical heating efficiency
varepsilon_w,\varepsilon_w,\si{gram},real heating efficiency
\end{filecontents}
\glsdisablehyper % Disable hyperref, Needed to allow for pdf tooltip pop-up
% Add custom unit key
\glsaddkey{unit}{\glsentrytext{\glslabel}}{\glsentryunit}{\GLsentryunit}{\glsunit}{\Glsunit}{\GLSunit}
\newglossary[slg]{symbolslist}{sys}{syo}{Symbolslist}
% When a math symbol is called with \gls{sym-key}, the symbol is displayed in math mode and a pdf tooltip is generated, with a description and the units
\defglsentryfmt[symbolslist]{
\pdftooltip{\ensuremath{\glsgenentryfmt}}{\glsentrydesc{\glslabel}, given in: \glsentryunit{\glslabel}}
}
% Handles the th csv file with the entries
\NewDocumentCommand{\loadglssymbolcsv}{ m }{
\DTLifdbexists{symdata}{}{\DTLloaddb{symdata}{#1}}%
\glsnoexpandfields
% Iterate through the columns and generate a newglossaryentry
\DTLforeach*{symdata}%
{%
\key=key,%
\symbol=symbol,%
\units=units,%
\description=description%
}{%
\edef\defnewentry{\noexpand\newglossaryentry{sym-\key}%
{name={\expandonce\symbol},
description={\expandonce\description},
unit={\expandonce\units},type=symbolslist}}%
\defnewentry
}%
}
%Custom symbolstyle
\newglossarystyle{symbolsliststyle}{%
\renewenvironment{theglossary}
{%
\tabulinesep=3pt
\noindent
\begin{longtabu}{X[1,c,m] X[6,l,m] X[1.5,c,m] X[2,r,m]}
}%
{%
\end{longtabu}%
}%
\renewcommand*{\glossaryheader}{%
\rowfont{\sffamily\bfseries}
SYMBOL & DESCRIPTION & UNIT & PAGE \\\endhead
}%
\renewcommand*{\glossentry}[2]{%
\color{black}\ensuremath{\glossentryname{##1}} & \glossentrydesc{##1} & \ensuremath{\si{\glsentryunit{##1}}} & ##2 \\
}%
\renewcommand*{\glsgroupheading}[1]{}%
\renewcommand*{\glsgroupskip}{}%
}
\begin{document}
%\input{glossary.tex} % The glossaries needs to be loaded with in the document environment aas-wellbecause I need to glsdef file to be created for my auto-complete in Atom
% example equation
\begin{equation}
\gls{sym-q_e} = \gls{sym-varepsilon_eff} \times \gls{sym-q_x} + 10.
\end{equation}
% Print the symbol list
\printglossary[type=symbolslist,style=symbolsliststyle]
\end{document}
关于需要创建.glsdefs
用于 atom 自动完成的文件,你现在可以(从glossaries-extra
版本 1.34,2018-07-29)使用包选项docdef=atom
,其行为类似docdef=restricted
,但也会在.glsdefs
不读取文件的情况下创建文件。这将允许前言定义,而无需glossaries.tex
在文档中重新加载。