2

I have a general question. I would like to center tables on the page, when I do not know the width. What is more, I am using tabularx and sometimes the width specified may be too small, meaning that the table is wider than the first argument to tabularx.

Here is some context for my question. I develop an R package which prints out tables in HTML, LaTeX etc. So, users may set the width - perhaps to a value which is too small for the content.

At the moment, a centered table uses \centering, like this (simplified version):

\begin{table}[h]
\centering
\begin{threeparttable}
\begin{tabularx}{0.5\textwidth}{p{0.5\textwidth}}

\multicolumn{1}{l}{Some content here} \tabularnewline[-0.5pt]

\end{tabularx}\end{threeparttable}

\end{table}

But that can lead to tables not being truly centered. Here's an example where the user has specified a too small table width for a wide table. As a result the table is not centered:

\documentclass{article}

\usepackage{threeparttable}
\usepackage{tabularx}
\usepackage{multirow}
\begin{document}
\begin{table}[h]
\centering
\begin{threeparttable}
\begin{tabularx}{0.2\textwidth}{p{0.2\textwidth}}

\multicolumn{1}{l}{Some very long content that goes on and on and on} \tabularnewline[-0.5pt]


\end{tabularx}\end{threeparttable}

\end{table}
\end{document}

Is there a way to make sure that tables stay centered?

Note that the multicolumn command overrides the table width specification. I know about this, and it is probably not optimal. But by default, my cells aren't wrapped. If a user wants they can turn wrapping on, in which case the multicolumn command would have a width spec of p{0.2\textwidth} or whatever. The point is, sometimes users may do things that make tables wider than tabularx thinks they are; is there a way to ensure that nevertheless, they are truly centered?

(Note 2: yes, I also know that in this case, multicolumn is not doing anything. But sometimes there are genuine multicolumn cells; the command allows per-cell left/right/top/bottom-alignment; and writing TeX programmatically from a different language is hard.)

4
  • 1
    \begin{tabularx}{0.5\textwidth}{p(0.5\textwidth}} makes very little sense: tabularx is only useful if you specify at least an X column.
    – egreg
    Nov 26 '18 at 21:37
  • on your "Note 2": the multicolumn in this case is doing something, it completely removes the p column specification (since no cell in the table uses it.) Nov 26 '18 at 23:38
  • 1
    I would say that is well centered table of 0.2\textwidth width and a unbreakable long cell that could not be fit on that width, forcing the column to enlarge to be right side that still are well centered in the left side, i.e., the problem is not center the table, but ensure that you do not modify the pre-fixed width of a tabularx . However, note that with tabular and even with tabulary that nonsense cell is not a problem to center the final fattened width of the table.
    – Fran
    Nov 27 '18 at 0:15
  • Thank you for the comment. I appreciate that conceptually, the table may be well centered. However, as it appears to the reader, it isn't :-) Is there a way to specify table width using tabular and/or tabulary? (Allowing users to specify the width is another constraint I face...)
    – dash2
    Nov 27 '18 at 5:11
3

A tabularx has to have at least one X column, a specification such as

\begin{tabularx}{0.2\textwidth}{p{0.2\textwidth}}

specifies the table is to be placed in a box of width .2\textwidth but has no way to make any adjustment to the width as changing the width of X columns is the only mechanism tabularx has. This table is setting particularly impossible constraints as not only is there no X column, the table specification just has a p{0.2\textwidth} column so its natural width is .2\textwidth+2\tabcolsep as there is \tabcolsep either side of the column. In the example the p column is replaced by l as all entries are spanned.

You should just use tabular here:

enter image description here

\documentclass{article}

\usepackage{threeparttable}
\usepackage{tabularx}
\usepackage{multirow}
\begin{document}


\noindent X\dotfill X % just to show the width

\begin{table}[htp]% never use [h] on its own
\centering
\begin{threeparttable}
\begin{tabular}{p{0.2\textwidth}}

\multicolumn{1}{l}{Some very long content that goes on and on and on} \tabularnewline[-0.5pt]


\end{tabular}
\end{threeparttable}

\end{table}
\end{document}
3
  • Thank you for the point about the X specification. If I use tabular, is there a way to specify the width of the table? If not, is there a way to get the table centred on the page using tabularx? Even though, as you say, it is overrunning? It sounds as if implicitly your answer is no - I just want to make sure.
    – dash2
    Nov 27 '18 at 5:09
  • @dash2 the tabularx was centred. tabularx always makes a box of the specified size, if you have user errors so the content of that box can not possibly be that size then tex will report an over-full box, but the tabularx itself is the size you specified and is centred as that size (with, as always overfull material sticking out to the right) Nov 27 '18 at 9:08
  • Thank you for the point, though it does slightly remind me of en.wikipedia.org/wiki/An_Oak_Tree ... think the answer is that I need to stop abusing tabularx and move to tabu or tabulary.
    – dash2
    Nov 27 '18 at 9:30

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.