2

The following code has LaTeX make a table. In the header, I want "End of the" over "kth week"in the first column and "Total Number of" over "Copies Sold" in the second column.

    \documentclass{amsart}

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

    \begin{document}

    \setlength\extrarowheight{2pt}\stackon{%
\begin{tabular}{|| c | c||} \hline
End of the \textit{k}\textsuperscript{th} week  &   Total Number of Copies Sold \\ \Xhline{0.8pt}
k=1 &   3200 \\ \hline
k=2 &   5500 \\ \hline
k=3 &   6800 \\ \hline
k=4 &   7400 \\ \hline
k=5 &   7700 \\ \hline
\end{tabular}
}
{\bfseries\Longstack{Sales of Book \textit{A}}}

\end{document}
2
  • How do you want the alignment of the split header cell parts? Centered or left (or right)? Commented Jun 1, 2015 at 17:12
  • @Steven B. Segletes Please show me the code for the display to have the text in the split header cells centered and include a comment to have the text flush left or flush right.
    – user74973
    Commented Jun 1, 2015 at 21:18

3 Answers 3

3

It's easy with the \thead command from makecell. I took the opportunity to improve your table with \hhline and changed the value of stackdap, as the title was much too close to the first \hline:

\documentclass{amsart}

\usepackage{adjustbox}\usepackage{mathtools}
\usepackage{hhline}
\usepackage{array}\usepackage{makecell}
\usepackage{stackengine}\setstackEOL{\cr}%EOL is abbreviation for "end of line."
\setstackgap{S}{2ex}
\setcellgapes{3pt}
\begin{document}

\stackon{\makegapedcells%
    \begin{tabular}{|| c | c||}
        \hline
        \thead{End of the & \\\textit{k}\textsuperscript{th} week} & \thead{Total Number of\\ Copies Sold} \\ \Xhline{0.8pt}
        $ k=1 $ & 3200 \\ \hhline{||--||}
        $ k=2 $ & 5500 \\ \hhline{||--||}
        $ k=3 $ & 6800 \\ \hhline{||--||}
        $ k=4 $ & 7400 \\ \hhline{||--||}
        $ k=5 $ & 7700 \\ \hline
    \end{tabular}
}
{\bfseries\Longstack{Sales of Book \textit{A}}}

\end{document} 

enter image description here

1

Do you mind something like this:

enter image description here

Code:

    \documentclass{article}

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

    \begin{document}

    \setlength\extrarowheight{2pt}\stackon{%
\begin{tabular}{|| c | c||} \hline
\thead{End of the\\ \textit{k}\textsuperscript{th} week}
    &   \thead{Total Number of\\ Copies Sold} \\ \Xhline{0.8pt}
k=1 &   3200 \\ \hline
k=2 &   5500 \\ \hline
k=3 &   6800 \\ \hline
k=4 &   7400 \\ \hline
k=5 &   7700 \\ \hline
\end{tabular}
}
{\bfseries\Longstack{Sales of Book \textit{A}}}

\end{document}

Many people have opinion, that table are more beautiful if they hasn't vertical lines:

    \documentclass{article}

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

    \begin{document}

    \setlength\extrarowheight{2pt}\stackon{%
\begin{tabular}{c c} 
    \toprule
\thead{End of the\\ \textit{k}\textsuperscript{th} week}
    &   \thead{Total Number of\\ Copies Sold} \\ 
    \midrule
k=1 &   3200 \\ 
k=2 &   5500 \\ 
k=3 &   6800 \\ 
k=4 &   7400 \\ 
k=5 &   7700 \\ 
    \bottomrule
\end{tabular}
}
{\bfseries\Longstack{Sales of Book \textit{A}}}

\end{document}

enter image description here

6
  • May I suggest you make th last version still nicer using two \cmidrule instead of one midrule?
    – Bernard
    Commented Jun 1, 2015 at 17:22
  • @Bernard To what package does \thead belong? Does it just allow two lines? What does midrule instruct LaTeX to draw? What is the difference between cmidrule and midrule?
    – user74973
    Commented Jun 1, 2015 at 17:32
  • @Bernard, with which option do you think that cmidrule will be nicer than midrule?
    – Zarko
    Commented Jun 1, 2015 at 17:42
  • \thead is from makecell. The difference between \midrule and \cmidrule is the same as between \hline and \cline: it draws rules across a selected number of columns, instead of the whole table.
    – Bernard
    Commented Jun 1, 2015 at 20:25
  • @Zarko: \cmidrule can be trimmed at both ends (independently), so two\cmidrules can be separated by a small space. Try \cmidrule(lr){1-1}\cmidrule(lr){2-2}, you'll see how it looks like.
    – Bernard
    Commented Jun 1, 2015 at 20:30
1

Here, I split the header using a stack. The OP asked in a comment how one would define the alignment of the split header. I show it centered, but add this line in the code, that will define the alignment of the split headers lines:

\renewcommand\stackalignment{c}% <--- USE l,c, OR r TO SET SPLIT HEADER 

The headers will be centered with respect to the data, as given by the {|c|c|} argument to the tabular, but the individual rows of the header will be aligned according to the above \stackalignment setting.

The full MWE:

\documentclass{amsart}

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

\begin{document}
\setlength\extrarowheight{2pt}\stackon{%
\renewcommand\stackalignment{c}% <--- USE l,c, OR r TO SET SPLIT HEADER ALIGNMENT
\begin{tabular}{|| c | c||} \hline
\stackanchor{End of the}{\textit{k}\textsuperscript{th} week}  &  
\addstackgap{\stackanchor{Total Number of}{Copies Sold}} \\ \Xhline{0.8pt}
k=1 &   3200 \\ \hline
k=2 &   5500 \\ \hline
k=3 &   6800 \\ \hline
k=4 &   7400 \\ \hline
k=5 &   7700 \\ \hline
\end{tabular}
}
{\bfseries\Longstack{Sales of Book \textit{A}}}

\end{document}

enter image description here

4
  • Why do you need \renewcommand in \renewcommand\stackalignment{c}? Is \stackalignment a command in the package array, makecell, or stackengine?
    – user74973
    Commented Jun 2, 2015 at 13:46
  • I don't plan to do much more with tables than the coding that you and Bernard have provided. I would like to pick one style and be consistent.
    – user74973
    Commented Jun 2, 2015 at 13:46
  • @user74973 \stackalignment is a stackengine definition that takes on one of 3 valid values: l, c, or r and defines the default horizontal alignment of any stack that follows. In order to reset the definition, one uses either \renewcommand or \def. Commented Jun 2, 2015 at 13:49
  • @user74973 The reason I chose (being the package author) to force the use of \def or \renewcommand to redefine it, is that it allows the use of \stackalignment as a macro argument, which will output the single letter of l, c, or r. For example, I can say \ShortStack[\stackalignment]{a\cr bbbb} with the provided syntax, which I could not do if \stackalignment were a macro that took an argument to actually set the alignment. Commented Jun 2, 2015 at 13:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .