如何在 LaTeX 中将表格堆叠在一起?

如何在 LaTeX 中将表格堆叠在一起?

我已经用 LaTeX 为三个表格编写了以下代码。但它们在页面上都变形了。我希望它们在页面上居中并堆叠在一起。有什么解决方案吗?

    \documentclass[a4paper, 12 pt]{article}
    \usepackage{listings}
    \usepackage{booktabs}
    \begin{document}
    \vspace{\baselineskip}
    \begin{tabular}{@{} *5l @{}}    \toprule
    \centering
    \emph{\textbf{mstatus}} & \textbf{Marital Status}   \\\midrule
    & \emph{Value}    & \emph{Label} \\ 
    & 1 & Single\\ 
    & 2 & Married\\ 
    & 3 & Common-law marriage\\ 
    & 4 & Divorced\\ 
    & 5 & Seperated\\ 
    & 6 & Widowed\\\bottomrule
     \hline
    \end{tabular}
    \vspace{\baselineskip}
    \begin{tabular}{@{} *5l @{}}    \toprule
    \emph{\textbf{edlevel}} & \textbf{Education Level}   \\\midrule
    & \emph{Value}    & \emph{Label} \\ 
    & 1 & None/school\\ 
    & 2 & Middle school/high school\\ 
    & 3 & University/post-secondary\\\bottomrule
     \hline
    \end{tabular}
    \vspace{\baselineskip}
    \begin{tabular}{@{} *5l @{}}    \toprule
    \emph{\textbf{edlevel}} & \textbf{Urban/Rural Cluster }   \\\midrule
    & \emph{Value}    & \emph{Label} \\ 
    & 0 & Urban\\ 
    & 1 & Rurall\\ \bottomrule
     \hline
    \end{tabular}
    \vspace{\baselineskip}
    \end{document}

编辑:我尝试了第二种解决方案,但出现了错误。

    \begin{table}[p]
    \centering
\begin{tabularx}{0.6\linewidth}{>{\centering}p{4em}  X}    
    \toprule
\emph{\textbf{mstatus}} & \textbf{Marital Status}   \\
    \midrule
\emph{Value}            & \emph{Label}              \\
    \addlinespace
    1                   & Single                    \\
    2                   & Married                   \\
    3                   & Common-law marriage       \\
    4                   & Divorced                  \\
    5                   & Separated                 \\
    6                   & Widowed                   \\
    \bottomrule
\end{tabularx}

\vspace{\baselineskip}
\begin{tabularx}{0.6\linewidth}{>{\centering}p{4em}  X}
    \toprule
\emph{\textbf{edlevel}} & \textbf{Education Level}  \\
    \midrule
\emph{Value}            & \emph{Label}              \\
    \addlinespace
    1                   & None/school               \\
    2                   & Middle school/high school \\
    3                   & University/post-secondary \\
    \bottomrule
 \hline
\end{tabularx}

\vspace{\baselineskip}
\begin{tabularx}{0.6\linewidth}{>{\centering}p{4em}  X}
    \toprule
\textbf{\emph{ur}} & \textbf{Urban/Rural Cluster}   \\
    \midrule
\emph{Value}            & \emph{Label}              \\
    \addlinespace
    0                   & Urban                     \\
    1                   & Rurall                    \\ 
    \bottomrule
\end{tabularx}

\vspace{\baselineskip}
\begin{tabularx}{0.6\linewidth}{>{\centering}p{4em}  X}
    \toprule
\textbf{\emph{age}} & \textbf{Age Group}   \\
    \midrule
\emph{Value}            & \emph{Label}              \\
    \addlinespace
    1 & 15---29\\ 
    2 & 30---44\\ 
    3 & 45---59\\ 
    4 & 60+\\
    \bottomrule
\end{tabularx}

\vspace{\baselineskip}
\begin{tabularx}{0.6\linewidth}{>{\centering}p{4em}  X}
    \toprule
\textbf{\emph{age}} & \textbf{Age Group}   \\
    \midrule
\emph{Value}            & \emph{Label}              \\
    \addlinespace
