4

I am building a table in latex but it is too wide to fit the page. Centering will only align it on the left side so that it will spill out on the right side. I already tried to rotate it and to set text width but it did not work. Every tip would be very much appreciated!

This is my code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\begin{document}
\begin{table}[htbp]
  \caption{Add caption}
       \begin{tabular}{lSSSSSSSS}
     \multicolumn{1}{l}{predicted Variable}&\multicolumn{2}{c}{step1\_coefs} & \multicolumn{2}{c}{step2\_coefs} & \multicolumn{2}{c}{step3\_coefs} & \multicolumn{2}{c}{step4\_coefs} \\
    \toprule
          & \multicolumn{1}{l}{\textbf{est}} & \multicolumn{1}{l}{\textbf{se}} & \multicolumn{1}{l}{\textbf{est}} & \multicolumn{1}{l}{\textbf{se}} & \multicolumn{1}{l}{\textbf{est}} & \multicolumn{1}{l}{\textbf{se}} & \multicolumn{1}{l}{\textbf{est}} & \multicolumn{1}{l}{\textbf{se}} \\
    \midrule
    \textbf{w} & 0.145 & 0.082 & 0.205* & 0.082 & 0.225** & 0.081 & 0.161 & 0.085 \\
    \textbf{w} & -0.138*** & 0.041 & -0.103* & 0.041 & -0.116** & 0.041 & -0.134** & 0.044 \\
    \textbf{w} & -1.146*** & 0.042 & -1.135*** & 0.042 & -1.09*** & 0.042 & -1.055*** & 0.042 \\
    \textbf{w} & 0.159 & 0.103 & 0.154 & 0.103 & 0.181 & 0.101 & 0.262* & 0.103 \\
    \textbf{w} & 0.023 & 0.107 & 0.019 & 0.107 & 0.086 & 0.106 & 0.235* & 0.112 \\
    \textbf{w} & -0.007 & 0.005 & -0.005 & 0.005 & -0.008 & 0.005 & -0.009 & 0.005 \\
    \textbf{w} & -0.335*** & 0.041 & -0.336*** & 0.041 & -0.331*** & 0.04  & -0.323*** & 0.04 \\
    \textbf{w} &       &       & -0.108** & 0.038 & -0.093* & 0.038 & -0.025 & 0.041 \\
    \textbf{w} &       &       & -0.159 & 0.151 & 0.03  & 0.152 & 0.035 & 0.152 \\
     \textbf{w} &       &       & 0.555*** & 0.096 & 0.515*** & 0.096 & 0.549*** & 0.096 \\
    \textbf{w} &       &       & 0.081 & 0.095 & 0.064 & 0.094 & 0.039 & 0.094 \\
     \textbf{w} &       &       &       &       & 0.397* & 0.187 & 0.324 & 0.188 \\
    \textbf{w of w} &       &       &       &       & 1.357*** & 0.211 & 1.335*** & 0.212 \\
 \textbf{w w} &       &       &       &       & 0.576*** & 0.086 & 0.474*** & 0.089 \\
 \textbf{w w} &       &       &       &       & 0.477** & 0.175 & 0.46**  & 0.175 \\
 \textbf{w} &       &       &       &       & 0.7***   & 0.134 & 0.645*** & 0.135 \\
\textbf{w} &       &       &       &       &       &       & -0.382*** & 0.101 \\
 \textbf{w} &       &       &       &       &       &       & -0.385*** & 0.106 \\
\textbf{w} &       &       &       &       &       &       & 0.307 & 0.177 \\
\textbf{w} &       &       &       &       &       &       & -0.254 & 0.132 \\
\textbf{w} &       &       &       &       &       &       & 0.041 & 0.156 \\
\textbf{w} &       &       &       &       &       &       & -0.041 & 0.043 \\
\textbf{w} &       &       &       &       &       &       & -0.045 & 0.043 \\
\textbf{w} &       &       &       &       &       &       & -0.088* & 0.044 \\
\textbf{w} &       &       &       &       &       &       & -0.047 & 0.044 \\
\textbf{w} &       &       &       &       &       &       & -0.079 & 0.049 \\
\textbf{w} &       &       &       &       &       &       & 0.102 & 0.089 \\
    \midrule
    \textbf{$R^2$} & 0.186* &   & 0.196* &  & 0.214* &  & 0.222* &  &\\
     \textbf{$\Delta R^2$} &    &   & 0.01 &    & 0.018 &   & 0.008 &   &\\
