如何在 LaTeX 中建構帶有標題的下表?

如何在 LaTeX 中建構帶有標題的下表?

我想建立一個如下圖的表格:

通緝

下面的程式碼代表了我迄今為止最好的嘗試(表中的單字實際上沒有任何意義):

\resizebox{9cm}{!} {
\begin{tabular}{l*{2}{c}r} 
Model              & Linear Regression && MLE \\
\hline
fakedata50  & -1.037 & &4   \\
fakedata100 & -1.038 && 3  \\
fakedata500  & -0.7859 && 2 \\
fakedata5000  &-0.7742 && 2  \\
fakedata10000 &-0.7665&&o \\
fakedata100000 &-0.7206&&j \\
fakedata1000000 &-0.6939&&j \\
\end{tabular} }

但是,我想將表格居中,但該命令\centering不起作用。

另外,當我嘗試透過更改 中的值來放大圖表時\resizebox,字體大小也增加了,這是不想要的。有沒有辦法根據我放大表格的程度按比例增加字體大小?

最後,如何為表格排版標題?

答案1

建表時,booktabs包裹是你最好的朋友;在你的具體情況下,我還推薦siunitx包來幫助十進制對齊(當然,除了列對齊之外,該包還可以做更多事情)。

螢幕截圖

S類型採用可選參數 - 這允許您調整每個單獨的列;我曾經table-format=1.4詳細說明過該欄有1編號小數點及其4後面的數字。您會注意到,我必須將列頭括起來,{Linear Regression}以將其從數學模式和小數對齊例程中「轉義」。

這是供您使用的完整程式碼。

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}

\begin{document}
\begin{tabular}{lS[table-format=1.4]r}
    \toprule
    Model           & {Linear Regression} & MLE \\
    \midrule
    fakedata50      & -1.037              & 4   \\
    fakedata100     & -1.038              & 3   \\
    fakedata500     & -0.7859             & 2   \\
    fakedata5000    & -0.7742             & 2   \\
    fakedata10000   & -0.7665             & o   \\
    fakedata100000  & -0.7206             & j   \\
    fakedata1000000 & -0.6939             & j   \\
    \bottomrule
\end{tabular}
\end{document}

如果您想為您的 製作一個「標題」table,那麼您應該使用以下caption命令;您必須將您的tabular環境放入環境中table,或使用套件captionof中的命令caption

帶標題的螢幕截圖

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}

\begin{document}

\begin{table}[!ht]
    \centering
    \caption{Your caption goes here}
    \begin{tabular}{lS[table-format=1.4]r}
        \toprule
        Model           & {Linear Regression} & MLE \\
        \midrule
        fakedata50      & -1.037              & 4   \\
        fakedata100     & -1.038              & 3   \\
        fakedata500     & -0.7859             & 2   \\
        fakedata5000    & -0.7742             & 2   \\
        fakedata10000   & -0.7665             & o   \\
        fakedata100000  & -0.7206             & j   \\
        fakedata1000000 & -0.6939             & j   \\
        \bottomrule
    \end{tabular}
\end{table}
\end{document}

答案2

即使標準tabular環境也為您提供了調整表格大小的工具。最值得注意的是,長度tabcolsep調整每列側面的填充。在我的範例中,我需要縮小它以使表適合標準\textwidth,但在您的情況下,您可以增加它的值。同樣,我將字體大小更改為\small,但不需要這樣做,除非空間是一個問題,或者為了保持整個文件的一致性。

對於垂直空間,我用來\renewcommand\arraystretch{}拉伸表行之間的空間。一旦進入環境,\centering就可以正常工作。在環境之外使用它時,請使用{\centering my table stuff \par}.大括號會將居中範圍限制為僅包含在所擁抱的物體上。並且\par需要激活\centering.

\documentclass{article}
\usepackage{lipsum}
\usepackage{stackengine}
\def\twostack#1#2{\addstackgap{\stackanchor[0pt]{\strut#1}{\strut#2}}}
\begin{document}
\lipsum[1]
\begin{table}[ht]
\centering
\renewcommand\arraystretch{2}
\tabcolsep 3pt
\caption{Chi-square analysis for data set Total Site 1}
\medskip\small
\begin{tabular}{ccccccc}
\hline
\hline
\textbf{Model} & \textbf{Parameters} & \textbf{Estimates} & 
  \textbf{\twostack{Chi-square}{statistic}} & 
  \textbf{P-value} & \textbf{Outcome} & \\
\hline
Geometric Series & $k$ & 0.129202 & 17019.3 & $<$0.001 & Reject\\
\twostack{Broken Stick}{(binned)} & - & - & 1430.72 & $<$0.001 & Reject\\
\twostack{Broken Stick}{(rank abundance)} & - & - & 120085 & $<$0.001 & Reject\\
Logseries & \twostack{$\alpha$}{$x$} & &&&...\\
\hline
\hline
\end{tabular}
\end{table}
\lipsum[2]
\end{document}

在此輸入影像描述

答案3

我嘗試重做OP作為範例給出的表格的開頭,因為它更複雜。對於小數點對齊有意義的列,我使用了booktabscaption和包。請注意,只要在此類列(類型為)中,儲存格包含非數位材料,就必須將其括在括號中。makecellnumprintn{…}{…}

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[showframe, nomarginpar, noheadfoot]{geometry} 
\usepackage{fourier}
\usepackage{heuristica} 
\usepackage{array}
\usepackage{booktabs}
\usepackage{caption} 
\captionsetup{labelfont = sc, labelsep = period, justification = centering }
\usepackage{makecell}

\renewcommand\theadfont{\bfseries}
\usepackage[autolanguage, np]{numprint}
\usepackage{stackengine} 
\usepackage[english]{babel} 

\begin{document}

\begin{table}[!h]
\centering
\caption{Chi-square analysis for data set\enspace  Total Site 1}
\begin{tabular}{c >{$}c<{$} n{2}{7}c >{$}n{1}{5}<{$} c}
\toprule\midrule
\thead{Model}  & \thead{\textbf{Parameters}} & {\thead{Estimates}} & \thead{Chi-square \\ statistics}  & \thead{\textbf{P-value}} & \thead{Outcome} \\
\midrule
\addlinespace
Geometric Series & k & 0.129202 & 17019.3 &  < 0.001 & Reject \\
\makecell*{Broken Stick \\ (binned)} & - & {\makecell{--}} & 1430.72 &  < 0.001 &     Reject \\
\makecell{Broken Stick \\ (rank abundance)} & - &  {\makecell{--}} &  120085 &  < 0.001 & Reject \\
\multirowcell{2}{Logseries}  & α &7.287017 & \multirowcell{2}{20.44702} & {\multirowcell{2}{0.11667}} & Cannot \\%
   & x  & 0.9999327 &  &  & reject \\[3pt]
\multirowcell{2}{Lognormal} & M & 6.67146 & \multirowcell{2}{30.8349} &  {\multirowcell{2}{< 0.001}} & \multirowcell{2}{Reject} \\
 & V & 16.0206 \\
 \midrule\bottomrule
\end{tabular}
\end{table}

\end{document} 

在此輸入影像描述

相關內容