1 & Amerindian\\ 
2 & Afro-Surinamese\\ 
3 & Mixed\\ 
4 & Hindustani ("East Indians”)\\ 
5 & Javanese\\ 
6 & Maroons\\
7 & Other\\\bottomrule
    \bottomrule
\end{tabularx}

\vspace{\baselineskip}
\begin{tabularx}{0.6\linewidth}{>{\centering}p{4em}  X}
    \toprule
\textbf{\emph{age}} & \textbf{Age Group}   \\
    \midrule
\emph{Value}            & \emph{Label}              \\
    \addlinespace
1 & A salaried employee of the government\\
&  or an independent state-owned enterprise\\ 
2 & A salaried employee in the private sector\\ 
3 & Owner or partner in a business/Self-employed\\ 
4 & Unpaid worker
    \bottomrule
\end{tabularx}

\vspace{\baselineskip}
\begin{tabularx}{0.6\linewidth}{>{\centering}p{4em}  X}
    \toprule
\textbf{\emph{age}} & \textbf{Age Group}   \\
    \midrule
\emph{Value}            & \emph{Label}              \\
    \addlinespace
0 & 0\\ 
1 & SRD 0 – SRD 200\\ 
2 & SRD 200 – SRD 300\\ 
3 & SRD 301 – SRD 400\\ 
4 & SRD 401 – SRD 600\\ 
5 & SRD 601 – SRD 800\\
6 & SRD 801 – SRD 900\\ 
7 & SRD 901 – SRD 1000\\ 
8 & SRD 1001 – SRD 1200\\ 
9 & SRD 1201 – SRD 1500\\ 
10 & SRD 1501 – SRD 1800\\ 
11 & SRD 1801 – SRD 2400\\
12 & SRD 2401 – SRD 3000\\ 
13 & SRD 3001 – SRD 3600\\ 
14 & SRD SRD 3601 – SRD 4800\\ 
15 &SRD 4801 – SRD 6000\\
16 &SRD 6000+
    \bottomrule
\end{tabularx}
\end{table} 
 

答案1

我将使用tabularx包并规定每个表中相等的列宽:

在此处输入图片描述

\documentclass[a4paper, 12 pt]{article}
\usepackage{booktabs, tabularx}

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

\begin{document}
    \begin{table}[p]
    \centering
\begin{tabularx}{0.6\linewidth}{>{\centering}p{4em}  X}    
    \toprule
\emph{\textbf{mstatus}} & \textbf{Marital Status}   \\
    \midrule
\emph{Value}            & \emph{Label}              \\
    \addlinespace
    1                   & Single                    \\
    2                   & Married                   \\
    3                   & Common-law marriage       \\
    4                   & Divorced                  \\
    5                   & Separated                 \\
    6                   & Widowed                   \\
    \bottomrule
\end{tabularx}

\vspace{\baselineskip}
\begin{tabularx}{0.6\linewidth}{>{\centering}p{4em}  X}
    \toprule
\emph{\textbf{edlevel}} & \textbf{Education Level}  \\
    \midrule
\emph{Value}            & \emph{Label}              \\
    \addlinespace
    1                   & None/school               \\
    2                   & Middle school/high school \\
    3                   & University/post-secondary \\
    \bottomrule
 \hline
\end{tabularx}

\vspace{\baselineskip}
\begin{tabularx}{0.6\linewidth}{>{\centering}p{4em}  X}
    \toprule
\textbf{\emph{edlevel}} & \textbf{Urban/Rural Cluster}   \\
    \midrule
\emph{Value}            & \emph{Label}              \\
    \addlinespace
    0                   & Urban                     \\
    1                   & Rurall                    \\ 
    \bottomrule
\end{tabularx}
    \end{table}
\end{document}

编辑:

您在我提出的解决方案中添加新表时遇到的错误已记录在您问题下方的 @leandriis 评论中。重复使用它:

  • 你需要精确地书写表格,表格主体中的每个水平线都必须在表格行终止之前(\\\tabularnewline
  • 由于表格可以占据多页,因此它不再被放在table浮动中。相反,你可以使用begin{center} ...\end{center}`:
...
\begin{center}
<your tables>
\end{center}

考虑到问题的新方面,您可以考虑使用xltabular在同名包中定义的表环境,而不是tabularx。此包结合了longtabletabularx功能。使用它们并将故事的宽度扩展为文本宽度,您可以编写以下解决方案:

\documentclass[a4paper, 12 pt]{article}
\usepackage{booktabs, xltabular}

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

\begin{document}
\begin{xltabular}{\linewidth}{>{\centering}p{4em}  X}
    \toprule
\emph{\textbf{mstatus}} & \textbf{Marital Status}   \\
    \midrule
\emph{Value}            & \emph{Label}              \\
    \addlinespace
    1                   & Single                    \\
    2                   & Married                   \\
    3                   & Common-law marriage       \\
    4                   & Divorced                  \\
    5                   & Separated                 \\
    6                   & Widowed                   \\
    \midrule
    \addlinespace
    \midrule
\emph{\textbf{edlevel}} & \textbf{Education Level}  \\
    \midrule
\emph{Value}            & \emph{Label}              \\
    \addlinespace
    1                   & None/school               \\
    2                   & Middle school/high school \\
    3                   & University/post-secondary \\
    \midrule
    \addlinespace
    \midrule
\textbf{\emph{edlevel}} & \textbf{Urban/Rural Cluster}   \\
    \midrule
\emph{Value}            & \emph{Label}              \\
    \addlinespace
    0                   & Urban                     \\
    1                   & Rurall                    \\
    \midrule
    \addlinespace
    \midrule
\textbf{\emph{age}} & \textbf{Age Group}   \\
    \midrule
\emph{Value}            & \emph{Label}              \\
    \addlinespace
    1                   & 15---29\\
    2                   & 30---44\\
    3                   & 45---59\\
    4                   & 60+\\
    \midrule
    \addlinespace
    \midrule
\textbf{\emph{age}} & \textbf{Age Group}   \\
    \midrule
\emph{Value}            & \emph{Label}              \\
    \addlinespace
    1                   & Amerindian                \\
    2                   & Afro-Surinamese           \\
    3                   & Mixed                     \\
    4                   & Hindustani ("East Indians”)\\
    5                   & Javanese                  \\
    6                   & Maroons                   \\
    7                   & Other                     \\
    \midrule
    \addlinespace
    \midrule
\textbf{\emph{age}} & \textbf{Age Group}   \\
    \midrule
\emph{Value}            & \emph{Label}      \\
    \addlinespace
    1   & A salaried employee of the 
          government or an independent 
          state-owned enterprise            \\
    2   & A salaried employee in the 
          private sector                    \\
    3   & Owner or partner in a 
          business/Self-employed            \\
    4   & Unpaid worker                     \\
    \midrule
    \addlinespace
    \midrule
\textbf{\emph{age}} & \textbf{Age Group}    \\
    \midrule
\emph{Value}            & \emph{Label}      \\
    \addlinespace
    0   & 0                                 \\
    1   & SRD 0 – SRD 200                   \\
    2   & SRD 200 – SRD 300                 \\
    3   & SRD 301 – SRD 400                 \\
    4   & SRD 401 – SRD 600                 \\
    5   & SRD 601 – SRD 800                 \\
    6   & SRD 801 – SRD 900                 \\
    7   & SRD 901 – SRD 1000                \\
    8   & SRD 1001 – SRD 1200               \\
    9   & SRD 1201 – SRD 1500               \\
    10  & SRD 1501 – SRD 1800               \\
    11  & SRD 1801 – SRD 2400               \\
    12  & SRD 2401 – SRD 3000               \\
    13  & SRD 3001 – SRD 3600               \\
    14  & SRD SRD 3601 – SRD 4800           \\
    15  & SRD 4801 – SRD 6000               \\
    16  & SRD 6000+                         \\                         
    \bottomrule
\end{xltabular}
\end{document}

以上MWE生成两页长的表:

在此处输入图片描述

如果你想添加更多表格,请将其表格主体插入\bottomrule到现有表格之前,并通过

    \midrule
    \addlinespace
    \midrule

答案2

你是說這樣嗎?

在此处输入图片描述

将其\centering从表格中取出,并在表格之间添加两个\vfills 加两个\vspace*{\baselineskip}s。还有其他解决方案,但这似乎是最简单的方法。

\documentclass[a4paper, 12 pt]{article}
\usepackage{listings}
\usepackage{booktabs}
\usepackage[showframe]{geometry} % <-- just to show margins, remove in real document
\begin{document}
\topskip0pt
\centering
~\vfill

\begin{tabular}{@{} *5l @{}}    \toprule
\emph{\textbf{mstatus}} & \textbf{Marital Status}   \\\midrule
& \emph{Value}    & \emph{Label} \\ 
& 1 & Single\\ 
& 2 & Married\\ 
& 3 & Common-law marriage\\ 
& 4 & Divorced\\ 
& 5 & Seperated\\ 
& 6 & Widowed\\\bottomrule
 \hline
\end{tabular}

\vspace*{\baselineskip}

\begin{tabular}{@{} *5l @{}}    \toprule
\emph{\textbf{edlevel}} & \textbf{Education Level}   \\\midrule
& \emph{Value}    & \emph{Label} \\ 
& 1 & None/school\\ 
& 2 & Middle school/high school\\ 
& 3 & University/post-secondary\\\bottomrule
 \hline
\end{tabular}

\vspace*{\baselineskip}

\begin{tabular}{@{} *5l @{}}    \toprule
\emph{\textbf{edlevel}} & \textbf{Urban/Rural Cluster }   \\\midrule
& \emph{Value}    & \emph{Label} \\ 
& 0 & Urban\\ 
& 1 & Rurall\\ \bottomrule
 \hline
\end{tabular}

\vfill~

\end{document}

相关内容