[Tex/LaTex] How to print acronyms of glossaries into a table

acronymsglossariestabularx

for now I am using a tabularx table to print acronyms manually. But I want to use the style of my table with glossaries and acronyms: \printglossary[type=\acronymtype,style=mystyle]

The table so far:

\begin{tabularx}{\linewidth}{l @{\hspace{1.5em}=\hspace{1em}}X}
  \toprule
  \multicolumn{1}{l}{\textbf{Abk.}} & \multicolumn{1}{@{}X}{\textbf{Bedeutung}}\\%
  \midrule
  %
  LED       & Light-Emitting Diode                  \\%
  VCC       & positive Versorgungsspannung          \\%
  GND       & Ground, negative Versorgungsspannung  \\[0.3ex]%
  \bottomrule%
\end{tabularx}%

How would the code of the corresponding style look like? The acronyms will be defined like this:

\newacronym{led}{LED}{Light-Emitting Diode}
\newacronym{vcc}{VCC}{positive Versorgungsspannung}
\newacronym{gnd}{GND}{Ground, negative Versorgungsspannung}

Thanks in advance

Best Answer

As far as I can tell tabularx doesn't work in a glossary style due to the way the tabularx environment processes its contents. However it's possible to achieve the same effect without using tabularx. The following example requires the latest version of glossaries (v4.01 at time of writing):

\documentclass{article}

\usepackage{calc}
\usepackage{booktabs}
\usepackage{tabularx}% (for comparison)
\usepackage[acronym,nomain]{glossaries}

\makeglossaries

\newacronym{led}{LED}{Light-Emitting Diode}
\newacronym{vcc}{VCC}{positive Versorgungsspannung}
\newacronym{gnd}{GND}{Ground, negative Versorgungsspannung}

\newlength\maxlength
\newlength\thislength

\newglossarystyle{mystyle}
{%
  \renewenvironment{theglossary}%
  {% start of glossary
   % Find maximum width of the first column:
    \setlength{\maxlength}{0pt}%
    \forglsentries[\currentglossary]{\thislabel}%
    {%
       \settowidth{\thislength}{\glsentryshort{\thislabel}}%
       \ifdim\thislength>\maxlength
         \setlength{\maxlength}{\thislength}%
       \fi
    }%
    % Now calculate the width of the second column:
    \settowidth{\thislength}{\hspace{1.5em}=\hspace{1em}}%
    \setlength{\glsdescwidth}{\linewidth-\maxlength-\thislength-2\tabcolsep}%
    % Start the tabular environment
    \begin{tabular}{l@{\hspace{1.5em}=\hspace{1em}}p{\glsdescwidth}}
    \toprule
    \multicolumn{1}{l}{\textbf{Abk.}} &
    \multicolumn{1}{@{}l}{\textbf{Bedeutung}}\\%
    \midrule
  }%
  {% end of glossary
     \bottomrule
     \end{tabular}%
  }%
  % Header has been incorporated into \begin{theglossary}
  \renewcommand*{\glossaryheader}{}%
  % Don't do anything between letter groups
  \renewcommand*{\glsgroupheading}[1]{}%
  \renewcommand*{\glsgroupskip}{}%
  % Set display for each the acronym entry
  \renewcommand{\glossentry}[2]{%
    \glstarget{##1}{\glsentryshort{##1}}% short form
    &
    \glsentrylong{##1}% long form
    \\% end of row
  }%
  % No sub-entries, so \subglossentry doesn't need redefining
}

\begin{document}

First use: \gls{led}, \gls{vcc}, \gls{gnd}.

Next use: \gls{led}, \gls{vcc}, \gls{gnd}.

\printglossary[type=\acronymtype,style=mystyle]

Compare with:

\noindent
\begin{tabularx}{\linewidth}{l @{\hspace{1.5em}=\hspace{1em}}X}
  \toprule
  \multicolumn{1}{l}{\textbf{Abk.}} &
\multicolumn{1}{@{}X}{\textbf{Bedeutung}}\\%
  \midrule
  %
  LED       & Light-Emitting Diode                  \\%
  VCC       & positive Versorgungsspannung          \\%
  GND       & Ground, negative Versorgungsspannung  \\[0.3ex]%
  \bottomrule%
\end{tabularx}%

\end{document}

This produces:

Image of resulting document

Note that the acronyms have been sorted alphabetically. If you want them in order of definition you need to use the sort=def package option.