% This file is public domain. See the "Examples" chapter
% in the bib2gls user manual for a more detailed description
% of this file.

\documentclass{scrreprt}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[version=4]{mhchem}
\usepackage{siunitx}
\usepackage[colorlinks]{hyperref}

\usepackage[record,% use bib2gls
 section,% use \section* for glossary headings
 postdot,% insert dot after descriptions in glossaries
 nomain,% don't create 'main' glossary
 index,% create 'index' glossary
 nostyles,% don't load default styles
% load and patch required style packages:
 stylemods={list,mcols,tree,bookindex}
]{glossaries-extra}

\newglossary*{bacteria}{Bacteria}
\newglossary*{markuplanguage}{Markup Languages}
\newglossary*{vegetable}{Vegetables}
\newglossary*{mineral}{Minerals}
\newglossary*{animal}{Animals}
\newglossary*{chemical}{Chemical Formula}
\newglossary*{baseunit}{SI Units}
\newglossary*{derivedunit}{Derived Units}

% abbreviation styles must be set before \GlsXtrLoadResources:
\setabbreviationstyle[bacteria]{long-only-short-only}
\setabbreviationstyle[markuplanguage]{long-short-desc}

% style-dependent name format must be set
% before \GlsXtrLoadResources:
\renewcommand*{\glsxtrlongshortdescname}{%
  \protect\glsabbrvfont{\the\glsshorttok}\space
  \glsxtrparen{\glslongfont{\the\glslongtok}}%
}

\GlsXtrLoadResources[
 src={bacteria,markuplanguages,vegetables,minerals,
  animals,chemicalformula,baseunits,derivedunits},
 selection={recorded and deps and see},
 set-widest,
 type=index,
 label-prefix={idx.},
 dual-prefix={},
 cs-label-prefix={},
 combine-dual-locations={primary},
 dual-field,
 sort={en},
 sort-field={name},
 dual-type={same as category},
 dual-sort={en},
 dual-sort-number-pad={2},
 dual-sort-pad-plus={1},
 dual-sort-pad-minus={0},
 dual-break-at=upper-upper-word,
 entry-type-aliases={
  abbreviation=dualindexabbreviation,
  entry=dualindexentry,
  symbol=dualindexsymbol,
  unit=dualindexsymbol,
  measurement=dualindexsymbol,
  chemical=dualindexsymbol
 },
 abbreviation-name-fallback={long},
 symbol-sort-fallback={name},
 field-aliases={
   identifier=category,
   formula=symbol,
   chemicalname=name,
   unitname=name,
   unitsymbol=symbol,
   measurement=description
 },
]

\newcommand{\bacteriafont}[1]{\emph{#1}}
\glssetcategoryattribute{bacteria}{textformat}{bacteriafont}
\glssetcategoryattribute{bacteria}{glossnamefont}{bacteriafont}
\glssetcategoryattribute{bacteria}{glossdescfont}{bacteriafont}

\glssetcategoryattribute{markuplanguage}{glossdesc}{firstuc}

\renewcommand*{\glsxtrbookindexname}[1]{%
  \glossentryname{#1}%
  \ifglshassymbol{#1}%
  {%
    \glsifcategory{#1}{chemical}%
    {, \glossentrysymbol{#1}}%
    {\space(\glossentrynameother{#1}{symbol})}%
  }%
  {%
    \glsifcategory{#1}{markuplanguage}%
    {%
      \glsxtrifhasfield{short}{\glsxtrusefield{#1}{dual}}%
      {\space(\glscurrentfieldvalue)}%
      {}%
    }%
    {}%
  }%
}

\begin{document}
\chapter{Sample}
\section{Bacteria}
\subsection{First Use}
\gls{cbotulinum}, \gls{pputida}, \gls{cperfringens},
\gls{bsubtilis}, \gls{ctetani}, \gls{pcomposti},
\gls{pfimeticola}, \gls{cburnetii}, \gls{raustralis},
\gls{rrickettsii}.

\subsection{Next Use}
\gls{cbotulinum}, \gls{pputida}, \gls{cperfringens},
\gls{bsubtilis}, \gls{ctetani}, \gls{pcomposti},
\gls{pfimeticola}, \gls{cburnetii}, \gls{raustralis},
\gls{rrickettsii}.

\section{Markup Languages}
\subsection{First Use}
\gls{LaTeX}, \gls{markdown}, \gls{xhtml}, \gls{mathml}, \gls{svg}.

\subsection{Next Use}
\gls{LaTeX}, \gls{markdown}, \gls{xhtml}, \gls{mathml}, \gls{svg}.

\section{Vegetables}
\gls{cabbage}, \gls{brussels-sprout}, \gls{artichoke}, 
\gls{cauliflower}, \gls{courgette}, \gls{spinach}.

\section{Minerals}
\Gls{beryl}, \gls{amethyst}, \gls{chalcedony}, \gls{aquamarine},
\gls{aragonite}, \gls{calcite}, \gls{bilinite}, 
\gls{cyanotrichite}, \gls{biotite}, \gls{dolomite}, 
\gls{quetzalcoatlite}, \gls{vulcanite}.

\section{Animals}
\Gls{duck}, \gls{parrot}, \gls{hedgehog}, \gls{sealion}.

\section{Chemicals}
\gls{Al2SO43}, \gls{H2O}, \gls{C6H12O6},
\gls{CH3CH2OH}, \gls{CH2O}, \gls{OF2}, \gls{O2F2}, \gls{SO42-},
\gls{H3O+}, \gls{OH-}, \gls{O2}, \gls{AlF3}, \gls{O},
\gls{Al2CoO4}, \gls{As4S4}, \gls{C10H10O4}, \gls{C5H4NCOOH},
\gls{C8H10N4O2}, \gls{SO2}, \gls{S2O72-}, \gls{SbBr3},
\gls{Sc2O3}, \gls{Zr3PO44}, \gls{ZnF2}.

\section{SI Units}
Base: \gls{ampere}, \gls{kilogram}, \gls{metre}, \gls{second},
\gls{kelvin}, \gls{mole}, \gls{candela}.
Derived: \gls{area}, \gls{volume}, \gls{velocity},
\gls{acceleration}, \gls{density}, \gls{luminance},
\gls{specificvolume}, \gls{concentration}, \gls{wavenumber}.

\chapter*{Glossaries}
\renewcommand{\glsextrapostnamehook}[1]{\glsadd[format=hyperbf]{#1}}
\printunsrtglossary[type=bacteria,style=mcoltree]
\printunsrtglossary[type=markuplanguage,style=altlist]
\printunsrtglossary[type=vegetable,style=tree,nogroupskip]
\printunsrtglossary[type=mineral,style=treegroup]
\printunsrtglossary[type=animal,style=tree]
\printunsrtglossary*[type=chemical,style=mcolalttreegroup]
{%
  \renewcommand\glsxtralttreeSymbolDescLocation[2]{%
   \glossentrysymbol{#1}\glspostdescription\glsxtrAltTreePar
  }%
  \renewcommand*{\glstreenamefmt}[1]{#1}%
  \renewcommand*{\glstreegroupheaderfmt}[1]{\textbf{#1}}%
}
\printunsrtglossary[type=baseunit,style=alttree]
\printunsrtglossary[type=derivedunit,style=alttree]

\renewcommand{\glsextrapostnamehook}[1]{}
\setupglossaries{section=chapter}
\printunsrtglossary[type=index,style=bookindex]
\end{document}}
