1

I am new here and I have a question about defining a glossary style.

I would like to have:

  • A glossary style with 3 columns
  • In the first should be the symbols, in the second the descriptions and in the third the pagenumbers

My problem is: I would like to define the width of each column manually (the width of the hole table should be as wide as \textwidth). Some symbols are abbreviations for longer terms and therefore I would like to link the page from the first use in the third column but other symbols are more or less familiar and so there shouldn't be a pagelink in the third column. More precisly, if I call a glossaryentry with \gls{..} in the text then there should be a pagelink in the third column of the glossary. The other glossaryentries should only appear with symbol and discription without a pagenumber. I don't call them seperately.

After a long research, I found similar problems but the solution doesn't work fine for me. Here is what I have now :

\documentclass{article}

\usepackage{amsfonts}
\newcommand*{\Z}{\mathbb{Z}}
\newcommand*{\Q}{\mathbb{Q}}

\usepackage{lipsum} % to have some dummy text, and therefore larger page numbers
\usepackage[colorlinks]{hyperref}
\usepackage{glossaries}
\usepackage{glossary-longragged}

\glsdisablehyper %Entfernt links von den Symbolen


\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\newglossarystyle{tabx3col}{%
 % put the glossary in a longtable environment:
 \renewenvironment{theglossary}%
  {\renewcommand{\arraystretch}{1.5}
  \begin{longtable}{@{}p{0.2\textwidth}@{}p{0.7\textwidth}@{}>{\raggedleft}p{0.1\textwidth}@{}}}%
  {\end{longtable}}%
 % Set the table's header:
 \renewcommand*{\glossaryheader}{}%
 % No heading between groups:
  \renewcommand*{\glsgroupheading}[1]{}%
 % Main (level 0) entries displayed in a row:
  \renewcommand*{\glossaryentryfield}[5]{%
    \glstarget{##1}{##2}% Name
    & ##3% Description
    & ##5% Page list
    \\% end of row
  }%
}

\newcommand*{\glsgobblenumber}[1]{}
% \renewcommand*{\glsgobblenumber}[1]{#1}% uncomment for testing

\makeatletter
%% lockstep's code
\newcommand*{\glsaddnp}[2][]{%
  \glsdoifexists{#2}%
  {
    %  \def\@glsnumberformat{glsnumberformat}% DELETED
    \def\@glsnumberformat{glsgobblenumber}% NEW
    \edef\@gls@counter{\csname glo@#2@counter\endcsname}%
    \setkeys{glossadd}{#1}%
    \@gls@saveentrycounter
    \@do@wrglossary{#2}%
  }%
}
%% new code; modified \glsaddall
\renewcommand{\glsaddallunused}[1][]{%
  \edef\@glo@type{\@glo@types}%
  \setkeys{glossadd}{#1}%
  \forallglsentries[\@glo@type]{\@glo@entry}{%
    \ifglsused{\@glo@entry}{}{%
     \glsaddnp[#1]{\@glo@entry}}}%
}
\makeatother


\newglossaryentry{integers}{name=\ensuremath{\Z},
  description={the ring of integers}, sort=Z}

\newglossaryentry{rationals}{name=\ensuremath{\Q},
  description={the field of rational numbers}, sort=Q, nonumberlist}

\newglossaryentry{vector-space}{name=\ensuremath{V},
  description={a vector space}, sort=V}

\makeglossaries

\begin{document}
A vector space \gls{vector-space} \ldots
\lipsum[1-10]

\glsaddallunused
\printglossary[style=tabx3col]
\end{document}

There seams to be problem when there is no symbol call with \gls{..} in the text. The error message is:

! Extra alignment tab has been changed to \cr.

Do you have any ideas to fix this problem?enter image description here

  • Welcome to TeX.SX! \glossaryentryfield is obsolete. What version of glossaries do you have installed? (It will be in the .log file.) – Nicola Talbot Oct 3 '17 at 10:51
  • Hey, thank you for your comment. I am using the version v4.29. Should I do an update? – Frank Oct 3 '17 at 10:59
  • That's new enough (\glossentryfield was made obsolete in v4.0). There are some bug fixes in the newer versions but v4.29 should be okay for this (at least in terms of answering it). – Nicola Talbot Oct 3 '17 at 11:14
0

You don't need:

\newcommand*{\glsgobblenumber}[1]{}

The glossaries package already provides \glsignore which does the same thing and is used in \glsaddallused.

The style command \glossaryentryfield is now obsolete and was replaced with \glossentry in glossaries v4.0. This just has two arguments: the label and the location list.

I think the main problem you were encountering was due to \\ for the row break. The column types change this so you need \tabularnewline instead.

There are also two commands missing that always need to be redefined in the style. These are \subglossentry (for sub-entries) and \glsgroupskip. If there are no sub-entries, \subglossentry isn't so important, but \glsgroupskip is important. The default style (list) defines this as

\ifglsnogroupskip \else \indexspace \fi

which interferes with the tabular material if you haven't used nogroupskip. If you want your style to honour the nogroupskip option you can define \glsgroupskip using:

  \ifglsnogroupskip
    \renewcommand*{\glsgroupskip}{}%
  \else
    \renewcommand*{\glsgroupskip}{ & &\tabularnewline}%
  \fi

If you never want your style to have a gap between groups then just define

\renewcommand*{\glsgroupskip}{}

Modified MWE:

\documentclass{article}

\usepackage{amsfonts}
\newcommand*{\Z}{\mathbb{Z}}
\newcommand*{\Q}{\mathbb{Q}}

\usepackage{lipsum} % to have some dummy text, and therefore larger page numbers
\usepackage[colorlinks]{hyperref}
\usepackage{glossaries}
\usepackage{glossary-longragged}

\glsdisablehyper %Entfernt links von den Symbolen


\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\newglossarystyle{tabx3col}{%
 % put the glossary in a longtable environment:
 \renewenvironment{theglossary}%
  {\renewcommand{\arraystretch}{1.5}
  \begin{longtable}{@{}p{0.2\textwidth}@{}p{0.7\textwidth}@{}>{\raggedleft}p{0.1\textwidth}@{}}}%
  {\end{longtable}}%
 % Set the table's header:
 \renewcommand*{\glossaryheader}{}%
 % No heading between groups:
  \renewcommand*{\glsgroupheading}[1]{}%
 % Main (level 0) entries displayed in a row:
  \renewcommand*{\glossentry}[2]{%
    \glstarget{##1}{\glossentryname{##1}}% Name
    & \glossentrydesc{##1}% Description
    & ##2% Page list
    \tabularnewline% end of row
  }%
  % make sub-entries the same 
  \renewcommand*{\subglossentry}[3]{%
    \glossentry{##2}{##3}%
  }%
  % no group separator
  \renewcommand*{\glsgroupskip}{}%
}

\newglossaryentry{integers}{name=\ensuremath{\Z},
  description={the ring of integers}, sort=Z}

\newglossaryentry{rationals}{name=\ensuremath{\Q},
  description={the field of rational numbers}, sort=Q, nonumberlist}

\newglossaryentry{vector-space}{name=\ensuremath{V},
  description={a vector space}, sort=V}

\makeglossaries

\begin{document}
A vector space \gls{vector-space} \ldots
\lipsum[1-10]

\glsaddallunused
\printglossary[style=tabx3col]
\end{document}

image of glossary

If any of your entries contain references to other entries (for example, if \gls or \glslink etc appear in the description) then this will add a page number to the number list. If this occurs after \glsaddallunused this can cause the number list to be \glsignore{pagenum1},pagenum2 which results in a spurious comma. So it's best to always place \glsaddallunused at the very end immediately before \end{document}:

\documentclass{article}

\usepackage{amsfonts}
\newcommand*{\Z}{\mathbb{Z}}
\newcommand*{\Q}{\mathbb{Q}}

\usepackage{lipsum} % to have some dummy text, and therefore larger page numbers
\usepackage[colorlinks]{hyperref}
\usepackage{glossaries}
\usepackage{glossary-longragged}

\glsdisablehyper %Entfernt links von den Symbolen


\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\newglossarystyle{tabx3col}{%
 % put the glossary in a longtable environment:
 \renewenvironment{theglossary}%
  {\renewcommand{\arraystretch}{1.5}
  \begin{longtable}{@{}p{0.2\textwidth}@{}p{0.7\textwidth}@{}>{\raggedleft}p{0.1\textwidth}@{}}}%
  {\end{longtable}}%
 % Set the table's header:
 \renewcommand*{\glossaryheader}{}%
 % No heading between groups:
  \renewcommand*{\glsgroupheading}[1]{}%
 % Main (level 0) entries displayed in a row:
  \renewcommand*{\glossentry}[2]{%
    \glstarget{##1}{\glossentryname{##1}}% Name
    & \glossentrydesc{##1}% Description
    & ##2% Page list
    \tabularnewline% end of row
  }%
  % make sub-entries the same 
  \renewcommand*{\subglossentry}[3]{%
    \glossentry{##2}{##3}%
  }%
  % no group separator
  \renewcommand*{\glsgroupskip}{}%
}

\newglossaryentry{integers}{name=\ensuremath{\Z},
  description={the ring of integers}, sort=Z}

\newglossaryentry{rationals}{name=\ensuremath{\Q},
  description={the field of rational numbers}, sort=Q, nonumberlist}

\newglossaryentry{vector-space}{name=\ensuremath{V},
  description={a vector space}, sort=V}

\makeglossaries

\begin{document}
A vector space \gls{vector-space} \ldots
\lipsum[1-10]

\printglossary[style=tabx3col]
\glsaddallunused
\end{document}

Alternatively

\AtEndDocument{\glsaddallunused}
  • Thank you for your answer. It looks better, but if I run your MWE the glossaryentries are not in the right row of the table. The V is now in the first coloum but not of the same height as it's description... – Frank Oct 3 '17 at 11:34
  • @Frank The redefinition of \glsgroupskip was missing from the style. I've fixed the MWE. – Nicola Talbot Oct 3 '17 at 13:33
  • Thank you Nicola for this perfect solution! This was my first approach defining a glossary style and I've learnd a lot with your improvements! – Frank Oct 4 '17 at 8:51
  • Is it possible to use a command like \glslink such that I can print an alternative text insted of the glossaryentry and the link in the glossary goes then to the page with this alternative text? – Frank Oct 4 '17 at 13:17
  • @Frank yes, \glslink{label}{text} will index the page on which it occurs, so that page will be included in the page list for the entry with the given label. You can also use \glsdisp{label}{text} which works in a similar way but also marks the entry as "used" (which glslink doesn't do). – Nicola Talbot Oct 6 '17 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.