0

I have a need to automatically set the width of a tabular environment; for certain reasons the tabularc/x/y environment won't work quite right. I have some code that uses \settowidth to measure the width of a sample table; I can then build on that to do the calculation I need. Unfortunately, though, \settowidth doesn't seem to produce consistent results. In my (large and complex) real document, the calculated width omits the width of the rules at the left and right sides of the tables; this eventually produces an overfull hbox.

The following short file demonstrates the problem. If you format the file (with either latex or pdflatex) and view a magnified result, you'll see that the horizontal rule isn't quite as wide as the table, while the vertical rule exactly matches the height. I've included a screenshot of the lower-left corner as an example of how it looks:

Surely this isn't a bug? Yet there's clearly more ink that \settowidth claims...

enter image description here

\documentclass{article}

\begin{document}

\newcommand{\tabletest}{%
    \begin{tabular}[b]{|l|r|}
        \hline
        Some & data\\
        \hline
        Some more & stuff\\
        \hline
    \end{tabular}%
}

\newlength{\tabledepth}
\newlength{\tableheight}
\newlength{\tablewidth}
\settodepth{\tabledepth}{\tabletest}
\settoheight{\tableheight}{\tabletest}
\settowidth{\tablewidth}{\tabletest}

\addtolength{\tableheight}{\tabledepth}

\noindent\rule{4pt}{\tableheight}\hspace*{2pt}\tabletest
\\[-6pt]
\noindent\hspace*{6pt}\rule{\tablewidth}{4pt}

\end{document}
2
  • 1
    Just load the array package, what do you observe?
    – AboAmmar
    May 4, 2020 at 1:38
  • The vertical rules take up zero width. This may cause them to bump into text, but will not cause overfull hbox errors! (Funny, I recalled that tabular \hlines also took up zero height.) By the way, there is \settototalheight so you don't need to add depth plus height. May 4, 2020 at 4:46

1 Answer 1

1

The array package implements tabular rules differently than TeX, it takes care of horizontal/vertical rule width.

\documentclass{article}
\usepackage{array}
\begin{document}

\newcommand{\tabletest}{%
    \begin{tabular}[b]{|l|r|}
        \hline
        Some & data\\
        \hline
        Some more & stuff\\
        \hline
    \end{tabular}%
}

\newlength{\tabledepth}
\newlength{\tableheight}
\newlength{\tablewidth}
\settodepth{\tabledepth}{\tabletest}
\settoheight{\tableheight}{\tabletest}
\settowidth{\tablewidth}{\tabletest}

\addtolength{\tableheight}{\tabledepth}

\noindent\rule{4pt}{\tableheight}\hspace*{2pt}\tabletest
\\[-6pt]
\noindent\hspace*{6pt}\rule{\tablewidth}{4pt}

\end{document}

enter image description here

1
  • Thanks for the \settotalheight tip; I had somehow missed its existence. I had tried the array package for other reasons, but unfortunately it completely breaks my document because I do quite a bit of dynamically constructing arguments to the tabular environment. I guess I could (a) look into the overfull hboxes more carefully, if the rules are supposed to take zero width, and (b) try harder with the array environment and lots of \expandafter. Or I could just ignore the overfull hbox messages, which would annoy me but is actually fairly harmless in the real world.
    – gkuenning
    May 5, 2020 at 7:06

You must log in to answer this question.

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