\bottomrule
    \end{tabular}%
\end{table}%
\end{document}
  • 1
    Please tell us which paper size you employ (A4? US Letter? Something else?) and how wide the horizontal margins are in your document. – Mico May 26 '18 at 10:04
  • how you define column S, is this from siunitx? it is not loaded in your preamble ... – Zarko May 26 '18 at 10:05
  • @Mico Of course, I am using A4 an have not set the horizontal margins. – Julia May 26 '18 at 10:14
  • @Zarko Yes, I am using it from siuntx, thanks for reminding me, but somehow it worked anyway. – Julia May 26 '18 at 10:15
  • 1
    @Julia, i realy doubt that it worked ... and since you not define options for this columns, you have uneccessary space between columns, also you have errors in column numbers (last two rows), etc. problem also are to wide the first column header – Zarko May 26 '18 at 10:26
2

After fixing some obvious typos and mistakes (e.g., surplus & symbols in the final two rows) in your code, replacing the first \midrule statement with four separate \cmidrule statements, and assuming horizontal margins of 2.5cm, I suggest you employ a tabularx environment (with a modified X column type for the first column, to allow line breaks as needed) and the dcolumn package to permit alignment on the decimal markers. (The siunitx package and its S column type can perform this form of alignment as well; however, if all you need to achieve is decimal marker alignment, loading the siunitx package may be a bit of overkill.)

Incidentally, the directives \textbf{$R^2$} and \textbf{$\Delta R^2$} do not embolden their arguments. Use either \mathbf (in math mode, obviously) or, better yet, don't render them in bold. Removing the \textbf wrappers is what I've done in the code below.

enter image description here

