About 🛈
Shop 🛒Shop 🛒
LaTeX 🖹
Software 💾
Books 📚
Gallery 🖺
News 📢
Contact 📧
Blog 🖋

Gallery (bib2gls) Constants Sample

Image of constants sample
This example is taken from the ‘Examples’ chapter of bib2gls's user manual.

The entries are defined in a .bib file, constants.bib. Instead of using the entry types (such as @entry) and field names (such as name) recognised by bib2gls, these entries are all defined using a custom field @constant and custom field names (constantname, constantsymbol, identifier and value). These can be aliased on a per-document basis to allow for greater flexibility. For example, one document might alias the unknown constantname field to the known name field, but another document might alias the unknown constantsymbol to the name field instead.

The file constants.bib contains the following:

% Encoding: UTF-8

@preamble{"\providecommand{\constanti}{\mathrm{i}}
\providecommand{\constantj}{\mathrm{j}}
\providecommand{\constante}{\mathrm{e}}
\providecommand{\constantpi}{\uppi}
\providecommand{\constantgamma}{\upgamma}
\providecommand{\constantphi}{\upphi}
\providecommand{\constantlambda}{\uplambda}"}

@constant{pi,
  constantname={pi},
  constantsymbol={\ensuremath{\constantpi}},
  definition={the ratio of the length of the circumference
    of a circle to its diameter},
  value={3.14159},
  identifier={constant}
}

@constant{eulercons,
  constantname={Euler's constant},
  constantsymbol={\ensuremath{\constantgamma}},
  definition={the limit of \[\sum_{r=1}^n\frac{1}{r}-\ln n\]
    as $n\to\infty$},
  value={0.57721},
  identifier={constant}
}

@constant{e,
  constantname={Euler's number},
  constantsymbol={\ensuremath{\constante}},
  definition={base of natural logarithms},
  value={2.71828},
  identifier={constant}
}

@constant{root2,
  constantname={Pythagoras' constant},
  constantsymbol={\ensuremath{\surd2}},
  definition={the square root of 2},
  value={1.41421},
  identifier={constant}
}

@constant{goldenratio,
  constantname={golden ratio},
  constantsymbol={\ensuremath{\constantphi}},
  definition={the ratio $\frac{1+\surd5}{2}$},
  value={1.61803},
  identifier={constant}
}

@constant{aperysconstant,
  constantname={Ap\'ery's constant},
  constantsymbol={\ensuremath{\zeta(3)}},
  definition={a special value of the Riemann zeta function},
  value={1.2020569},
  identifier={constant}
}

@constant{conwaysconstant,
  constantname={Conway's constant},
  constantsymbol={\ensuremath{\constantlambda}},
  definition={the invariant growth rate of all derived strings},
  value={1.30357},
  identifier={constant}
}

@constant{zero,
  constantname={zero},
  constantsymbol={\ensuremath{0}},
  definition={nothing or nil},
  identifier={constant}
}

@constant{one,
  constantname={one},
  constantsymbol={\ensuremath{1}},
  definition={single entity, unity},
  identifier={constant}
}

@constant{imaginary,
  constantname={imaginary unit},
  constantsymbol={\ensuremath{\constanti}},
  definition={defined as $\constanti^2 = -1$},
  identifier={constant},
  alternative={\ensuremath{\constantj}}
}

The document aliases @constant to @number with entry-type-aliases={constant=entry} and aliases the unknown fields with field-aliases. The numeric value is aliased to the user1 field, so it's possible to sort the entries numerically by selecting that field as the sort value. These entries use some commands from the upgreek package, so the document must load that package.

The post-description category hooks provided by the glossaries-extra package to append an alternative symbol and the approximate value after the description.

\documentclass[12pt,a4paper]{article}

\usepackage[T1]{fontenc}
\usepackage{upgreek}

\usepackage[record,% use bib2gls
 nostyles,% don't load default styles
 postdot,% add dot after descriptions
% load glossary-tree.sty and patch styles:
 stylemods={tree},
 style=alttree]{glossaries-extra}

\GlsXtrLoadResources[
  src={constants},% data in constants.bib
  % make @constant behave like @number
  entry-type-aliases={constant=number},
  field-aliases={
    identifier=category,
    constantsymbol=name,
    constantname=description,
    value=user1,
    definition=user2,
    alternative=user3
  },
  type=main,
  set-widest,
  sort-field=user1,
  missing-sort-fallback=name,
  sort=double,
  selection=all
]

\newcommand{\glsxtrpostdescconstant}{%
  \glsxtrifhasfield{useriii}{\glscurrententrylabel}%
  { (also denoted \glscurrentfieldvalue
      \glsxtrifhasfield{useri}{\glscurrententrylabel}%
      {, approximately \glscurrentfieldvalue}%
      {}%
   )%
  }%
  {%
    \glsxtrifhasfield{useri}{\glscurrententrylabel}%
    { (approximately \glscurrentfieldvalue)}%
    {}%
  }%
  \glsxtrifhasfield{userii}{\glscurrententrylabel}%
  {: \glscurrentfieldvalue}%
  {}%
}

\begin{document}
\printunsrtglossary[title={Constants}]
\end{document}

To build the document, you need to run the following commands:
pdflatex sample-constants
bib2gls sample-constants
pdflatex sample-constants
These commands can be run from a terminal or command prompt or you might be able to run them by clicking on a button in your text editor.

Download: PDF, document source.

© 2018 Dickimaw Books. "Dickimaw", "Dickimaw Books" and the Dickimaw parrot logo are trademarks. The Dickimaw parrot was painted by Magdalene Pritchett.

Terms of Use Privacy Policy Cookies Site Map FAQs