移動表格以適合頁面

移動表格以適合頁面

我知道有幾個類似的線程,但我無法真正找到答案......我有一個表格,我根本無法放在我的頁面上。我是 Latex 的初學者,幾乎再次放棄......這是我的標題:

\documentclass[a4paper, fontsize=12pt]{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage[ngerman, english]{babel}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\graphicspath{{my_images/}}
\usepackage[nottoc]{tocbibind}
\usepackage[super]{nth}
\usepackage{wrapfig}

\def\code#1{\texttt{#1}}

\usepackage[backend=biber,natbib,style=apa]{biblatex}
\addbibresource{references.bib}

 
\usepackage{listings}

\begin{document}
\maketitle

\thispagestyle{empty}
\tableofcontents


\listoffigures
\let\LaTeXStandardClearpage\clearpage
\let\clearpage\relax  % Do nothing when a \clearpage command appears 
\listoftables
\let\clearpage\LaTeXStandardClearpage % Return to the old definition


\input{01_introduction.tex}
\let\LaTeXStandardClearpage\clearpage
\let\clearpage\relax  % Do nothing when a \clearpage command appears 
\pagenumbering{arabic}
\setcounter{page}{4}
\input{02_data_methods.tex}
\let\clearpage\LaTeXStandardClearpage % Return to the old definition
\input{03_results.tex}
\input{04_discussion.tex}
\input{05_conclusion.tex}
\printbibliography[heading=bibintoc]

\end{document}

在第二個輸入中我有這個表:

\begin{table}[!htbp] 
  \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] 
sqft\_living & 2,000 & 2,156.307 & 958.696 & 440 & 1,460 & 2,640 & 9,640 \\ 
view & 2,000 & 0.373 & 1.026 & 0 & 0 & 0 & 4 \\ 
condition & 2,000 & 3.417 & 0.654 & 1 & 3 & 4 & 5 \\ 
grade & 2,000 & 7.748 & 1.200 & 4 & 7 & 8 & 13 \\ 
yr\_built & 1,800 & 1,973.880 & 28.719 & 1,900.000 & 1,954.000 & 2,000.000 & 2,014.000 \\ 
yr\_renovated & 2,000 & 118.719 & 472.126 & 0 & 0 & 0 & 2,015 \\ 
sqft\_living15 & 2,000 & 2,033.584 & 705.835 & 460 & 1,500 & 2,450 & 5,030 \\ 
price\_per\_sqft & 2,000 & 269.090 & 118.534 & 87.588 & 184.960 & 320.706 & 800.000 \\ 
lot\_to\_living & 2,000 & 7.154 & 15.580 & 0.544 & 2.403 & 6.488 & 281.229 \\ 
basement\_percent & 2,000 & 0.127 & 0.172 & 0 & 0 & 0.3 & 1 \\ 
\hline \\[-1.8ex] 
\end{tabular} 
\end{table} 

產生以下輸出:

在此輸入影像描述

我能做些什麼來將其向左移動一點嗎?

答案1

您無需將桌子移到任何地方。請嘗試以下操作:

\documentclass[a4paper, fontsize=12pt]{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman, english]{babel}
%---------------- 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]
\caption{My huge table}
\label{tab:???}
    \setlength\tabcolsep{0pt}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lccccccc @{}}
    \hline \hline
Statistic   & {N} & {Mean}  & {St. Dev.} & {Min} 
            & {Pctl(25)}    & {Pctl(75)} & {Max} \\
    \hline
sqft\_living & 2,000 & 2,156.307 & 958.696 & 440 & 1,460 & 2,640 & 9,640 \\
view & 2,000 & 0.373 & 1.026 & 0 & 0 & 0 & 4 \\
condition & 2,000 & 3.417 & 0.654 & 1 & 3 & 4 & 5 \\
grade & 2,000 & 7.748 & 1.200 & 4 & 7 & 8 & 13 \\
yr\_built & 1,800 & 1,973.880 & 28.719 & 1,900.000 & 1,954.000 & 2,000.000 & 2,014.000 \\
yr\_renovated & 2,000 & 118.719 & 472.126 & 0 & 0 & 0 & 2,015 \\
sqft\_living15 & 2,000 & 2,033.584 & 705.835 & 460 & 1,500 & 2,450 & 5,030 \\
price\_per\_sqft & 2,000 & 269.090 & 118.534 & 87.588 & 184.960 & 320.706 & 800.000 \\
lot\_to\_living & 2,000 & 7.154 & 15.580 & 0.544 & 2.403 & 6.488 & 281.229 \\
basement\_percent & 2,000 & 0.127 & 0.172 & 0 & 0 & 0.3 & 1 \\
\hline 
\end{tabular*}
    \end{table}
\end{document}

在此輸入影像描述

(紅線顯示頁面佈局)

上面的 MWE 是:

  • 對於表格使用tabular*環境,選擇寬度\textwidth
  • \tabcolsep預設為零
  • 的實際大小\tabcolsep由指令計算@{\extracolsep{\fill}}

答案2

我建議您擺脫那些\multicolumn{1}{c}使標題單元格內容窒息的繭,擺脫@{\extracolsep{5pt}}列間距指令,並擺脫最小、最大、誇脫1中日曆年數字的坦率地看起來荒謬的十進制數字和 Quart3 列。透過這些調整,桌子就很適合了。

\documentclass[a4paper, fontsize=12pt]{scrreprt}
\usepackage[ngerman, english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs,array}

\begin{document}
\setcounter{chapter}{3} % just for this example
\begin{table}[!htbp]
\caption{xxx} \label{tab:xxx}
\centering
\begin{tabular}{@{} >{\ttfamily}l *{7}{c} @{}}
\toprule
\multicolumn{1}{@{}l}{Statistic} & $N$ & Mean & St.\ Dev. & Min & Pctl(25) & Pctl(75) & Max \\
\midrule
sqft\_living & 2,000 & 2,156.307 & 958.696 & 440 & 1,460 & 2,640 & 9,640 \\
view & 2,000 & 0.373 & 1.026 & 0 & 0 & 0 & 4 \\
condition & 2,000 & 3.417 & 0.654 & 1 & 3 & 4 & 5 \\
grade & 2,000 & 7.748 & 1.200 & 4 & 7 & 8 & 13 \\
yr\_built & 1,800 & 1,973.88 & 28.72 & 1900 & 1954 & 2000 & 2014 \\
yr\_renovated & 2,000 & 118.719 & 472.126 & 0 & 0 & 0 & 2015 \\ % why all the zeroes in this row?
sqft\_living15 & 2,000 & 2,033.584 & 705.835 & 460 & 1,500 & 2,450 & 5,030 \\
price\_per\_sqft & 2,000 & 269.09 & 118.53 & 87.59 & 184.96 & 320.71 & 800.00 \\
lot\_to\_living & 2,000 & 7.154 & 15.580 & 0.544 & 2.403 & 6.488 & 281.229 \\
basement\_percent & 2,000 & 0.127 & 0.172 & 0 & 0 & 0.3 & 1 \\
\bottomrule
\end{tabular}
\end{table}

\end{document}

相關內容