1

I'm trying to create a multi-columned table to provide some summary of data, the code below is what I currently have.

\begin{figure}[H]
\begin{center}
\begin{tabularx}{0.5\textwidth}{|l||ll||ll|}
\hline
             & \multicolumn{2}{l}{Top Column 1} & \multicolumn{2}{l}{Top Column 2}      \\  \hline
ROW 1        &                     &                   &               &                \\ \hline
ROW 2        &                     &                   &               &                \\ \hline
ROW 3        &                     &                   &               &                \\ \hline
ROW 4        &                     &                   &               &                \\ \hline
ROW 5        &                     &                   &               &                \\ \hline
ROW 6        &                     &                   &               &                \\ \hline
\end{tabularx}
\end{center}
\caption{}
\end{figure}

However in the resulting table, the horizontal lines do not span the entire row.

enter image description here

I suspect this is due to my headers (i.e. the use of 'l' and the separators), but I can't find a solution despite looking only. Why do the horizontal lines not span the entire row length?

  • I can't reproduce your issue when wrapping a document around your fragment (which is your job, actually). Also, the given fragment does not have the Yes No etc. content like the screenshot... – user31729 Mar 15 '18 at 18:46
  • 1
    tabularx tables must have at least one X column otherwise it has no way to make the table the specified width. But data tables with no linebreaking in the cells it is better to just use a standard tabular – David Carlisle Mar 15 '18 at 19:03
1

Try this:

  \begin{center}
  \begin{tabular}{|c||p{1cm}|p{1cm}||p{1cm}|p{1cm}|}\hline %\cline{2-5}
   & \multicolumn{2}{c||}{Top Column 1}&\multicolumn{2}{c|}{Top Column 2}\\\hline
   Row 1 & Yes & No & Yes & No \\ \hline
   Row 2 & 124 & 6 & 124 & 6 \\ \hline
   Row 3 & 124 & 6 & 124 & 6 \\ \hline
   Row 4 & 124 & 6 & 124 & 6 \\ \hline
   Row 5 & 124 & 6 & 124 & 6 \\ \hline
   Row 6 & 124 & 6 & 124 & 6 \\ \hline
  \end{tabular}
  \end{center}

enter image description here

Maybe it fits for you better ;)

0
  • as pointed in comment David Carlisle comment above, tabularx had to have at least one X column:
  • don't use H for table placement, it can make unexpected problems with white space on a the bottom of a page in table can't float
  • don't use \begin{center} ... \end{center} in float environment. it add unnecessary vertical space above and below table
  • i suggest to omit vertical rules and for horizontal use rules from the package booktabs

\documentclass{article}
\usepackage{booktabs, tabularx}

\begin{document}
\begin{figure}[htb]
  \centering
\begin{tabularx}{0.6\linewidth}{@{}l *{4}{>{\centering\arraybackslash}X}@{}}
    \toprule
        & \multicolumn{2}{c}{Top Column 1}
                    &\multicolumn{2}{c}{Top Column 2}   \\
    \cmidrule(lr){2-3}\cmidrule(lr){4-5}
Row 1   & Yes   & No    & Yes   & No    \\
    \midrule
Row 2   & 124   & 6     & 124   & 6     \\
Row 3   & 124   & 6     & 124   & 6     \\
Row 4   & 124   & 6     & 124   & 6     \\
Row 5   & 124   & 6     & 124   & 6     \\
Row 6   & 124   & 6     & 124   & 6     \\
    \bottomrule
\end{tabularx}
  \end{figure}
\end{document}

enter image description here

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.