Glossaries in tabularx

I'm trying to get a glossarystyle working with tabularx but run into some issues. I have seen people claiming both they have been able to make it work and others saying glossaries will never work with tabularx (I haven't seen an explanation as to why).

I've put together a MWE showing the issue:

\documentclass[10pt, a4paper]{article}

\usepackage{tabularx}
\usepackage{hyperref}
\usepackage{glossaries}

\makeglossaries
\newglossarystyle{glostable}
{%
\renewenvironment{theglossary}%
{\begin{tabularx}{\linewidth}{ll}}%
{\end{tabularx}}%
\renewcommand*{\glsgroupskip}{}%
\renewcommand{\glossentry}[2]{%
\glossentryname{##1} & \glossentrydesc{##1} \\%
}%
\renewcommand*{\subglossentry}[3]{}%
}

\setglossarystyle{glostable}

\newglossaryentry{latex}
{
name=latex,
description={Is a mark up language specially suited for scientific documents}
}

\longnewglossaryentry{HTML}
{
name={Hypertext Markup Language (HTML)},
description={Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScript.},
first={Hypertext Markup Language (HTML)},
text={HTML}
}

\begin{document}


When I try to compile this document (assume makeglossaries has run successfully), I see the following error in the log

[...]
Runaway argument?
\glossarypostamble
! File ended while scanning use of \TX@get@body.
<inserted text>
\par
l.45 \printglossaries

?
! Emergency stop.
[...]


I have tried with longtable instead, but I can't get it to look as nice as the other tables, especially the columns with long texts.

Some notes:

• I know the ll columns don't make much sense in the MWE, but I tried to keep it as simple as possible here
• I want to usetabularx because:
• that is what I use in the rest of the document
• I have items with long descriptions and tabularx gives me nice controls over columns
• I use it in combination with ltablex for repeating headers
• Probably you could make use of the alttree style. See also tex.stackexchange.com/a/392873/134144 – leandriis Jun 9 '20 at 21:24
• Hi Leandris, thanks for your reply. I know that there are some styles that get somewhat close to what I want. But I'd really like to know why my MWE fails and how (if at all) it can be fixed while keeping tabularx – SeeDoubleYou Jun 10 '20 at 6:29

You can't use \begin{tabularx} and \end{tabularx} inside of new environment definitions. Use \tabularx and \endtabularx instead.

A hint on that can be found in page 4 of the tabularx manual:

Allow \tabularx \endtabularx(but not \begin{tabularx} \end{tabularx}) to be used in \newenvironment definitions

Changing you definition to

\newglossarystyle{glostable}
{%
\renewenvironment{theglossary}%
{\tabularx{\linewidth}{lX}}%
{\endtabularx}%
\renewcommand*{\glsgroupskip}{}%
\renewcommand{\glossentry}[2]{%
\glossentryname{##1} & \glossentrydesc{##1} \\%
}%
\renewcommand*{\subglossentry}[3]{}%
}


should therefore make your code compilable.

If you want your glossary to occupy more than one page you might have to switch to a different approach. If you wish to stick with a twocolumn table with X type columns but want to allow mid table pagebreaks, you could give xltabular a try. Just like with the tabularx package, use \xltabular and \endxltabular inside of your environment definition instead of the usual \begin{xltabular} and \end{xltabular}. A different alternative might be switching to a list-like style such as a customized alttree style.

• Thanks, makes sense. However, when I try your suggestion and then compile, this gives me this error \begin{theglossary} on input line 2 ended by \end{tabularx}. I think this may have to do with this line from the manual: There are some restrictions on this usage, the principal one being that \endtabularx must not be inside any { } pairs ... – SeeDoubleYou Jun 11 '20 at 13:39
• @SeeDoubleYou: I can only reproduce the error message you quoted when using \renewenvironment{theglossary}% {\tabularx{\linewidth}{ll}}% {\end{tabularx}}% instead of \renewenvironment{theglossary}% {\tabularx{\linewidth}{ll}}% {\endtabularx}% that is in my answer. – leandriis Jun 11 '20 at 13:42
• That's weird. I have the exact thing as in your answer but it fails in my full document. It works in the minimal example though. So there must be something else breaking it. I guess I have some more debugging to do. Thanks for the explanation! Update: found the culprit. I use ltablex as well, which causes the error – SeeDoubleYou Jun 11 '20 at 13:56
• @SeeDoubleYou: Interesting that ltablex breaks the code. xltabularx might be an alternative. – leandriis Jun 11 '20 at 14:28

To those getting here, I want to add how I solved this, in the end. I'll leave @leandriis's answer as the accepted one, since it explains why the mwe in my question didn't work and he proposed a working solution.

However, as you can see in my comment, the solutions stops working when you use tabularx together with ltablex to get longtable capabilities, it stops working again. So I looked into xltabular as Leandriis suggested and... ran into the same issues. However, while looking into xltabular and glossaries I came across this answer: https://tex.stackexchange.com/a/539737/45330 that proposed the use of the environ package. It was actually even based on this answer that uses the same technique with tabularx(!): https://tex.stackexchange.com/a/343239/45330

So I ended up with something like this:

\NewEnviron{glostableenviron}{%
\begin{tabularx}{\linewidth}{lX}%
\BODY
\end{tabularx}
\addtocounter{table}{-1} % prevent table counter to go up
}

\newglossarystyle{glostable}
{%
\renewenvironment{theglossary}%
{\glostableenviron}%
{\endglostableenviron}%