Advanced Table Drawing - Part 2

Advanced Table Drawing: Part 2 #

Author: Erhua

Keywords: Merging and splitting cells, fixed-width tables

In the previous section, we discussed the basic aspects of drawing tables. In this section, we will cover merging and splitting cells, as well as fixed-width tables.

Merging and Splitting Cells #

Sometimes we need to merge multiple cells into one. Here’s how you can do it:

  • Use the command \multicolumn{number of cells to merge}{alignment}{cell content} to merge cells in the same row but different columns.
  • Use the \multirow{number of rows}*{content} command to merge cells in the same column but different rows. Note that the * should be included.
\documentclass [border={30mm 3mm 30mm 3pt}]{standalone}
\usepackage{multirow}
\usepackage{ctex}
\begin{document}
\begin{tabular}{|l|c|c|r|}
\hline
\multirow{2}*{Name}&\multicolumn{3}{|c|}{Project}\\
\cline{2-4}
    &Eat&Drink&Play\\
\hline
John&Fish&Wine&Guitar\\
Jane&Meat&Water&Code\\
Mary&Seafood&Beverage&Badminton\\
\hline

\end{tabular}
\end{document}

Key Points:

Here are a few key points about this table:

  • The {|c|} in \multicolumn{3}{|c|}{Project} ensures the presence of vertical lines in the table. If not included, there will be no vertical lines.
  • The \cline{2-4} command helps draw incomplete or interrupted horizontal lines after merging cells.

The top and leftmost rows of the table can be controlled separately using the \makecell package. You can use \\ for line breaks within \makecell{ }.

Diagonal lines in the table header can be created using the \diagbox package.

  • \diagbox{left}{right}
  • \diagbox{left}{center}{right}
\documentclass [border={30mm 3pt 30mm 3pt}]{standalone}
\usepackage{multirow}
\usepackage{diagbox}
\usepackage{ctex}
\begin{document}
\begin{tabular}{|l|c|c|r|}
\hline
\diagbox{Name}{Project}    &Eat&Drink&Play\\
\hline
John&Fish&Wine&Guitar\\
Jane&Meat&Water&Code\\
Mary&Seafood&Beverage&Badminton\\
\hline
\end{tabular}
\end{document}

Scientific Research Paper Three-Line Table #

In most universities, the graduation thesis requires tables to be set as three-line tables, with thicker lines at the top and bottom and thinner lines in between. The ratio of thickness is determined by each institution.

To create a three-line table, we can use the booktabs package:

\toprule: Top line with an optional argument to change the thickness, e.g., \toprule[line width]
\midrule: Middle line
\bottomrule: Bottom line
\cmidrule: Partial middle line
\documentclass [border={30mm 3pt 30mm 3pt}]{standalone}
\usepackage{diagbox}
\usepackage{booktabs}
\usepackage{ctex}
\usepackage{tabularx}
\begin{document}
\begin{tabularx}{\textwidth}{c|X|X|X|X|X|X|X}
\toprule [1mm]
\diagbox{Project}{Date}&Mon&Tue&Wed&Thu&Fri&Sat&Sun\\
\midrule
Ping Pong&1&2&4&1&7&3&6\\
Badminton&6&0&8&1&2&5&8\\
\bottomrule [1mm]
\end{tabularx}
\end{document}

\documentclass [border={30mm 3pt 30mm 3pt}]{standalone}
\usepackage{diagbox}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{ctex}
\usepackage{tabularx}
\begin{document}
\begin{tabular}{c|ccccccc}
\toprule
\multirow{2}*{\diagbox{Project}{Date}}&\multicolumn{1}{c}{Male}&\multicolumn{2}{c}{Female}\\
\cmidrule{2-2}\cmidrule{3-4}
    &Li&Zhang&Wang\\
\midrule
Ping Pong&1&2&4\\
Badminton&6&0&8\\
\bottomrule
\end{tabular}
\end{document}

Using Fixed-Width Tables #

LaTeX’s tabular* environment can generate tables with fixed widths. In general, we use the tabularx environment provided by the tabularx package to achieve the same effect. The tabularx environment introduces a column format specifier X (similar to c, l, and r) that generates columns with fixed widths.

Here is the package environment:

\usepackage{tabularx}
\begin{tabularx}{width}[vertical alignment]{column format (c,l,r,X,p{width})}
%\begin{tabularx}{\textwidth}[vertical alignment]{|c|l|r|X|p{10pt}|}
\end{tabularx}
\documentclass [border={30mm 3pt 30mm 3pt}]{standalone}
\usepackage{diagbox