0

I have a table in which I want rows to have two lines of text. In the first row, I would like "Number of pairs" centered over "of newborn bunnies." In the second row, I would like "January 1" aligned against the left edge of the table and below the date, I would like "Start" indented 3mm from the left edge. (I put a "%" in front of the first two lines of code so that the code for the table would compile.)

I would also appreciate a suggestion for putting the title within the boundaries of the table.

\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}

user143462
  • 1,039

1 Answers1

2

Like this:

enter image description here

Edit: As I understood your problem, you like to have multiline text in cells in columns of type c and l )or in r. In such a case is handy to use makecell package:

\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}

However, in the both examples you can use for columns with numbers p{<width> type of columns, where text is automatically broken into two lines.

For example, the second table version you can write as:

\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}

enter image description here

Zarko
  • 296,517
  • In your table with the \multicolumn{, you have the commands \setlength\extrarowheight{2pt} and \sisetup{table-column-width=4em, table-format=1.0}{\bfseries Fibonacci's Rabbit\\ Population Explosion} before the tabular environment, but you don't have these in the first tabular environment. The widths of the rows in the two tables look the same to me, though. Is 2pt too small to be noticeable? What is "sisetup," and what is the difference between "table-format=1.0" and table-format=2.0"? – user143462 Jul 15 '22 at 18:55
  • @user143462, first table use default values of S columns but the second has defined them. It has in sisetup defined common features for S columns, i.e. column width by table-column-width=4em and table-format=1.0 (numbers in column have only one digit integers) which is at last column overwitted by local setting table-format=2.0 (two digit integers, zero decimals). I don't see any reason why the text Fibonacci's Rabbit Population Explosion should be inside tabular or inside some box. As is written in answer gives nice result at simpler code. – Zarko Jul 15 '22 at 19:22
  • 1
    @user143462, in answer are shown two possible solution. You should use one of them, which you more liked. Both solved your problem, how you can write two line cell content in cels of type c, and r. Beside showed possibilities of course exist other too. For example using p{width} column type ca automatically broke text into more lines. – Zarko Jul 15 '22 at 19:27
  • 1
    @user143462, see edited answer. – Zarko Jul 15 '22 at 19:44
  • I appreciate the options that you bothered to typeset. I will use the first one, but I like the other two. – user143462 Jul 17 '22 at 00:08
  • What is the difference between using the option l and the option S[table-format = 2.0]? What does the 2.0 signify? I looked on stackexchange for a description of this option and did find any. The only options with which I am familiar is l, r, and c. – user143462 Jul 17 '22 at 00:16