0

I'm having trouble getting two tables to sit nicely next to each other. I get errors like this:
Overfull \hbox (255.00095pt too wide) in paragraph at lines 9--35
It looks like this:


enter image description here


It seems to insist on this big stupid blank spot (much wider than the margin) that takes up the first quarter of the page, and causes the fourth quarter of the content to fall off the edge, despite using \centering, \begin{centered}, etc.

You can see I tried a few different things (commented out).
Here's the code:

\documentclass[varwidth]{article}

\usepackage{booktabs}

\begin{document}

% \begin{table}
% \begin{minipage}[t][29.7cm][c]{21cm}
\begin{tabular}{cc}
\begin{tabular}{cccc}
\toprule
            &   $X$       &   $X$       &   $XXX$     \\
\midrule 
XXXXXX      &   XXXXXXXX    &   XXXXXXXX    &   XXXXXXXX  \\
\midrule
XXXXXXX     &   XXX         &   XXX         &   XXX       \\
\bottomrule
\end{tabular} &
    % \caption{A}\label{tab:first}
%   \end{minipage}%
% \begin{minipage}[t][29.7cm][c]{21cm}
\begin{tabular}{cccc}
\toprule
            &   $X$       &   $X$       &   $XXX$     \\
\midrule 
XXXXXX      &   XXXXXXXX    &   XXXXXXXX    &   XXXXXXXX  \\
\midrule
XXXXXXX     &   XXX         &   XXX         &   XXX       \\
\bottomrule
\end{tabular} 
\end{tabular}
    % \caption{B}\label{tab:second}
%   \end{minipage}
% \end{table}
\end{document}

So, I have two tables side by side and they overflow into the righthand margin. How can I split the difference between both margins (bleed evenly into each) to keep the tables centered? Just to clear up any confusion, here are a couple of mockups to show the kind of thing I'm going for.

enter image description here

voices
  • 2,039
  • 4
    The two tables are actually too wide to fit side by side with LaTeX's default margins. Add \usepackage[margin=5mm,showframe]{geometry} to your example and it will fit. (You'll also need a \noindent in front of your first tabular environment if you don't put everything in a table environment.) – David Purton Aug 07 '19 at 04:14
  • The "big stupid blank spot" is in fact the left margin plus the width of the usual parindent. If you want to make the margins visible, you can use \usepackage{showframe} in your preamble. – leandriis Aug 07 '19 at 09:04
  • @leandriis No need for air quotes. It's big. It's stupid. It's blank. Thanks though, that's actually pretty useful. I can't believe the default margins are so big; they take up almost half the entire page. It would be more useful to remain centered; just split the difference and spill into both margins by default. – voices Aug 07 '19 at 09:58
  • https://tex.stackexchange.com/questions/71172/why-are-default-latex-margins-so-big/71211#71211 – David Carlisle Aug 07 '19 at 10:15
  • @tjt263: Regarding centering content that is wider than the textwidth and letting the excess spill equally into both margins: Center figure that is wider than \textwidth – leandriis Aug 07 '19 at 10:44
  • @DavidCarlisle That's fine. I don't have some kind of irrational prejudice against margins. Note that the current default margins look to be ~1¼" each; constituting over 54% of the area of a standard US letter. So if one of the other design goals was to waste half of every page by default, then.. no wait, it's still stupid. Still, the most frustrating/confusing part is the left margins priority over the right. Please don't take this as some kind of obscure insult or challenge to justify x or y, it's not personal. – voices Aug 07 '19 at 14:47
  • I don't know what you mean by priority there but latex doesn't have any representation of the right margin. You specify the paper size, the left margin, and the width of the text block, the right margin is just whatever is the difference between those. These settings have been in place since the first releases of latex in the 1980s and certainly the default values can not be changed, they are of course all user-settable so it isn't a big issue if you don't like them. If your document just consists of tables then making it a lot wider is reasonable, if you have paragraphs at 10pt, perhaps not. – David Carlisle Aug 07 '19 at 15:00
  • @DavidCarlisle When overfull, the left margin is respected, but the right margin is not. I don't necessarily even want to change the margin size, but if an object happens to be larger than the space between the margins, I still want to try to accommodate it. There's 1¼" of wiggle room we can use before running right off the edge. An object that is 2" too wide for the hbox will run right off the RH side and get cropped. Nobody will ever want that. Not when it could just bleed 1" into each margin and remain whole. That is what I mean by priority. – voices Aug 07 '19 at 17:14
  • @tjt263: For your desired alignment take a look at: https://tex.stackexchange.com/q/16582/134144 – leandriis Aug 07 '19 at 18:02
  • @leandriis That's the same link as before, right? They're not graphics to be imported, they're just tables. I've been trying. – voices Aug 07 '19 at 18:06
  • @tjt263: It does not make a difference if you have too wide images or too wide tables. The answers in the linked question even contain an example on how to center a too wide table: https://tex.stackexchange.com/a/154766/134144 – leandriis Aug 07 '19 at 18:13
  • @leandriis I just tried that. Maybe because it's two tables, not one. If not, I'm doing something wrong. – voices Aug 07 '19 at 18:22
  • 1
    @tjt263 that is not really anything to do with margins, a box starts where you place it (at the left of the tex block in this case) and if it is overfull then it will stick out to the right of its allotted space. If you want it to stick into the left margin you can use \hspace*{-1in} either side then it can be 2in bigger than \textwidth without being overfull. – David Carlisle Aug 07 '19 at 18:58
  • @DavidCarlisle can you show me? I've tried everything. Every time i fix one thing it breaks another. I've literally spent about 12 hours on this alone. – voices Aug 07 '19 at 19:55
  • the link leandriis gave has all the code you need. To be honest you might consider removing the snide comments about the default settings, which are chosen with care and maintained over many years – David Carlisle Aug 07 '19 at 20:00