\documentclass[a4paper]{article}      % 'a4paper' per the OP's comment
\usepackage[hmargin=2.5cm]{geometry} % choose the appropriate margins
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{dcolumn,tabularx,ragged2e} % <-- new
\newcolumntype{d}[1]{D..{#1}}
\newcolumntype{L}{>{\RaggedRight\arraybackslash\hspace{0pt}}X}
\newcommand\sym[1]{^{#1}}

\begin{document}
\begin{table}[htbp]
\caption{Add caption} \label{add_label}
\begin{tabularx}{\textwidth}{@{} L *{4}{d{2.5}d{1.3}} @{}}
\toprule
Predicted Variable 
& \multicolumn{2}{c}{step1\_coefs} & \multicolumn{2}{c}{step2\_coefs} 
& \multicolumn{2}{c}{step3\_coefs} & \multicolumn{2}{c@{}}{step4\_coefs} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} \cmidrule(l){8-9} 
& \multicolumn{1}{c}{est} & \multicolumn{1}{c}{se} 
& \multicolumn{1}{c}{est} & \multicolumn{1}{c}{se} 
& \multicolumn{1}{c}{est} & \multicolumn{1}{c}{se} 
& \multicolumn{1}{c}{est} & \multicolumn{1}{c@{}}{se} \\
\midrule
\textbf{w} & 0.145 & 0.082 & 0.205\sym{*}& 0.082 & 0.225\sym{**}& 0.081 & 0.161 & 0.085 \\
\textbf{w} & -0.138\sym{***}& 0.041 & -0.103\sym{*}& 0.041 & -0.116\sym{**}& 0.041 & -0.134\sym{**}& 0.044 \\
\textbf{w} & -1.146\sym{***}& 0.042 & -1.135\sym{***}& 0.042 & -1.09\sym{***}& 0.042 & -1.055\sym{***}& 0.042 \\
\textbf{w} & 0.159 & 0.103 & 0.154 & 0.103 & 0.181 & 0.101 & 0.262\sym{*}& 0.103 \\
\textbf{w} & 0.023 & 0.107 & 0.019 & 0.107 & 0.086 & 0.106 & 0.235\sym{*}& 0.112 \\
\textbf{w} & -0.007 & 0.005 & -0.005 & 0.005 & -0.008 & 0.005 & -0.009 & 0.005 \\
\textbf{w} & -0.335\sym{***}& 0.041 & -0.336\sym{***}& 0.041 & -0.331\sym{***}& 0.04  & -0.323\sym{***}& 0.04 \\ 
\addlinespace
\textbf{w} &       &       & -0.108\sym{**}& 0.038 & -0.093\sym{*}& 0.038 & -0.025 & 0.041 \\
\textbf{w} &       &       & -0.159 & 0.151 & 0.03  & 0.152 & 0.035 & 0.152 \\
\textbf{w} &       &       & 0.555\sym{***}& 0.096 & 0.515\sym{***}& 0.096 & 0.549\sym{***}& 0.096 \\
\textbf{w} &       &       & 0.081 & 0.095 & 0.064 & 0.094 & 0.039 & 0.094 \\
\addlinespace
\textbf{w} &       &       &       &       & 0.397\sym{*}& 0.187 & 0.324 & 0.188 \\
\textbf{w of w} &  &       &       &       & 1.357\sym{***}& 0.211 & 1.335\sym{***}& 0.212 \\
\textbf{w w} &     &       &       &       & 0.576\sym{***}& 0.086 & 0.474\sym{***}& 0.089 \\
\textbf{w w} &     &       &       &       & 0.477\sym{**}& 0.175 & 0.46\sym{**} & 0.175 \\
\textbf{w} &       &       &       &       & 0.7\sym{***}  & 0.134 & 0.645\sym{***}& 0.135 \\
\addlinespace
\textbf{w} &       &       &       &       &       &       & -0.382\sym{***}& 0.101 \\
\textbf{w} &       &       &       &       &       &       & -0.385\sym{***}& 0.106 \\
\textbf{w} &       &       &       &       &       &       &  0.307 & 0.177 \\
\textbf{w} &       &       &       &       &       &       & -0.254 & 0.132 \\ 
\textbf{w} &       &       &       &       &       &       &  0.041 & 0.156 \\
\textbf{w} &       &       &       &       &       &       & -0.041 & 0.043 \\
\textbf{w} &       &       &       &       &       &       & -0.045 & 0.043 \\
\textbf{w} &       &       &       &       &       &       & -0.088\sym{*}& 0.044 \\
\textbf{w} &       &       &       &       &       &       & -0.047 & 0.044 \\
\textbf{w} &       &       &       &       &       &       & -0.079 & 0.049 \\
\textbf{w} &       &       &       &       &       &       &  0.102 & 0.089 \\
\midrule
$R^2$        & 0.186\sym{*}& & 0.196\sym{*}& & 0.214\sym{*}& & 0.222\sym{*}  \\
$\Delta R^2$ &             & & 0.01        & & 0.018       & & 0.008    \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
5

with use of the package siunitx, defining page layout by the package geometry and left to tabular* to define space between columns:

edit: in mwe is considered Mico suggestion how to improve column spacing.

enter image description here

(red lines indicate page layout)

\documentclass[a4paper]{article}
\usepackage{geometry}    
\usepackage[utf8]{inputenc}
\usepackage{booktabs, multirow}
\usepackage{siunitx}

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
\begin{table}[htbp]
\setlength\tabcolsep{0pt}
\caption{Add caption}
\label{tab:very impartand table}
    \begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}
                    >{\bfseries}l
                           *{4}{S[table-space-text-post=***,
                                  table-align-text-post=false,
                                  table-format=-1.3]           % <---
                                S[table-format=1.3]}           % <---
                                }
    \toprule
\normalfont\multirow{2}{11ex}[-1ex]{predicted\\ Variable}
    &   \multicolumn{2}{c}{step1\_coefs}
        &   \multicolumn{2}{c}{step2\_coefs}
            &   \multicolumn{2}{c}{step3\_coefs}
                &   \multicolumn{2}{c}{step4\_coefs} \\
    \cmidrule(lr){2-3}
    \cmidrule(lr){4-5}
    \cmidrule(lr){6-7}
    \cmidrule(l){8-9}
    & {\textbf{est}} & {\textbf{se}} & {\textbf{est}} & {\textbf{se}}
    & {\textbf{est}} & {\textbf{se}} & {\textbf{est}} & {\textbf{se}} \\
    \midrule
w       &  0.145    & 0.082 &  0.205*   & 0.082 &  0.225**  & 0.081 & 0.161     & 0.085 \\
w       & -0.138*** & 0.041 & -0.103*   & 0.041 & -0.116**  & 0.041 & -0.134**  & 0.044 \\
w       & -1.146*** & 0.042 & -1.135*** & 0.042 & -1.09***  & 0.042 & -1.055*** & 0.042 \\
w       &  0.159    & 0.103 &  0.154    & 0.103 &  0.181    & 0.101 & 0.262*    & 0.103 \\
w       &  0.023    & 0.107 &  0.019    & 0.107 &  0.086    & 0.106 & 0.235*    & 0.112 \\
w       & -0.007    & 0.005 & -0.005    & 0.005 & -0.008    & 0.005 & -0.009    & 0.005 \\
w       & -0.335*** & 0.041 & -0.336*** & 0.041 & -0.331*** & 0.04  & -0.323*** & 0.04  \\
w       &           &       & -0.108**  & 0.038 & -0.093*   & 0.038 & -0.025    & 0.041 \\
w       &           &       & -0.159    & 0.151 &  0.03     & 0.152 & 0.035     & 0.152 \\
w       &           &       &  0.555*** & 0.096 &  0.515*** & 0.096 & 0.549***  & 0.096 \\
w       &           &       &  0.081    & 0.095 &  0.064    & 0.094 & 0.039     & 0.094 \\
w       &           &       &           &       &  0.397*   & 0.187 & 0.324     & 0.188 \\
w of w  &           &       &           &       &  1.357*** & 0.211 & 1.335***  & 0.212 \\
w w     &           &       &           &       &  0.576*** & 0.086 & 0.474***  & 0.089 \\
w w     &           &       &           &       &  0.477**  & 0.175 & 0.46**    & 0.175 \\
w       &           &       &           &       &  0.7***   & 0.134 & 0.645***  & 0.135 \\
w       &           &       &           &       &           &       & -0.382*** & 0.101 \\
w       &           &       &           &       &           &       & -0.385*** & 0.106 \\
w       &           &       &           &       &           &       & 0.307     & 0.177 \\
w       &           &       &           &       &           &       & -0.254    & 0.132 \\
w       &           &       &           &       &           &       & 0.041     & 0.156 \\
w       &           &       &           &       &           &       & -0.041    & 0.043 \\
w       &           &       &           &       &           &       & -0.045    & 0.043 \\
w       &           &       &           &       &           &       & -0.088*   & 0.044 \\
w       &           &       &           &       &           &       & -0.047    & 0.044 \\
w       &           &       &           &       &           &       & -0.079    & 0.049 \\
w       &           &       &           &       &           &       & 0.102     & 0.089 \\
    \midrule
$R^2$   & 0.186*    &       &  0.196*   &       &  0.214*   &       & 0.222*    &       \\
$\Delta R^2$    &   &       &  0.01     &       &  0.018    &       & 0.008     &       \\
\bottomrule
    \end{tabular*}
\end{table}
\end{document}
  • +1. For tighter spacing of the se columns, consider changing the second S column from just S to S[table-format=1.3]. – Mico May 26 '18 at 11:47
  • @Mico, you are (as usual) right. considered in edited answer. thank you very much! – Zarko May 26 '18 at 14:54
3

Another variant, with a slightly simpler code and a plain tabular, with the help ofmakecell and caption for a better vertical spacing between above caption and table:

\documentclass{article}
\usepackage[showframe]{geometry} 

\usepackage[utf8]{inputenc}
\usepackage{booktabs, makecell, caption}
\usepackage{siunitx} 

\begin{document}

\begin{table}[htbp]
\sisetup{table-format=1.3, table-number-alignment = center}
\renewcommand{\theadfont}{\normalsize\bfseries}
\renewcommand{\theadalign}{lc}
\setlength{\extrarowheight}{2pt}
\centering
  \caption{Add caption}
       \begin{tabular}{>{\bfseries\boldmath}l*{4}{S[table-space-text-post=***, table-space-text-pre = $-$]S}} %
     \makecell[l]{predicted\\ Variable}&\multicolumn{2}{c}{step1\_coefs} & \multicolumn{2}{c}{step2\_coefs} & \multicolumn{2}{c}{step3\_coefs} & \multicolumn{2}{c}{step4\_coefs} \\
    \toprule
          &{\thead{est}} & {\thead{se}} & {\thead{est}} & {\thead{se}} & {\thead{est}} & {\thead{se}} & {\thead{est}} & {\thead{se}} \\
    \midrule
    w & 0.145 & 0.082 & 0.205* & 0.082 & 0.225** & 0.081 & 0.161 & 0.085 \\
    w & -0.138*** & 0.041 & -0.103* & 0.041 & -0.116** & 0.041 & -0.134** & 0.044 \\
    w & -1.146*** & 0.042 & -1.135*** & 0.042 & -1.09*** & 0.042 & -1.055*** & 0.042 \\
    w & 0.159 & 0.103 & 0.154 & 0.103 & 0.181 & 0.101 & 0.262* & 0.103 \\
    w & 0.023 & 0.107 & 0.019 & 0.107 & 0.086 & 0.106 & 0.235* & 0.112 \\
    w & -0.007 & 0.005 & -0.005 & 0.005 & -0.008 & 0.005 & -0.009 & 0.005 \\
    w & -0.335*** & 0.041 & -0.336*** & 0.041 & -0.331*** & 0.04 & -0.323*** & 0.04 \\
    w & & & -0.108** & 0.038 & -0.093* & 0.038 & -0.025 & 0.041 \\
    w & & & -0.159 & 0.151 & 0.03 & 0.152 & 0.035 & 0.152 \\
    w & & & 0.555*** & 0.096 & 0.515*** & 0.096 & 0.549*** & 0.096 \\
    w & & & 0.081 & 0.095 & 0.064 & 0.094 & 0.039 & 0.094 \\
    w & & & & & 0.397* & 0.187 & 0.324 & 0.188 \\
    w of w & & & & & 1.357*** & 0.211 & 1.335*** & 0.212 \\
    w w & & & & & 0.576*** & 0.086 & 0.474*** & 0.089 \\
    w w & & & & & 0.477** & 0.175 & 0.46** & 0.175 \\
    w & & & & & 0.7*** & 0.134 & 0.645*** & 0.135 \\
    w & & & & & & & -0.382*** & 0.101 \\
    w & & & & & & & -0.385*** & 0.106 \\
    w & & & & & & & 0.307 & 0.177 \\
    w & & & & & & & -0.254 & 0.132 \\
    w & & & & & & & 0.041 & 0.156 \\
    w & & & & & & & -0.041 & 0.043 \\
    w & & & & & & & -0.045 & 0.043 \\
    w & & & & & & & -0.088* & 0.044 \\
    w & & & & & & & -0.047 & 0.044 \\
    w & & & & & & & -0.079 & 0.049 \\
    w & & & & & & & 0.102 & 0.089 \\
        \midrule
     $R^2$ & 0.186* & & 0.196* & & 0.214* & & 0.222* & \\
      $\Delta R^2$ & & & 0.01 & & 0.018 & & 0.008 & \\
    \bottomrule
    \end{tabular}%
\end{table}%

\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.