在表格行中排版两行文本

在表格行中排版两行文本

我有一张表格,我希望其中的行包含两行文本。在第一行中,我希望“对数”居中显示在“新生兔子数量”上方。在第二行中,我希望“1 月 1 日”与表格的左边缘对齐,在日期下方,我希望“开始”3mm从左边缘缩进。(我在前两行代码前面放置了一个“%”,以便表格的代码可以编译。)

我还希望有人能建议将标题放在表格边界内。

\documentclass{amsart}
\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage{makecell}
\usepackage{stackengine}\setstackEOL{\cr} %EOL is abbreviation for "end of line."
\usepackage{adjustbox}
\usepackage{siunitx}
\usepackage{booktabs}



\begin{document}



\noindent \hspace*{\fill}
\setlength\extrarowheight{2pt}
\belowbaseline[-7pt]{\stackunder{\bfseries\Longstack{Fibonacci's Rabbit Population\cr Explosion}}{%
\begin{tabular}{|| c | S[table-format=-1.0] | S[table-format=-1.0] | S[table-format=-1.0] ||} \hline
%&Number of pairs\cr of newborn bunnies&{Number of pairs\cr of mature rabbits}&{Total number\cr of pairs of rabbits} \\ \Xhline{0.8pt}
%{January 1\cr \hspace{3mm}Start}&1&0&1 \\ \hline
February 1&0&1&1 \\ \hline
March 1&1&1&2 \\ \hline
April 1&1&2&3 \\ \hline
May 1&2&3&5 \\ \hline
June 1&3&5&8 \\ \hline
July 1&5&8&13 \\ \hline
\end{tabular}
}}
\hspace{\fill}



\end{document}

答案1

像这样:

在此处输入图片描述

编辑: 据我了解,您喜欢在类型为cl) 或 的列中的单元格中使用多行文本r。在这种情况下,使用makecell包很方便:

\documentclass{amsart}
%\usepackage{amsmath}     % not needed in this MWE
%\usepackage{amsfonts}    % not needed in this MWE
%\usepackage{stackengine} % not needed in this MWE
%\usepackage{adjustbox}   % not needed in this MWE

\usepackage{booktabs, makecell}
\usepackage{siunitx}

\begin{document}
    \begin{center}
\setlength\extrarowheight{2pt}
{\bfseries  
 Fibonacci's Rabbit Population Explosion
 }
 
\smallskip
\begin{tabular}{|| l | S|S|S[table-format=2.0] ||}
    \hline
    & {\makecell{Number of pairs\\ of newborn bunnies}}
        & {\makecell{Number of pairs\\ of mature rabbits}}
            & {\makecell{Total number\\ of pairs of rabbits}}   \\ 
    \Xhline{0.8pt}
\makecell[l]{January 1\\ \quad Start}
            & 1 & 0 &  1    \\ \hline
February 1  & 0 & 1 &  1    \\ \hline
March 1     & 1 & 1 &  2    \\ \hline
April 1     & 1 & 2 &  3    \\ \hline
May 1       & 2 & 3 &  5    \\ \hline
June 1      & 3 & 5 &  8    \\ \hline
July 1      & 5 & 8 & 13    \\ \hline
\end{tabular}
    \end{center}


\bigskip
    \begin{center}
\setlength\extrarowheight{2pt}
\sisetup{table-column-width=4em,
         table-format=1.0}
{\bfseries
 Fibonacci's Rabbit\\ Population Explosion
 }

\smallskip
\begin{tabular}{|| l | S|S|S[table-format=2.0] ||}
    \hline
    & \multicolumn{3}{c||}{Number of pairs of}    \\
    \cline{2-4}
    & {\makecell{newborn\\ bunnies}}
        & {\makecell{mature\\ rabbits}}
            & {\makecell{Total}}       \\
    \Xhline{0.8pt}
\makecell[l]{January 1\\ \quad Start}
            & 1 & 0 &  1    \\ \hline
February 1  & 0 & 1 &  1    \\ \hline
March 1     & 1 & 1 &  2    \\ \hline
April 1     & 1 & 2 &  3    \\ \hline
May 1       & 2 & 3 &  5    \\ \hline
June 1      & 3 & 5 &  8    \\ \hline
July 1      & 5 & 8 & 13    \\ \hline
\end{tabular}
    \end{center}
\end{document}

但是,在这两个例子中,您都可以使用带有数字p{<width>类型的列,其中文本会自动分成两行。

例如,第二个表格版本你可以写成:

\documentclass{amsart}
\usepackage{array, booktabs, makecell}

\begin{document}
    \begin{center}
\setlength\extrarowheight{2pt}
{\bfseries
 Fibonacci's Rabbit Population Explosion
 }

\smallskip
\begin{tabular}{|| l | *{3}{>{\centering\arraybackslash}m{4em}|} |}
    \hline
    & \multicolumn{3}{c||}{Number of pairs of}    \\
    \cline{2-4}
    & newborn bunnies 
        &  mature rabbits 
            &  Total        \\
    \Xhline{0.8pt}
\makecell[l]{January 1\\ \quad Start}
            & 1 & 0 &  1    \\ \hline
February 1  & 0 & 1 &  1    \\ \hline
March 1     & 1 & 1 &  2    \\ \hline
April 1     & 1 & 2 &  3    \\ \hline
May 1       & 2 & 3 &  5    \\ \hline
June 1      & 3 & 5 &  8    \\ \hline
July 1      & 5 & 8 & 13    \\ \hline
\end{tabular}
    \end{center}
\end{document}

在此处输入图片描述

相关内容