I'm try to create a custom latex class based on scrbook. I make heavy use of glossaries for my math symbols, for which pdftool tips are created showing the description and units when a mouse pointer hoovers over.
Because I have an extensive list of glossary entries, not just symbols, but also acronyms and normal entry, I would like to store them in a csv file and load them with the datatool package.
The Atom editor (which I use) allows for the auto-completion of user defined glossaries with the package autocomplete-glossaries. But this requires the creation of the jobname.glsdef file. Hence the input{glossary.tex} at the beginning of the document environment.
It appears that I have trouble with the expansion of the \si command. Either it only reference the latest csv entry, in this case grams (bogus unit needed for illustration purposes). Which probably happens due to the \glssetnoexpandfield{unit} command. But if I set this to \glssetexpandfield{unit} siunitx starts to sputter with an "Undefined control sequence".
My guess is that I need to expand the \units command but not the actual \si command. But I can't seem to get it to work.
Any help is much appreciated.
\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}
.glsdefs
file should be ignored. It won't work with\makenoidxglossaries
or with\GlsXtrLoadResources
, which don't allow document definitions.