[Tex/LaTex] glossaries package – How to format the positions of the columns and width of the glossary

formattingglossaries

I am very fond of the glossaries package, since I am able to use multiple glossaries with different styles and so on. However, I have a problem with formatting it.

I would like to use the predefined glossarystyle altlong4colheader, since I need the possibility to have multirow descriptions, a header and the possibility to easily sort entries. I wanted my glossary entries to have a name, a description and a unit. So I got rid of the last column. I have defined the new style as follows:

 \newglossarystyle{formel_altlong4colheader}{\setglossarystyle{altlong4colheader}
% 
\renewcommand*{\glossaryheader}{%
   \bfseries sign
  & \bfseries description
  & \bfseries unit
  & \\
  \hline
  \\\endhead}%
\renewcommand{\glossentry}[2]{% 
\glstarget{##1}{\glossentryname{##1}}% 
& \glossentrydesc{##1}% 
& \glossentrysymbol{##1}% 
\tabularnewline % end of row
 }%
 }

The outcome looks like this, which is pretty close to what I want:

Example

As seen, the width of the glossary is not as wide as my text. How do I change this? I also would like the sign-column to be left-aligned and the unit-column to be right-aligned, according to my textwidth. Being able to set the position and width of the columns would also be very nice and helpful.

I hope there is someone who can help me. Thanks.

Best Answer

The simplest solution is to change the value of \glsdescwidth using \setlength. For example \setlength{\glsdescwidth}{3in} as in the following example:

\documentclass{article}

\usepackage{lipsum}
\usepackage{glossaries}

\makeglossaries

\newglossarystyle{formel_altlong4colheader}{%
 \setglossarystyle{altlong4colheader}%
% 
 \renewcommand*{\glossaryheader}{%
    \bfseries sign
   & \bfseries description
   & \bfseries unit
   & \\
   \hline
   \\\endhead}%
 \renewcommand{\glossentry}[2]{% 
 \glstarget{##1}{\glossentryname{##1}}% 
 & \glossentrydesc{##1}% 
 & \glossentrysymbol{##1}% 
 \tabularnewline % end of row
 }%
}

\setlength{\glsdescwidth}{3in}
\setglossarystyle{formel_altlong4colheader}

\newglossaryentry{L}{name={L},description={Buchstabe},symbol={---}}
\newglossaryentry{P}{name={P},description={Leistung},symbol={kW}}

\begin{document}

\lipsum[1]
\glsaddall

\printglossaries

\end{document}

This produces:

Image of result

However this approach is a matter of trial and error to work out the value of \glsdescwidth. You also still have the fourth column, even though you're not using it, which is taking up unnecessary space. Instead I recommend a different approach that works out the width:

\documentclass{article}

\usepackage{lipsum}
\usepackage{booktabs}
\usepackage{calc}
\usepackage{glossaries}

\makeglossaries

\newlength\glsnamewidth
\newlength\glsunitwidth

\settowidth{\glsnamewidth}{\textbf{sign}}
\settowidth{\glsunitwidth}{\textbf{unit}}

\newglossarystyle{namedescunit}{%
  \setlength{\glsdescwidth}{\linewidth-\glsnamewidth-\glsunitwidth-6\tabcolsep}%
  \renewenvironment{theglossary}%
    {\begin{longtable}{p{\glsnamewidth}p{\glsdescwidth}p{\glsunitwidth}}}%
    {\end{longtable}}%
 \renewcommand*{\glossaryheader}{%
    \bfseries sign
   & \bfseries description
   & \bfseries unit
   \tabularnewline
   \midrule
   \tabularnewline\endhead}%
  \renewcommand*{\glsgroupheading}[1]{}%
  \renewcommand{\glossentry}[2]{%
    \glstarget{##1}{\glossentryname{##1}} &
    \glossentrydesc{##1} &
    \glossentrysymbol{##1}\tabularnewline
  }%
  \renewcommand{\subglossentry}[3]{\glossentry{##2}{##3}}%
  \renewcommand*{\glsgroupskip}{}%
}

\setglossarystyle{namedescunit}

\newglossaryentry{L}{name={L},description={Buchstabe},symbol={---}}
\newglossaryentry{P}{name={P},description={Leistung},symbol={kW}}

\begin{document}

\lipsum[1]
\glsaddall

\printglossaries

\end{document}

This produces:

Image of result

This assumes that the maximum widths of the first and third columns are given by their header text. This may not be the case, so the following example computes them as the entries are defined:

\documentclass{article}

\usepackage{lipsum}
\usepackage{booktabs}
\usepackage{calc}
\usepackage{glossaries}

\makeglossaries

\newlength\glsnamewidth
\newlength\glsunitwidth

\settowidth{\glsnamewidth}{\textbf{sign}}
\settowidth{\glsunitwidth}{\textbf{unit}}

\newglossarystyle{namedescunit}{%
  \setlength{\glsdescwidth}{\linewidth-\glsnamewidth-\glsunitwidth-6\tabcolsep}%
  \renewenvironment{theglossary}%
    {\begin{longtable}{p{\glsnamewidth}p{\glsdescwidth}p{\glsunitwidth}}}%
    {\end{longtable}}%
 \renewcommand*{\glossaryheader}{%
    \bfseries sign
   & \bfseries description
   & \bfseries unit
   \tabularnewline
   \midrule
   \tabularnewline\endhead}%
  \renewcommand*{\glsgroupheading}[1]{}%
  \renewcommand{\glossentry}[2]{%
    \glstarget{##1}{\glossentryname{##1}} &
    \glossentrydesc{##1} &
    \glossentrysymbol{##1}\tabularnewline
  }%
  \renewcommand{\subglossentry}[3]{\glossentry{##2}{##3}}%
  \renewcommand*{\glsgroupskip}{}%
}

\setglossarystyle{namedescunit}

\makeatletter
 \appto\@newglossaryentryposthook{%
   \settowidth{\dimen@}{\glsentryname{\@glo@label}}%
   \ifdim\dimen@>\glsnamewidth
     \setlength{\glsnamewidth}{\dimen@}%
   \fi
   \settowidth{\dimen@}{\glsentrysymbol{\@glo@label}}%
   \ifdim\dimen@>\glsunitwidth
     \setlength{\glsunitwidth}{\dimen@}%
   \fi
 }%
\makeatother

\newglossaryentry{L}{name={L},description={Buchstabe},symbol={---}}
\newglossaryentry{P}{name={P},description={Leistung},symbol={kW}}

\begin{document}

\lipsum[1]
\glsaddall

\printglossaries

\end{document}

In this case, the result is the same as the previous example. If you want the name and unit columns centred you can add \centering to the style:

\documentclass{article}

\usepackage{lipsum}
\usepackage{booktabs}
\usepackage{calc}
\usepackage{glossaries}

\makeglossaries

\newlength\glsnamewidth
\newlength\glsunitwidth

\settowidth{\glsnamewidth}{\textbf{sign}}
\settowidth{\glsunitwidth}{\textbf{unit}}

\newglossarystyle{namedescunit}{%
  \setlength{\glsdescwidth}{\linewidth-\glsnamewidth-\glsunitwidth-6\tabcolsep}%
  \renewenvironment{theglossary}%
    {\begin{longtable}{p{\glsnamewidth}p{\glsdescwidth}p{\glsunitwidth}}}%
    {\end{longtable}}%
 \renewcommand*{\glossaryheader}{%
    \centering\bfseries sign
   & \bfseries description
   & \centering\bfseries unit
   \tabularnewline
   \midrule
   \tabularnewline\endhead}%
  \renewcommand*{\glsgroupheading}[1]{}%
  \renewcommand{\glossentry}[2]{%
    \centering\glstarget{##1}{\glossentryname{##1}} &
    \glossentrydesc{##1} &
    \centering\glossentrysymbol{##1}\tabularnewline
  }%
  \renewcommand{\subglossentry}[3]{\glossentry{##2}{##3}}%
  \renewcommand*{\glsgroupskip}{}%
}

\setglossarystyle{namedescunit}

\makeatletter
 \appto\@newglossaryentryposthook{%
   \settowidth{\dimen@}{\glsentryname{\@glo@label}}%
   \ifdim\dimen@>\glsnamewidth
     \setlength{\glsnamewidth}{\dimen@}%
   \fi
   \settowidth{\dimen@}{\glsentrysymbol{\@glo@label}}%
   \ifdim\dimen@>\glsunitwidth
     \setlength{\glsunitwidth}{\dimen@}%
   \fi
 }%
\makeatother

\newglossaryentry{L}{name={L},description={Buchstabe},symbol={---}}
\newglossaryentry{P}{name={P},description={Leistung},symbol={kW}}

\begin{document}

\lipsum[1]
\glsaddall

\printglossaries

\end{document}

This now produces:

First and third columns now centred

Related Question