6

My following Latex code generated the table as shown in the image. I want the top, bottom and midrule to extend until the margin. Since I use resize option, the table is resized but not the rules. Without changing the existing code much, I would like to know how can I draw the rules, such that the table fits within the margin.

\newcommand\mcc[1]{\multicolumn{2}{>{\Centering}p{2.9cm}}{#1}}
\begin{table*}
% \centering
\setlength\tabcolsep{3pt}
\caption{Performance Summary}
\resizebox{0.73\textwidth}{!}{
\begin{tabularx}
{\textwidth}{@{}l *{8}{p{1.45cm}<{\centering}}@{}}      
\toprule
Methodology Details & \mcc{Output prediction accuracy} & \mcc{Parameter estimation accuracy} & \mcc{Training time} & \mcc{Parameter count}\\
\cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} \cmidrule(lr){8-9}
& Sim. & Meas. & Sim. & Meas. & Sim. & Meas. & Sim. & Meas. \\
\midrule
Method 1 & \textcolor{purple}{$\approx$} & \textcolor{green}{\checkmark} & \textcolor{green}{\checkmark} & \textcolor{green}{\checkmark} & \textcolor{green}{\checkmark} & \textcolor{green}{\checkmark} & \textcolor{red}{$\times$} & \textcolor{red}{$\times$} \\
Method 2 & \textcolor{purple}{$\approx$} & \textcolor{green}{\checkmark} & \textcolor{green}{\checkmark} & \textcolor{green}{\checkmark} & \textcolor{green}{\checkmark} & \textcolor{green}{\checkmark} & \textcolor{purple}{$\approx$} & \textcolor{red}{$\times$} \\
\bottomrule
\end{tabularx}
}
\noindent\parbox{\textwidth}{
\vspace{1mm}
{\footnotesize
Legend (compared to baseline): 
\textcolor{green}{\checkmark} = better, 
\textcolor{purple}{$\approx$} = approximately equal, 
\textcolor{red}{$\times$} = worse, 
-- = not applicable.}
}
\end{table*}

enter image description here

5
  • 5
    Use at least one X column if you use tabularx. And don't scale elements which contain text, choose a suitable font size instead of change the table layout. Commented yesterday
  • 5
    remove \resizebox{0.73\textwidth}{!}{ (never scale tables that way) and use \tabularx{\textwidth} so the rules are full width. Commented yesterday
  • Thank you for your suggestions. Without resizebox option, the table is extended beyond margin and changing the fontsize upto \scriptsize doesn't help to fit the table within the margin
    – Neuling
    Commented yesterday
  • Off-topic: p{1.45cm}<{\centering} isn't right. Try >{\centering\arraybackslash}p{1.45cm} instead. Better still, use >{\centering\arraybackslash}X.
    – Mico
    Commented yesterday
  • The table is just too wide as you are forcing the width using p columns so not allowing tabularx to work as there is no X column, Commented 12 hours ago

3 Answers 3

8

Without changing the existing code much...

I will assume that your main objective is to keep using a tabularx environment, but no longer employ the \resizebox sledgehammer. Here's a solution that continues to employ a tabularx environment, but now with 8 columns of type X. The utility macro \mcc also employs a (modified) type-X column. I would also allow automatic line breaking in the first column.

enter image description here

\documentclass[twocolumn]{article} % or some other suitable document class
\usepackage{booktabs,xcolor,tabularx}
\usepackage{ragged2e} % for '\RaggedRight' and '\Centering' macros
\usepackage{amssymb}  % for '\checkmark' macro
\usepackage{calc}     % for '\widthof' macro
% Create macros for frequently-repeated instructions:
\newcommand\purpapprox{\textcolor{purple}{$\approx$}}
\newcommand\greencheck{\textcolor{green}{\checkmark}}
\newcommand\redtimes{\textcolor{red}{$\times$}}
% Handy shortcut macro:
\newcommand\mcc[1]{\multicolumn%
    {2}{>{\Centering\hsize=\dimeval{2\hsize+2\tabcolsep}}X}{#1}}
\begin{document}
\begin{table*} % span both columns
%\setlength\tabcolsep{3pt} % not needed
\caption{Performance Summary}
\smallskip % <-- for a less cramped "look"
\begin{tabularx}{\textwidth}{@{} 
         >{\RaggedRight}p{\widthof{Methodology}} 
         *{8}{>{\Centering}X} @{}}      
\toprule
Methodology Details & 
\mcc{Output prediction accuracy} & \mcc{Parameter estimation accuracy} & 
\mcc{Training time} & \mcc{Parameter count}\\
\cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} \cmidrule(l){8-9}
& Sim. & Meas. & Sim. & Meas. & Sim. & Meas. & Sim. & Meas. \\
\midrule
Method 1 & \purpapprox & \greencheck & \greencheck & \greencheck 
         & \greencheck & \greencheck & \redtimes   & \redtimes \\
Method 2 & \purpapprox & \greencheck & \greencheck & \greencheck 
         & \greencheck & \greencheck & \purpapprox & \redtimes \\
\bottomrule
\end{tabularx}
\medskip % for a less cramped "look"
\small   % '\footnotesize' seems excessive
Legend (compared to baseline): \greencheck\ better; 
\purpapprox\ approximately equal; \redtimes\ worse; 
-- not applicable.
\end{table*}
\end{document}
8
  • You not provide any information about used document class and your document layout. Consequently size of \textwidth is unknown. So, please, always provide MWE (Minimal Working Example), a small but complete document which we can test as it is and which reproduce your problem.

  • Since you use figure* float environment I assume, that your document has two columns. AT this be aware that in such document table will appear at top of the next page from place where you will insert table in text.

  • To my opinion use of talltblr of tabularray package enable simpler code for table

  • I also would define new commands for check marks used in table

\documentclass[twocolumn]{article}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\usepackage{amsmath}
\usepackage{pifont}
\newcommand{\cmark}{\textcolor{green}{\ding{51}}}%
\newcommand{\xmark}{\textcolor{red}{\ding{55}}}%
\newcommand{\aprx}{\textcolor{purple}{$\boldsymbol{\approx}$}}%
\begin{document}
    \begin{table*}
\begin{talltblr}[
caption = {Performance Summary in comparison to baseline.},
  label = {tab:comparison},
remark{Legend} = {\cmark = better, \aprx = approximately equal, \xmark = worse, 
                  -- = not applicable.}
                ]{colspec = {@{} l *{8}{X[b, c]} @{}},
             cell{1}{1} = {r=2}{},
             cell{1}{even[2]} = {c=2}{},
             cells = {font=\small},
             hspan=minimal
             }     
    \toprule
{Methodology\\ Details}
            &   Output prediction accuracy
                    &       &   Parameter estimation accuracy 
                                    &       &   {Training\\ time} 
                                                    &       &   {Parameter\\ count}   
                                                                    &       \\
    \cmidrule[lr]{2-3} \cmidrule[lr]{4-5} 
    \cmidrule[lr]{6-7} \cmidrule[lr]{8-9}
            & Sim.  & Meas. & Sim.  & Meas. & Sim.  & Meas. & Sim.  & Meas. \\
\midrule
Method 1    & \aprx &\cmark &\cmark &\cmark &\cmark &\cmark &\xmark & \xmark \\
Method 2    & \aprx &\cmark &\cmark &\cmark &\cmark &\cmark & \aprx & \xmark \\
    \bottomrule
\end{talltblr}
    \end{table*}
\end{document}

enter image description here

5

I'd go with tabular*. You only need to measure the width of the first column.

Small size fits the standard textwidth.

\documentclass{article}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage{xcolor}
\usepackage{caption} % to have captions above tables
\usepackage{booktabs}
\usepackage{amssymb}
\newlength{\tabmodule}
\begin{document}
\begin{table*}
\small
\setlength\tabcolsep{0pt}
\settowidth{\tabmodule}{Methodology}
\setlength{\tabmodule}{\dimeval{(0.95\textwidth-\tabmodule)/8}}
\newcommand\mcc[1]{\multicolumn{2}{>{\centering}p{\dimeval{2\tabmodule}}}{#1}}
\newcommand{\aeq}{\textcolor{purple}{$\approx$}}
\newcommand{\better}{\textcolor{green!80!blue}{\checkmark}}
\newcommand{\worse}{\textcolor{red!90!blue}{$\times$}}
\caption{Performance Summary}
\begin{tabular*}{\textwidth}{
  @{\extracolsep{\fill}}
  l *{8}{w{c}{\tabmodule}}
}
\toprule
\smash{\begin{tabular}[t]{@{}c@{}}Methodology \\ Details \end{tabular}}
& \mcc{Output prediction accuracy}
& \mcc{Parameter estimation accuracy}
& \mcc{Training time}
& \mcc{Parameter count}\\
\cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} \cmidrule(l){8-9}
& Sim. & Meas. & Sim. & Meas. & Sim. & Meas. & Sim. & Meas. \\
\midrule
Method 1 & \aeq & \better & \better & \better & \better & \better & \worse & \worse \\
Method 2 & \aeq & \better & \better & \better & \better & \better & \aeq & \worse \\
\bottomrule
\end{tabular*}
\vspace{1mm}
\footnotesize
Legend (compared to baseline): 
\better\ = better, 
\aeq\ = approximately equal, 
\worse\ = worse, 
-- = not applicable.
\end{table*}
\end{document}

output

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.