1

I have this MWE which I would like to use for my glossary. Problem is that the width of the third column is based on \setlength{\glsdescwidth}{12cm} which will/will not work depending on the document class's page margins.

I would like the right column to use the full usable page width after left and center column have adopted to what is needed to display their content. I understand that ltxtable or tabularx can be utilized but I just can't figure out how to get it right. Can someone point me in the right direction?

\documentclass{article}
\usepackage{lipsum}
\usepackage{siunitx}

\usepackage[acronym,toc]{glossaries}              % use glossaries-package
\setlength{\glsdescwidth}{12cm}
\usepackage{glossary-longbooktabs}

\newglossary[slg]{unitlist}{syi}{syg}{Verzeichnis der Kurzzeichen} % create add. symbolslist
\glsaddkey{unit}{\glsentrytext{\glslabel}}{\glsentryunit}{\GLsentryunit}{\glsunit}{\Glsunit}{\GLSunit}
\makeglossaries                                   % activate glossaries-package

%===================================================================
%Einträge Einheitenverzeichnis
%===================================================================

   \newglossaryentry{m}{name=\ensuremath{m},
    description={Meter},
    unit={\si{m}},
    type=unitlist}

    \newglossaryentry{symb:Pi}{name=\ensuremath{\pi},
    description={Geometrischer Wert},
    unit={},
    type=unitlist}

    \newglossaryentry{energyconsump}{
    name=\ensuremath{P},
    description={Leistung},
    unit={\si{kW}},
    type=unitlist}

\glsnoexpandfields
 \newglossaryentry{sqkm}{
    name=\ensuremath{A},
    description=Quadratkilometer Quadratkilometer Quadratkilometer Quadratkilometer Quadratkilometer,
    unit=\si{\square\kilo\metre},
    type=unitlist}

%===================================================================
%Stilvorlage
%===================================================================

