[Tex/LaTex] Automated generation of equation key

automationequationsglossaries

I am looking for a way to automatically create the legend for an equation (let's say embedded in the align environment).

What I'm currently using is a semi automated to fully manual way. Let me explain: The result should look like this:

img1.

What I am doing to accomplish this requires 3 steps to generate the output shown above:

1) Placing equation:

\begin{align}  
    \dot{D} = \frac{1-\eta_{Deg,r}}{\tau} \label{eq:deg}  
\end{align}

2) Defining the corresponding entries in the symbol glossary:

\newglossaryentry{sym:deg}{  
type=symbols,  
name={$\dot{D}$},  
description={Prozentuale jährliche Abnahme des Wirkungsgrades},  
user1={\%/a},  
sort=deg  
}  

\newglossaryentry{sym:etadegr}{
type=symbols,
name={$\eta_{Deg,r}$},
description={Verbleibender Wirkungsgrad bezogen auf den ursprünglichen Wirkungsgrad},
user1=\%,
sort=eta deg rest
}

\newglossaryentry{sym:tau}{
type=symbols,
name={$\tau$},
description={Lebenszyklus / Einsatzdauer des betrachteten Systems},
user1=a,
sort=tau
}

3) Custom command to fill table with glossaries reference:

%Command
\newcommand{\items}[1]{\glstext*{#1} & \glsdesc*{#1} [\glsuseri*{#1}] \\}

%Legend Environment
\begin{tabular}{@{} p{1cm} p{0.9 \textwidth}}
    \items{sym:deg}
    \items{sym:etadegr}
    \items{sym:tau}
\end{tabular}

Question

I'd like to create a command which searches the previous align environment for elements of my symbol glossary and puts them into a legend (in this case tabular) environment. Does this already exist (didn't find anything)? And how would i realize this?
Not expecting a perfect solution but I have no clue how to do this.

An MWE to tinker with can be found here –> Gist

€:
-Additional Information: Using miktex 2.9 and TechniXCenter 2 on Windows.
-Matches for the search could be done with the name key of the glossary entry

Best Answer

Here's a possible solution. It requires xindy rather than makeindex. If you really don't want to use xindy, it's possible to adjust the code, but it's more complicated.

% arara: pdflatex
% arara: makeglossaries
% arara: pdflatex
\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage[counter=alignglos,xindy]{glossaries}

\newglossary[slg]{symbols}{slm}{sbl}{List of Symbols}

\GlsSetXdyMinRangeLength{0}

\makeglossaries

\newglossaryentry{sym:deg}{  
type=symbols,  
name={\ensuremath{\dot{D}}},  
description={Prozentuale jährliche Abnahme des Wirkungsgrades},  
user1={\%/a},  
sort=deg  
}  

\newglossaryentry{sym:etadegr}{
type=symbols,
name={\ensuremath{\eta_{Deg,r}}},
description={Verbleibender Wirkungsgrad bezogen auf den
ursprünglichen Wirkungsgrad},
user1=\%,
sort=eta deg rest
}

\newglossaryentry{sym:tau}{
type=symbols,
name={\ensuremath{\tau}},
description={Lebenszyklus / Einsatzdauer des betrachteten Systems},
user1=a,
sort=tau
}

\newcommand{\items}[2]{\glstext*{#1} & #2 [\glsuseri*{#1}] \\}

\newglossarystyle{alignglos}%
{%
  \renewcommand{\glsgroupskip}{}%
  \renewcommand{\glossaryheader}{}%
  \renewcommand{\glsgroupheading}[1]{}%
  \renewenvironment{theglossary}%
   {\begin{tabular}{@{}p{1cm}p{0.9\textwidth}}}%
   {\end{tabular}}%
  \renewcommand*{\glossaryentrynumbers}[1]{##1}%
  \renewcommand*{\glsXalignglosXglsnumberformat}[2]{##2}%
  \renewcommand*{\delimN}{,}%
  \renewcommand*{\glossaryentryfield}[5]{%
    \edef\doifinlocation{\noexpand\ifinlocation{\thealignglos}{\expandafter\striprelax##5\endstriprelax}}%
    \doifinlocation
    {%
      \items{##1}{##3}%
    }%
  }%
}

\newcommand{\ifinlocation}[3]{%
 \DTLifinlist{#1}{#2}{#3}{}%
}

\def\striprelax\relax#1\endstriprelax{#1}


\newcounter{alignglos}

\newenvironment{alignglos}%
 {\stepcounter{alignglos}}%
 {%
   \printglossary[type=symbols,style=alignglos,title={}]
 }

\begin{document}

\begin{alignglos}
\begin{align}  
    \gls{sym:deg} &= \frac{1-\gls{sym:etadegr}}{\gls{sym:tau}}
\end{align}
\end{alignglos}

\begin{alignglos}
\begin{align}  
    \gls{sym:tau} &= \ldots
\end{align}
\end{alignglos}

\end{document}

Edit:

Here's the code for use with makeindex instead of xindy:

% arara: pdflatex
% arara: makeglossaries
% arara: pdflatex
\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{etoolbox}
\usepackage[counter=alignglos]{glossaries}

\newglossary[slg]{symbols}{slm}{sbl}{List of Symbols}

\makeglossaries

\newglossaryentry{sym:deg}{  
type=symbols,  
name={\ensuremath{\dot{D}}},  
description={Prozentuale jährliche Abnahme des Wirkungsgrades},  
user1={\%/a},  
sort=deg  
}  

\newglossaryentry{sym:etadegr}{
type=symbols,
name={\ensuremath{\eta_{Deg,r}}},
description={Verbleibender Wirkungsgrad bezogen auf den
ursprünglichen Wirkungsgrad},
user1=\%,
sort=eta deg rest
}

\newglossaryentry{sym:tau}{
type=symbols,
name={\ensuremath{\tau}},
description={Lebenszyklus / Einsatzdauer des betrachteten Systems},
user1=a,
sort=tau
}

\newcommand{\items}[2]{\glstext*{#1} & #2 [\glsuseri*{#1}] \\}

% clean up the format of the location list
\newcommand{\stripentrynumbers}[1]{%
  \striprelax#1\endstriprelax
}

\newglossarystyle{alignglos}%
{%
  \renewcommand{\glsgroupskip}{}%
  \renewcommand{\glossaryheader}{}%
  \renewcommand{\glsgroupheading}[1]{}%
  \renewenvironment{theglossary}%
   {\begin{tabular}{@{}p{1cm}p{0.9\textwidth}}}%
   {\end{tabular}}%
  \renewcommand*{\glsnumberformat}[1]{##1}%
  \renewcommand*{\delimR}{-}%
  \renewcommand*{\delimN}{,}%
  \renewcommand*{\glossaryentryfield}[5]{%
  \let\glossaryentrynumbers\stripentrynumbers
    \edef\doifinloclist{\noexpand\ifinloclist{\thealignglos}{##5}}%
    \doifinloclist
    {%
      \items{##1}{##3}%
    }%
  }%
  \renewcommand*{\glossarysubentryfield}[1]{\glossaryentryfield}%
}

\newcommand{\ifinloclist}[3]{%
  \renewcommand*{\do}[1]{%
     \let\dolistbreak\relax
     \doifinloc{#1}{#3}##1-\relax\endrange
     \dolistbreak
  }%
  \docsvlist{#2}%
}

\def\doifinloc#1#2#3-#4\endrange{%
   \ifx\relax#4\relax
     % just a number
     \ifnum#1=#3\relax
       #2%
       \let\dolistbreak\listbreak
     \fi
   \else
    \doifinrange{#1}{#2}#3-#4%
   \fi
}

\def\doifinrange#1#2#3-#4-\relax{%
  \ifnum#1=#3\relax
     #2%
     \let\dolistbreak\listbreak
  \else
    \ifnum#1=#4\relax
       #2%
       \let\dolistbreak\listbreak
    \else
      \ifnum#1>#3\relax
        \ifnum#1<#4\relax
          #2%
          \let\dolistbreak\listbreak
        \fi
      \fi
    \fi
  \fi
}

\def\striprelax\relax\setentrycounter[]#1#2\endstriprelax{#2}


\newcounter{alignglos}

\newenvironment{alignglos}%
 {\stepcounter{alignglos}}%
 {%
   \printglossary[type=symbols,style=alignglos,title={}]
 }

\begin{document}

\begin{alignglos}
\begin{align}  
    \gls{sym:deg} &= \frac{1-\gls{sym:etadegr}}{\gls{sym:tau}}
\end{align}
\end{alignglos}

\begin{alignglos}
\begin{align}  
    \gls{sym:tau} &= \ldots
\end{align}
\end{alignglos}

\end{document}

Result for either method:

Image of resulting document

This will also work if you want to use equation instead of align. The code is adapted from Glossary per chapter or section. You just need to wrap your equations inside the alignglos environment.

Notes:

  1. I've used \ensuremath instead of $ so it doesn't matter if you are in or out of math mode when you use the glossary entry
  2. I think Deg should probably be \operatorname{Deg} or \mathrm{Deg} (assuming it's not just D times e times g).

Edit 2:

I've modified the alignglos style so that it suppresses the group skip. I've also deferred \listbreak to the end of \do. If you have Perl installed, you can use the makeglossaries script to call makeindex or xindy, which will skip any empty glossaries. If you have Perl and Java installed, you can just run arara on the document and it will run all the commands listed in the % arara: directives. When you switch to linux, you should have Perl preinstalled and probably also the Java runtime environment.

Related Question