2 Answers2

1

The margins are just the standard margins for the document class and page size (US Letter) that you have specified. You also have a paragraph indent to the left, and two lots of \tabcolsep spacing from the tabular columns. (The outer tabular is doing nothing other than add this extra space as there is no actual alignment in the outer, one row table.)

If you want the tables side by side you need to make the page wider or the tables smaller, I opted for the latter here, as well as removing the paragraph indent and the superfluous space from the nested tabluar \tabcolsep.

\documentclass
 %?[varwidth]
{article}

\usepackage{booktabs}

\begin{document}

% \begin{table}
% \begin{minipage}[t][29.7cm][c]{21cm}
%\begin{tabular}{cc}
\centering
\tiny
\setlength\tabcolsep{2.9pt}
\begin{tabular}{@{}cccc}
\toprule
            &   $X$       &   $X$       &   $XXX$     \\
\midrule 
XXXXXX      &   XXXXXXXX    &   XXXXXXXX    &   XXXXXXXX  \\
\midrule
XXXXXXX     &   XXX         &   XXX         &   XXX       \\
\bottomrule
\end{tabular}%
\begin{tabular}{cccc@{}}
\toprule
            &   $X$       &   $X$       &   $XXX$     \\
\midrule 
XXXXXX      &   XXXXXXXX    &   XXXXXXXX    &   XXXXXXXX  \\
\midrule
XXXXXXX     &   XXX         &   XXX         &   XXX       \\
\bottomrule
\end{tabular}
    % \caption{B}\label{tab:second}
%   \end{minipage}
% \end{table}

\normalsize
X\dotfill normal text block\dotfill X
\end{document}

enter image description here

David Carlisle
  • 757,742
  • Thanks, but now it's basically just one table; two halves of a whole. Is that the superfluous space you mentioned? It should look more like two individual entities arranged horizontally. Basically, there is an even number of tables and only an odd number will fit on one page and it ruins the flow of information. So I'm presenting them in pairs to utilize the space, so it's a page of tables, rather than a sparse string of tables scattered over several pages. I hope I explained it well. Does it work with captions and labels too? I commented them out just to isolate the problem. – voices Aug 07 '19 at 07:35
  • @tjt263 no I meant that your outer tabular was not aligning anything (as only one row) so the only thing it was doing is making you get twice the cell padding (\tabcolsep) on from the outer and one from the inner table. Since your "question" was mostly a complaint about spacing removing the outer table seemed to be the main point of the answer. You can of course use \caption if you put this in a table environment. However \tiny text verges on unreadable unless this is always pdf where people can scroll in, on paper I wouldn't go this small. – David Carlisle Aug 07 '19 at 08:10
  • Wait, so your suggestion is basically just use a smaller font? – voices Aug 07 '19 at 10:04
  • Your tables are wider than the page you have specified, so the choices are to make the tables smaller or the page wider. Usually in most publications the page size is a given and fixed for a document,so that only leaves making the tables smaller. If you want some other outcome you should ask for that in the question. This does not just use a smaller font size, it removes a lot of additional space that you had added (\parindentand multiple\tabcolsep`) @tjt263 – David Carlisle Aug 07 '19 at 10:10
1

The two tables need to fit side-by-side, to begin with. If your tables are wider than half the text width, they don't fit side by side.

Here's a proposal with subcaption, but note the very wide size reserved for text.

\documentclass{article}
\usepackage[margin=1cm]{geometry}

\usepackage{booktabs,subcaption}

\begin{document}

\begin{table}
\centering

\renewcommand{\thesubtable}{\Alph{subtable}}
\captionsetup[subtable]{labelformat=simple}

\small

\subcaptionbox{\label{tab:first}}{%
  \begin{tabular}{cccc}
  \toprule
          & $X$      & $X$      & $XXX$    \\
  \midrule 
  XXXXXX  & XXXXXXXX & XXXXXXXX & XXXXXXXX \\
  \midrule
  XXXXXXX & XXX      & XXX      & XXX      \\
  \bottomrule
  \end{tabular}%
}\hfill
\subcaptionbox{\label{tab:second}}{%
  \begin{tabular}{cccc}
  \toprule
          & $X$      & $X$      & $XXX$    \\
  \midrule 
  XXXXXX  & XXXXXXXX & XXXXXXXX & XXXXXXXX \\
  \midrule
  XXXXXXX & XXX      & XXX      & XXX      \\
  \bottomrule
  \end{tabular}%
}

\caption{Global caption}

\end{table}

\end{document}

enter image description here

egreg
  • 1,121,712
  • You're also saying to make the tables smaller, right? I don't care If the tables encroach the margins. If they do, I want them to remain centered. I added a picture to demonstrate. – voices Aug 07 '19 at 18:03