5

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.

Nicola Talbot
  • 41,153
  • Welcome to TeX.SX!. The altlong4colheader style uses longtable which computes the cell widths according to the widest value for that column, except in the case of the p specifier (which is used for the description column). You can change the value of \glsdescwidth (using \setlength) to make the description column wider. – Nicola Talbot May 01 '14 at 12:10
  • Ok, thank you so far. But honestly, I don't really know how to do it. Could you give me an example? I have problems with the right syntax. – user50897 May 01 '14 at 12:17

1 Answers1

6

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

Nicola Talbot
  • 41,153
  • Hello, when I wanted to remove the glossary header, I removed this part from your code \renewcommand*{\glossaryheader}{% \bfseries sign & \bfseries description & \bfseries unit & \\ \hline \\\endhead}%, but I feel like there is some additional vertical space before the first entry. How can I fix this? Thanks – Diaa Sep 05 '17 at 15:29
  • This additional space can be seen when comparing your style namedescunit with superraged. I would be grateful if I could make your style have the same skip of superraged after the title without including headers. – Diaa Sep 05 '17 at 15:47
  • 1
    @DiaaAbidou You need \renewcommand*{\glossaryheader}{} to suppress the header. If you completely remove that definition you could have style leakage. The vertical space is a combination of the natural vertical space after the section title (which you'd get with other styles) but as the vertical space automatically inserted by \begin{longtable}. You'll need to find out how to adjust that. I recommend posting a follow-up question with a MWE. Include \hrulefill \begin{longtable}{ll} X & Y \end{longtable} for comparison, as that's independent of glossaries and will show the spacing. – Nicola Talbot Sep 05 '17 at 15:54
  • 1
    @DiaaAbidou Alternatively, use supertabular instead of longtable. – Nicola Talbot Sep 05 '17 at 15:55
  • Many thanks for your consideration, I will consider your comments, then post a follow-up question (if needed) and put its link here. – Diaa Sep 05 '17 at 15:58
  • This code block \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 makes my code crash when using siunitx abbreviations inside the symbol key as shown in my question. – Diaa Sep 06 '17 at 03:30