1

I tried using multiple tabularx environments inside a table environment, but this leads to issues with the entire text being aligned towards the left and not matching with the column headings, which in this example below are V1, V2, V3:

I'm getting the error for the following code (I tried removing \hline but that doesn't help):

Misplaced \noalign.: You have used a \hline command in the wrong place, probably outside a table. If the \hline command is written inside a table, try including \\ before it.
\begin{table}[]
\begin{tabular*}{\textwidth}{SSSS} \hline
    {$ $} & {$metric$} & {$metric$} & {$metric$} \\ \hline
    \textbf{class 1}  & 0 & 0 & 0 \\
    \midrule
    \textbf{HEADING}
    \midrule
    \textbf{model (1)}  & 0  &  0  &  0\\ 
    \textbf{model (1)}  & 0  &  0   & 0  \\
    \textbf{model (1)}  & 0   & 0  &  0  \\
    \textbf{model (1)}  & 0  &  0 &  0 \\
    \midrule
    \textbf{model (1)}  & 0  &  0  &  0\\ 
    \textbf{model (1)}  & 0  &  0   & 0  \\
    \textbf{model (1)}  & 0   & 0  &  0  \\
    \textbf{model (1)}  & 0  &  0 &  0 \\
    \midrule
    \textbf{HEADING}
    \midrule
    \textbf{model (1)}  & 0  &  0  &  0 \\ 
    \textbf{model (1)}  & 0  &  0   & 0  \\
    \textbf{model (1)}  & 0   & 0  &  0  \\
    \textbf{model (1)}  & 0  &  0 &  0 \\
    \midrule
    \textbf{model (1)}  & 0  &  0  &  0\\ 
    \textbf{model (1)}  & 0  &  0   & 0  \\
    \textbf{model (1)}  & 0   & 0  &  0  \\
    \textbf{model (1)}  & 0  &  0 &  0 \\
    \bottomrule
\end{tabular*}


\caption{caption here}
\label{table:results}

\end{table}

enter image description here

Simon Dispa
  • 39,141
  • if you are getting errors with your tex code someone will help, but you need to show the code, add a small complete document that shows the problem. tabularx X columns are p columns so you can center text with \centering\arraybackslash – David Carlisle Feb 17 '21 at 00:18
  • @DavidCarlisle thanks, I've added the tex code and error I'm getting. – latexuser Feb 17 '21 at 00:24
  • 1
    we can not run your fragment as it is incomplete, but in your title you mention tabularx but the code fragment usestabular* but a tabular* will not expand unless you use \extracolsep . The error you show is unrelated and simply that you have forgotten the \\ after \textbf{HEADING} – David Carlisle Feb 17 '21 at 00:47
  • Please see my answer with a comment on the problem of underline. – Simon Dispa Feb 22 '21 at 16:26

2 Answers2

4

I believe you intended something like this (note you are not using tabularx at all. Personally I would use a normal tabular here: stretching the columns to full with just makes the table harder to read, but this shows it full width.

enter image description here


\documentclass{article}

\usepackage{siunitx} \usepackage{array,booktabs}

\begin{document} \begin{table}% don't use an empty argument []

\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lSSS@{}} \\\toprule

% don't use math mode to get italics!!!! % {$ $} & {$metric$} & {$metric$} & {$metric$} \ \hline & {\textit{metric}}&{\textit{metric}}&{\textit{metric}}\\midrule \textbf{class 1} & 0 & 0 & 0 \ \midrule &\multicolumn{3}{c}{\textbf{HEADING}}\ \midrule \textbf{model (1)} & 0 & 0 & 0\ \textbf{model (1)} & 0 & 0 & 0 \ \textbf{model (1)} & 0 & 0 & 0 \ \textbf{model (1)} & 0 & 0 & 0 \ \midrule \textbf{model (1)} & 0 & 0 & 0\ \textbf{model (1)} & 0 & 0 & 0 \ \textbf{model (1)} & 0 & 0 & 0 \ \textbf{model (1)} & 0 & 0 & 0 \ \midrule &\multicolumn{3}{c}{\textbf{HEADING}}\ \midrule \textbf{model (1)} & 0 & 0 & 0 \ \textbf{model (1)} & 0 & 0 & 0 \ \textbf{model (1)} & 0 & 0 & 0 \ \textbf{model (1)} & 0 & 0 & 0 \ \midrule \textbf{model (1)} & 0 & 0 & 0\ \textbf{model (1)} & 0 & 0 & 0 \ \textbf{model (1)} & 0 & 0 & 0 \ \textbf{model (1)} & 0 & 0 & 0 \ \bottomrule \end{tabular*}

\caption{caption here}
\label{table:results}

\end{table}

\end{document}

If instead you use

   \centering
   \begin{tabular}{lSSS} \\\toprule

Then it looks like

enter image description here

David Carlisle
  • 757,742
  • Thanks so much! I just have a quick follow up, when I underline things it shifts the element underlined, how do i prevent that? – latexuser Feb 17 '21 at 03:16
1

This alternative uses the package nicematrix. I followed the general design of the drawing with a vertical line.

Vertical lines are generally discouraged in technical publications, but of course they are useful in many situations. Some packages have difficulty joining vertical and horizontal lines (see the second figure). This is not the case with nicematrix.

I added a new column type to allow the expanding of the last 3 columns, as a percentage of the text width.

Finally, several elements were underlined without noticeable consequences. Note also the correct centering of "metric".

NOTE To underline under S columns use \uline from the ulem package. (loaded as \usepackage[normalem]{ulem}). See https://tex.stackexchange.com/a/159634/161015

A

Problems when joining lines

x

\documentclass{article}

\usepackage{siunitx} \usepackage{array,booktabs}

\usepackage{nicematrix}

\begin{document}

\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1\textwidth}} % size of the column as a percent of textwidth

\NiceMatrixOptions{cell-space-top-limit = 5pt,cell-space-bottom-limit = 5pt} %expand the cells vertically and horizontally

\begin{table} \centering \begin{NiceTabular}{>\bfseries{c} | P{0.2} P{0.2} P{0.2}} % first column in bf & \textit{metric}&\textit{metric}&\textit{\underline{metric}}\ \midrule \textbf{class 1} & 0 & 0 & 0 \ \midrule &\Block{1-3}{\textbf{\underline{HEADING}}}\ \midrule model (1) & 0 & 0 & 0 \ \underline{model (1)} & 0 & 0 & 0 \ model (1) & 0 & 0 & 0 \ model (1) & 0 & 0 & 0 \ \midrule model (1) & 0 & 0 & 0 \ model (1) & 0 & 0 & 0 \ model (1) & 0 & 0 & 0 \ model (1) & \underline{0} & 0 & 0 \ \midrule &\Block{1-3}{\textbf{HEADING}}\ \midrule model (1) & 0 & 0 & 0 \ model (1) & 0 & 0 & 0 \ model (1) & 0 & 0 & 0 \ model (1) & 0 & 0 & 0 \ \midrule model (1) & 0 & 0 & 0 \ model (1) & 0 & 0 & 0 \ model (1) & 0 & \underline{0} & 0 \ model (1) & 0 & 0 & 0 \ \bottomrule \end{NiceTabular}

\caption{caption here}
\label{table:results}

\end{table}

\end{document}

Simon Dispa
  • 39,141