\newglossarystyle{3colger}{%
    \setglossarystyle{longragged3col}% base this style on the list style
    \renewenvironment{theglossary}{% Change the table type --> 3 columns
        \begin{longtable}{l l p{\glsdescwidth}}}%
        {\end{longtable}}%
    %
    \renewcommand*{\glossaryheader}{%  Change the table header
        \bfseries Zeichen & \bfseries Einheit & \bfseries Beschreibung \\
        %       \hline
        \vspace{0.05cm}
        \endhead}
    \renewcommand*{\glossentry}[2]{%  Change the displayed items
        \glstarget{##1}{\glossentryname{##1}} %
        & \glsunit{##1} 
        &  \glossentrydesc{##1}  \tabularnewline
    }
}

\begin{document}
    \lipsum[2-4]
\glsaddall
\printglossary[type=unitlist,style=3colger] 
\end{document}

How it looks like if the \setlength{\glsdescwidth}{12cm} has not been chosen correctly: enter image description here

1
  • I have removed the [geometry] tag, as that applies explicitly to the geometry package. Replaced by [layout]. Nov 22, 2019 at 3:23

1 Answer 1

1

Assuming that "Zeichen" is wider than any of the names and "Einheit" is wider than any of the units, then the width can be computed as line-width - width of "Zeichen" - width of "Einheit" - inter-column spaces. The inter-column spaces are governed by \tabcolsep. (A space of width \tabcolsep is inserted on either side of a column, which means the space between columns is actually 2\tabcolsep, so for three columns, the total inter-column space is 6\tabcolsep, which includes the space before the first column and after the last column.) The width of the column header text can be calculated using \settowidth.

\newglossarystyle{3colger}{%
    \setglossarystyle{longragged3col}% base this style on the list style
    \renewenvironment{theglossary}{% Change the table type --> 3 columns
     % compute the description width
        \settowidth{\dimen0}{\bfseries Zeichen}%
        \settowidth{\dimen1}{\bfseries Einheit}%
        \glsdescwidth=\dimexpr\linewidth-\dimen0-\dimen1-6\tabcolsep\relax
        \begin{longtable}{l l p{\glsdescwidth}}}%
        {\end{longtable}}%
    %
    \renewcommand*{\glossaryheader}{%  Change the table header
        \bfseries Zeichen & \bfseries Einheit & \bfseries Beschreibung \\
        %       \hline
        \vspace{0.05cm}
        \endhead}
    \renewcommand*{\glossentry}[2]{%  Change the displayed items
        \glstarget{##1}{\glossentryname{##1}} %
        & \glsunit{##1}
        &  \glossentrydesc{##1}  \tabularnewline
    }
}

This produces:

image of glossary

This isn't flush with the text because of the automatically inserted spaces (of width \tabcolsep) before the first column and after the last column. These can be removed with @{} and the calculation needs to be adjusted to only subtract 4\tabcolsep instead of 6\tabcolsep:

\newglossarystyle{3colger}{%
    \setglossarystyle{longragged3col}% base this style on the list style
    \renewenvironment{theglossary}{% Change the table type --> 3 columns
        \settowidth{\dimen0}{\bfseries Zeichen}%
        \settowidth{\dimen1}{\bfseries Einheit}%
        \glsdescwidth=\dimexpr\linewidth-\dimen0-\dimen1-4\tabcolsep\relax
        \begin{longtable}{@{}l l p{\glsdescwidth}@{}}}%
        {\end{longtable}}%
    %
    \renewcommand*{\glossaryheader}{%  Change the table header
        \bfseries Zeichen & \bfseries Einheit & \bfseries Beschreibung \\
        %       \hline
        \vspace{0.05cm}
        \endhead}
    \renewcommand*{\glossentry}[2]{%  Change the displayed items
        \glstarget{##1}{\glossentryname{##1}} %
        & \glsunit{##1}
        &  \glossentrydesc{##1}  \tabularnewline
    }
}

This now produces:

image of document

In answer to your comment, your original value of 12cm is too long. It's nearly the entire text width, which means that you haven't taken into account the width of the first two columns. You can see this if we make a minor modification to your original MWE:

\newglossarystyle{3colger}{%
    \setglossarystyle{longragged3col}% base this style on the list style
    \renewenvironment{theglossary}{% Change the table type --> 3 columns
        \noindent\rule{\glsdescwidth}{1pt}\par% add rule for debugging
        \begin{longtable}{@{}l l p{\glsdescwidth}@{}}}%
        {\end{longtable}}%
    %
    \renewcommand*{\glossaryheader}{%  Change the table header
        \bfseries Zeichen & \bfseries Einheit & \bfseries Beschreibung \\
        %       \hline
        \vspace{0.05cm}
        \endhead}
    \renewcommand*{\glossentry}[2]{%  Change the displayed items
        \glstarget{##1}{\glossentryname{##1}} %
        & \glsunit{##1}
        &  \glossentrydesc{##1}  \tabularnewline
    }
}

This adds a rule of 12cm just before the start of the glossary so you can compare it with the table:

image of glossary with 12cm-wide description column

So the third column (the description column) is obeying the value of \glsdescwidth, but that value is too wide. Changing the length in the preamble

\setlength{\glsdescwidth}{5cm}

is reflected in the updated document:

image of glossary with 5cm-wide description column

Now returning to my modification to your MWE:

\renewenvironment{theglossary}{%
 % compute the description width
    \settowidth{\dimen0}{\bfseries Zeichen}%
    \settowidth{\dimen1}{\bfseries Einheit}%
    \glsdescwidth=\dimexpr\linewidth-\dimen0-\dimen1-6\tabcolsep\relax
    \begin{longtable}{@{}l l p{\glsdescwidth}@{}}}%
    {\end{longtable}}%

This explicitly calculates \glsdescwidth just before displaying the glossary, which means that any change to this length in the preamble will be (locally) overridden by the glossary style.

2
  • Perfect, thank you very much! Just for my understanding, could you explain what \glsdescwidth actually does? Doesn't it have to be defined in the preamble? If I change the value form 12 to e.g 5 cm it doesn't change anything in your build (which is perfect for me, but I do not get the point of it) Mar 24, 2017 at 19:04
  • @Redfox87678 I've added to my answer. Basically a change from 12 to 5 cm in the preamble won't change anything in my example, because \glsdescwidth is changed by the style, but it should cause a change in your example. (You might need two latex calls to update the longtable to reflect the new column width. That's a quirk of the way longtable works.) Mar 25, 2017 at 13:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .