# glossaries: Calculating the width of custom field

For the following MWE

% arara: lualatex: { options: [ '-synctex=1', '-shell-escape' ]}
% arara: makeglossaries
% arara: lualatex: { options: [ '-synctex=1', '-shell-escape' ]}

\documentclass{elsarticle}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{calc,siunitx,booktabs,lipsum}
\usepackage[automake,stylemods,symbols,
xindy={codepage=utf8, language=greek, glsnumbers=false}
]{glossaries-extra}

\makeglossaries

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

\newglossarystyle{nameunitdesc}{%
\setlength{\glsdescwidth}{\linewidth-\glsnamewidth-\glsunitwidth-4\tabcolsep}%
\renewenvironment{theglossary}%
{\bottomrule\end{supertabular}}%
\renewcommand{\glossentry}[2]{%
\raggedright\glstarget{##1}{\glossentryname{##1}} &
\centering\glossentrysymbol{##1} &
\glossentrydesc{##1}\tabularnewline
}%
\renewcommand{\subglossentry}[3]{\glossentry{##2}{##3}}%
\renewcommand*{\glsgroupskip}{}%
}

\glsnoexpandfields

\newglossaryentry{Cp}{name={Cp},description={specific heat}, symbol={[\si{\J\per\kg\per\K}]}}

\journal{Journal name}

\begin{document}

\begin{frontmatter}

\title{Title}

\begin{abstract}

\lipsum[1]

\end{abstract}

\end{frontmatter}

\gls{Cp}

\glsFindWidestUsedAnyNameSymbol{\glsunitwidth}
\settowidth{\dimen0}{\glsgetwidestname}
\ifdim\dimen0>\glsnamewidth
\glsnamewidth=\dimen0
\fi

\printglossary[style=nameunitdesc,title=Nomenclature]

\end{document}


In this line \setlength{\glsdescwidth}{\linewidth-\glsnamewidth-\glsunitwidth-2\tabcolsep}, intuitively, I need to subtract 2\tabcolsep in order to make the description field span the whole line width, however, subtracting 4\tabcolsep results in the following correctly aligned right margin of the glossary table:

while subtracting 2\tabcolsep results in the following overflown right margin of the glossary table:

So, how to accurately calculate the needed number of \tabcolsep to be subtracted without trial and error?

This actually has nothing to do with glossaries, it is just how tabular works:

\tabcolsep is the margin that is inserted on either side of every column in the table. Thus, in the example

\begin{tabular}{ c c c }
\hline
a & b & c \\
\hline
\end{tabular}


the table extends \tabcolsep to the left of a and to the right of c (you can see the width of the table using the \hlines), while there is a space of 2\tabcolsep between a and b as well as b and c.

In your example, you remove the separations at the edges of the table using @{} and you have three columns. That makes 2 * 2\tabcolsep = 4\tabcolsep between them.

• Thanks for your explanation. Off topic question: how to remove both separations on each side of the first column in your example while keeping all the other ones? – Diaa Jul 24 at 11:06
• @Diaa You can use { @{} c @{\hspace{\tabcolsep}} c c } as the column specifier. – schtandard Jul 24 at 11:10
• Thanks for help. I am always in rush and struggle a lot with latex basics :) – Diaa Jul 24 at 11:21