1

Newbie question here. I am looking for a way to align my last column which is stretched.

Looked for an answer at: booktabs with custom column spacing

I tried using the online table generator for LaTeX, and I had a table working. The columns became shifted and I am not sure how to tweak or fix this in R Markdown using LaTeX commands. The generator feels a bit much to use, I'd rather do it myself.

As far as I know, the commands below are correct, but I am missing something.

\begin{tabular}{@{}llcc@{}} Implies the alignment of the 4 columns, right? Aligning the last column to the left does not help. It seems the stretched text from the bottom of the table causes it stretch perhaps?

\multicolumn{4}{l} States that there should be 4 columns.

So I guess the question is how do I limit the range on the bottom line, as in a new line to continue the subscript? Or just make a new line?

Adding \multicolumn{4}{l}{\textit{Italics}: significant model} returns an error.

Code:

\begin{table}[h]
\centering
\begin{tabular}{@{}llcc@{}}
\toprule
{\textbf{Parameter}} & \multicolumn{1}{l}{\textit{Null\_model}} & \multicolumn{1}{l}{\textit{Model 1}} & \multicolumn{1}{l}{\textit{Model 2}} \\ 

\midrule

Intercept          & .652            & -.976                        & -.983                   \\
Round              &   -.145         & .1082                        & .111                    \\
Treatment          &                 & \underline{5.875}            & \underline{5.653}       \\
Neuroticism        &                 &                              & .013                    \\
Neur * Treatment   &                 &                              & .019                    \\
Round * Treatment  &                 & \underline{-.955}            & \underline{-.909}       \\ 

\midrule

LogLikelihood      & -830.51         &  -826.4                      & \textbf{-825.92}        \\
Deviance           & 1661.0          & 1652.8                       & \textbf{1651.8}         \\
df residual        & \textbf{235}    & 233                          & 231                     \\
Akaike IC          & 1671.0          & \textbf{1666.8}              & 1669.8                  \\
Bayesian IC        & \textbf{1688.4} & 1691.2                       & 1701.2                  \\ 

\midrule

\multicolumn{4}{l}{\textbf{Bold}: most optimal values. \underline{Underlined}: significant predictor \textit{Italics}: significant model}                                                                                                                                                                                            
\end{tabular}
\end{table}

Shows this:

enter image description here

fleems
  • 27

2 Answers2

3

this answer not solve your problem (it is solved by @Mico answer +1), rather it gives new aspect onto your table design. it may be interesting to you.

i would emphasize bold and underlined numbers with bold text and make difference between them with use of * and ** using \tnote from the threeparttable package. cor column type i woull'd use S column from the siunitx package:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{siunitx}
\usepackage{booktabs, makecell, threeparttable}
\renewcommand\theadfont{\normalsize\itshape}
\renewcommand\theadgape{}
\usepackage{caption}

\usepackage{xcolor}
\usepackage{etoolbox}

\begin{document}
    \begin{table}[h]
    \centering
\renewcommand{\bfseries}{\color{teal!30!black}\fontseries{b}\selectfont}
\robustify\bfseries
\newrobustcmd\B{\bfseries}
    \begin{threeparttable}
\begin{tabular}{@{}l*{3}{S[table-format=4.3,
                           group-four-digits,
                           mode=text,
                           detect-weight,
                           table-align-text-post=false,
                           table-space-text-post={**}]}
                @{}}
    \toprule
        &   \multicolumn{3}{c}{Significant models}                       \\
    \cmidrule{2-4}
\textbf{Parameter}
            &   {\thead{Null\_model}}
                        &   {\thead{Model 1}}
                                &   {\thead{Model 2}}                   \\
    \midrule
Intercept   &   .652    & -.976 & -.983                                 \\
Round       &   -.145   & .1082 & .111                                  \\
Treatment   &           & \B  5.875\tnote{**}   & \B 5.653\tnote{**}    \\
Neuroticism &           &                       & .013                  \\
Neur * Treatment
            &           &                       & .019                  \\
Round * Treatment
            &           & \B 955\tnote{**}      & \B -.909\tnote{**}    \\
\midrule
LogLikelihood
            & -830.51   &  -826.4               & \B -825.92\tnote{*}   \\
Deviance    & 1661.0    & 1652.8                & \B 1651.8\tnote{*}    \\
df residual & \B 235\tnote{*}
                        & 233                   & 231                   \\
Akaike IC   & 1671.0    & \B 1666.8\tnote{*}    & 1669.8                \\
Bayesian IC & \B 1688.4\tnote{*}
                        & 1691.2                & 1701.2                \\
    \midrule
\end{tabular}
\begin{tablenotes}[para]\footnotesize
    \item[*]:   most optimal values.
    \item[**]:  significant predictor
\end{tablenotes}
\end{threeparttable}
    \end{table}
\end{document}

which gives:

enter image description here

Zarko
  • 296,517
  • Thanks for your comment, the code does look fancier. I'll have a look how to apply some to my current table(s)! – fleems Apr 14 '19 at 13:02
1

The string

\textbf{Bold}: most optimal values. \underline{Underlined}: significant predictor \textit{Italics}: significant model

is quite long. Importantly, much wider than the natural width of the 4-column tabular environment. In consequence, the fourth and final column has to be made much wider than its natural width.

