0

I know that there are already similar questions to this on stack exchange, but I am working in R studio with the Stargazer library and I want to be able to directly copy and paste my tables into overleaf and have them be centered (the problem is that they are often too big and fall off the edge of the page). Is there some easy way to center these tables without having to recode the tables in any way? A lot of other responses I have seen are hard for me to follow because I am trying not to have to recode the tables. The whole perk of using Stargazer is that it makes the tables for you... :/

Below is an example table (obviously fake results):

\begin{table}[!htbp] \centering 
  \caption{} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}}lccccccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
Statistic & \multicolumn{1}{c}{N} & \multicolumn{1}{c}{Mean} & \multicolumn{1}{c}{St. Dev.} & \multicolumn{1}{c}{Min} & \multicolumn{1}{c}{Pctl(25)} & \multicolumn{1}{c}{Pctl(75)} & \multicolumn{1}{c}{Max} \\ 
\hline \\[-1.8ex] 
Marriage & 10000 & 100000 & 10000 & 10000 & 1000 & 10000 & 1000000 \\ 
Real Income & 10000 & 100000 & 10000 & 10000 & 1000 & 10000 & 1000000 \\
Unemployment & 10000 & 100000 & 10000 & 10000 & 1000 & 10000 & 1000000 \\
\hline \\[-1.8ex] 
\end{tabular} 
\end{table} 

EDIT: I am not sure why the table is not showing up correctly here, but it will in overleaf.

enter image description here

Roland
  • 6,655
  • 5
  • 1
    How representative is the sample table you posted? E.g., is its width close to the largest width of actual tables? The reason I ask is that there is only one general solution to your question, viz., to use \resizebox (or its close relative, adjustbox) to inflict shrinkage on the tabular material in order to limit the width. However, that's also the absolutely worst solution from a typographic point of view. Indeed, nothing screams as loudly and unmistakably "hacker solution that doesn't care a fig about legibility or aesthetics" as the the \resizebox approach does. – Mico Apr 21 '21 at 04:52

1 Answers1

1

Here a suggestion with tabularx:

\documentclass{article}
\usepackage{tabularx}
\usepackage{hhline}
\newcolumntype{A}{>{\centering\arraybackslash}X}
\renewcommand\tabularxcolumn[1]{m{#1}}%
\usepackage{caption} 
\captionsetup[table]{skip=5pt}
\usepackage[singlelinecheck=false]{caption}

\begin{document}

\begin{table}[]
    \renewcommand{\arraystretch}{1.5} 
    \centering
    \caption{Some Table description}
    \label{tab:my-table}
    \begin{tabularx}{\textwidth}{lAAAAAAA}
        \hhline{========}
        Statistic    & N     & Mean  & SD & Min   & Pctl 25 & Pctl 75 & Max   \\ \hline
        Marriage     & 10000 & 10000 & 10000    & 10000 & 10000   & 10000   & 100000 \\
        Real Income  & 10000 & 10000 & 10000    & 10000 & 10000   & 10000   & 100000 \\
        Unemployment & 10000 & 10000 & 10000    & 10000 & 10000   & 10000   & 100000 \\ \hline
    \end{tabularx}
\end{table}

\end{document}

enter image description here

A suggestion with resizebox:

Note: this changes your font size!.

\documentclass{article}
\usepackage{graphicx}

\begin{document}

\begin{table}[!htbp] \centering \caption{} \label{} \resizebox{\textwidth}{!}{
\begin{tabular}{@{\extracolsep{5pt}}lccccccc} \[-1.8ex]\hline \hline \[-1.8ex] Statistic & \multicolumn{1}{c}{N} & \multicolumn{1}{c}{Mean} & \multicolumn{1}{c}{St. Dev.} & \multicolumn{1}{c}{Min} & \multicolumn{1}{c}{Pctl(25)} & \multicolumn{1}{c}{Pctl(75)} & \multicolumn{1}{c}{Max} \ \hline \[-1.8ex] Marriage & 10000 & 100000 & 10000 & 10000 & 1000 & 10000 & 1000000 \ Real Income & 10000 & 100000 & 10000 & 10000 & 1000 & 10000 & 1000000 \ Unemployment & 10000 & 100000 & 10000 & 10000 & 1000 & 10000 & 1000000 \ \hline \[-1.8ex] \end{tabular} } \end{table}

\end{document}

enter image description here

Roland
  • 6,655
  • This seems as though I will have to recode the table, which is what I am trying to avoid... – Sparkles the unicorn Apr 21 '21 at 03:49
  • 1
    The only way without re-coding would be to just scale the table down to \textwidth for example with \resizebox of the graphicx package. But this will change your font size and should (in my oppinion) be avoided. I edited my question and added an example with resizebox. – Roland Apr 21 '21 at 04:01
  • That's actually wonderful! Thank you so much! – Sparkles the unicorn Apr 21 '21 at 04:24