How to fix this? I suggest you replace

\multicolumn{4}{l}{\textbf{Bold}: most optimal values. \underline{Underlined}: significant predictor \textit{Italics}: significant model}

with

\multicolumn{4}{@{}l}{\textbf{Bold}: most optimal values.}\\
\multicolumn{4}{@{}l}{\underline{Underlined}: significant predictor} \\
\multicolumn{4}{@{}l}{\textit{Italics}: significant model}

A full MWE (minimum working example):

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\newcommand\blurb{\textbf{Bold}: most optimal values. \underline{Underlined}: significant predictor \textit{Italics}: significant model}
\newlength\lengtha
\settowidth{\lengtha}{\blurb}

\begin{document}

\begin{table}[h]
\centering
\begin{tabular}{@{}lccc@{}}
\toprule
\textbf{Parameter} & 
\textit{Null model} & 
\textit{Model 1} & 
\textit{Model 2} \\

\midrule

Intercept          & .652            & -.976                        & -.983                   \\
Round              &   -.145         & .1082                        & .111                    \\
Treatment          &                 & \underline{5.875}            & \underline{5.653}       \\
Neuroticism        &                 &                              & .013                    \\
Neuroticism $\times$ Treatment   &                 &                              & .019                    \\
Round $\times$ Treatment  &                 & \underline{-.955}            & \underline{-.909}       \\

\midrule

LogLikelihood      & -830.51         &  -826.4                      & \textbf{-825.92}        \\
Deviance           & 1661.0          & 1652.8                       & \textbf{1651.8}         \\
df residual        & \textbf{235}    & 233                          & 231                     \\
Akaike IC          & 1671.0          & \textbf{1666.8}              & 1669.8                  \\
Bayesian IC        & \textbf{1688.4} & 1691.2                       & 1701.2                  \\

\midrule

\multicolumn{4}{@{}l}{\textbf{Bold}: most optimal values.}\\
\multicolumn{4}{@{}l}{\underline{Underlined}: significant predictor} \\
\multicolumn{4}{@{}l}{\textit{Italics}: significant model}
\end{tabular}
\end{table}
\end{document} 

Incidentally, what's a "most optimal" value? How does a most optimal value differ from a (plain) optimal value? :-)



Addendum to answer the OP's follow-up question, about how to make the column widths equal while having a long 1-line legend in the final row of the tabular material. First off: don't do this. The result isn't worth it.

But if you insist, here's one way to achieve the questionable objective: Use a tabularx environment and set its overall width to the width of the string

\textbf{Bold}: most optimal values. \underline{Underlined}: significant predictor \textit{Italics}: significant model

See the following screenshot for the result of this effort. Apart from the fact that the table looks dreadful, there's absolutely no guarantee that the width of the 1-line legend, and hence the entire tabularx environment, will fit into the width of the textblock. Sure enough, the tabularx environment in the following screenshot is wider than the textblock.

In short, don't do this.

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\newcommand\blurb{\textbf{Bold}: most optimal values. \underline{Underlined}: significant predictor \textit{Italics}: significant model}
\newlength\lengtha
\settowidth{\lengtha}{\blurb}

\begin{document}
\begin{table}[h]
\begin{tabularx}{\lengtha}{@{}lCCC@{}}
\toprule
\textbf{Parameter} & 
\textit{Null model} & \textit{Model 1} & \textit{Model 2} \\

\midrule

Intercept          & .652            & -.976                        & -.983                   \\
Round              &   -.145         & .1082                        & .111                    \\
Treatment          &                 & \underline{5.875}            & \underline{5.653}       \\
Neuroticism        &                 &                              & .013                    \\
Neur * Treatment   &                 &                              & .019                    \\
Round * Treatment  &                 & \underline{-.955}            & \underline{-.909}       \\

\midrule

LogLikelihood      & -830.51         &  -826.4                      & \textbf{-825.92}        \\
Deviance           & 1661.0          & 1652.8                       & \textbf{1651.8}         \\
df residual        & \textbf{235}    & 233                          & 231                     \\
Akaike IC          & 1671.0          & \textbf{1666.8}              & 1669.8                  \\
Bayesian IC        & \textbf{1688.4} & 1691.2                       & 1701.2                  \\

\midrule

\multicolumn{4}{@{}l@{}}{\blurb}
\end{tabularx}
\end{table}
\end{document} 
Mico
  • 506,678
  • 1
    Thank for your solution, I knew it was something in that direction. I think I forgot the \\ to make the new line actually work.

    I've been told that a most optimal value is a value, in this case, derived from optimization, whereas optimization suggests multiple optimizations of which one optimization is the "most" optimized, or the best tuned parameter or the average (ergo, the best) RMSE when doing cross validation. But yes, there should be only one truly optimal value from a semantic point of view. Good thing statistics is filled with interchangeable jargon ;)

    – fleems Apr 14 '19 at 13:00
  • How would I align the columns in an even way, if I would keep the text as it were? – fleems Apr 14 '19 at 14:32
  • @fleems - I've posted an addendum to answer your follow-up question. The upshot: While it's possible to achieve your objective, it's not advisable to do so. – Mico Apr 14 '19 at 16:34
  • Understood, thank you :) – fleems Apr 14 '19 at 17:41