Convert this table to latex

As shown, this is a relatively complex table to format well since it requires the following elements:

  • rotated cells -> rotating package
  • cells which span multiple columns -> \multicolumn{<number of columns>}{<alignment>}{<content>}
  • cells which span multiple rows -> multirow package

In addition, LaTeX tables do not, by default allow sufficient vertical spacing. This problem is exacerbated by the use of horizontal rules. Moreover, some horizontal rules should typically be heavier than others. To accommodate these considerations in a table with vertical rules, I used makecell. I also used array which is a basic extension of the standard LaTeX syntax and makes it a bit easier to customise things.

So, in the document preamble, we have

\usepackage{rotating,multirow,makecell,array}

to load the packages. I used 3 different rule weights: standard, a bit thicker and thicker still. For convenience, we define the non-standard rule heights in one place so they can be easily modified and consistently applied.

\newlength\xlineht
\newlength\mlineht
\setlength\xlineht{1.2pt}
\setlength\mlineht{.8pt}

We define a couple of extra column types for convenience and consistency again.

\newcolumntype{C}{c!{\vrule width \xlineht}}
\newcolumntype{M}{c!{\vrule width \mlineht}}

makecell allows us to configure some extra spacing around cells, which we need. So we set some things up.

\setcellgapes{3pt}

We want our headers to be bold.

\renewcommand\theadfont{\bfseries}

That's it for the preamble. Now, in the document itself.

\begin{table}

Put the table in the table environment if you want it to float. If you do this, you probably want to use a \caption{} and \label{} so you can refer to it in the text.

If you don't want the table to move from where you put it, use the center environment instead.

  \makegapedcells

This applies our extra spacing around cells.

  \begin{tabular}{!{\vrule width \xlineht}c|M*{3}{c|}C}

Here's the tabular specification: start at the left with a vertical rule of width thicker-still; then a centred column and a standard rule; then one of our custom columns, M; then 3 centred columns followed by standard rules; finally another custom column, C.

    \Xhline{\xlineht}

A rule of height thicker-still to start.

    \multicolumn{2}{!{\vrule width \xlineht}M}{}& \multicolumn{2}{c|}{\thead{C1}} & \multicolumn{2}{C}{\thead{C2}}\\\cline{3-6}

The first header row, using \multicolumn for cells spanning 2 columns and \thead for header content, followed by a partial rule.

    \multicolumn{2}{!{\vrule width \xlineht}M}{}& \thead{P1} & \thead{P2} & \thead{P1} & \thead{P2} \\\cline{3-6}

The second header row.

    \multicolumn{2}{!{\vrule width \xlineht}M}{}& {0.33} & {0.66} & {0.2} & {0.8} \\\Xhline{\mlineht}

The third header row with cell contents not formatted as headers, followed by a thicker rule.

Now for the first cell spanning multiple rows (9), which is also rotated. We use \rotatebox rather than makecell here because I couldn't figure out how to do this with makecell's own rotation wrappers.

    \multirowcell{9}{\rotatebox[]{-90}{\theadfont Different alpha}} & 0.1 & \multicolumn{2}{c|}{9} & \multicolumn{2}{C}{25} \\\cline{2-6}

And some more column-spanning content and a partial rule.

We continue in the same way.

    & 0.2 & \multicolumn{2}{c|}{9} & \multicolumn{2}{C}{67} \\\cline{2-6}
    & 0.3 & \multicolumn{2}{c|}{9} & \multicolumn{2}{C}{890} \\\cline{2-6}
    & 0.4 & \multicolumn{2}{c|}{9} & \multicolumn{2}{C}{34} \\\cline{2-6}
    & 0.5 & \multicolumn{2}{c|}{9} & \multicolumn{2}{C}{23} \\\cline{2-6}
    & 0.6 & \multicolumn{2}{c|}{9} & \multicolumn{2}{C}{25} \\\cline{2-6}
    & 0.7 & \multicolumn{2}{c|}{69} & \multicolumn{2}{C}{54} \\\cline{2-6}
    & 0.8 & \multicolumn{2}{c|}{9} & \multicolumn{2}{C}{21} \\\cline{2-6}
    & 0.9 & \multicolumn{2}{c|}{69} & \multicolumn{2}{C}{55} \\\hline

Here comes the second rotated cell spanning multiple columns.

    \multirowcell{9}{\rotatebox[]{-90}{\theadfont Different beta}} & 0.1 & \multicolumn{2}{c|}{19} & \multicolumn{2}{C}{69} \\\cline{2-6}
    & 0.2 & \multicolumn{2}{c|}{99} & \multicolumn{2}{C}{83} \\\cline{2-6}
    & 0.3 & \multicolumn{2}{c|}{99} & \multicolumn{2}{C}{35} \\\cline{2-6}
    & 0.4 & \multicolumn{2}{c|}{99} & \multicolumn{2}{C}{64} \\\cline{2-6}
    & 0.5 & \multicolumn{2}{c|}{99} & \multicolumn{2}{C}{46} \\\cline{2-6}
    & 0.6 & \multicolumn{2}{c|}{99} & \multicolumn{2}{C}{12} \\\cline{2-6}
    & 0.7 & \multicolumn{2}{c|}{99} & \multicolumn{2}{C}{16} \\\cline{2-6}
    & 0.8 & \multicolumn{2}{c|}{99} & \multicolumn{2}{C}{71} \\\cline{2-6}
    & 0.9 & \multicolumn{2}{c|}{99} & \multicolumn{2}{C}{38} \\\Xhline{\xlineht}

We end with a thicker-still rule.

  \end{tabular}
\end{table}

fancy table

Complete code:

\documentclass{article}
\usepackage{rotating,multirow,makecell,array}
\newlength\xlineht
\newlength\mlineht
\setlength\xlineht{1.2pt}
\setlength\mlineht{.8pt}
\newcolumntype{C}{c!{\vrule width \xlineht}}
\newcolumntype{M}{c!{\vrule width \mlineht}}
\setcellgapes{3pt}
\renewcommand\theadfont{\bfseries}
\begin{document}
\begin{table}
  \makegapedcells
  \begin{tabular}{!{\vrule width \xlineht}c|M*{3}{c|}C}
    \Xhline{\xlineht}
    \multicolumn{2}{!{\vrule width \xlineht}M}{}& \multicolumn{2}{c|}{\thead{C1}} & \multicolumn{2}{C}{\thead{C2}}\\\cline{3-6}
    \multicolumn{2}{!{\vrule width \xlineht}M}{}& \thead{P1} & \thead{P2} & \thead{P1} & \thead{P2} \\\cline{3-6}
    \multicolumn{2}{!{\vrule width \xlineht}M}{}& {0.33} & {0.66} & {0.2} & {0.8} \\\Xhline{\mlineht}
    \multirowcell{9}{\rotatebox[]{-90}{\theadfont Different alpha}} & 0.1 & \multicolumn{2}{c|}{9} & \multicolumn{2}{C}{25} \\\cline{2-6}
    & 0.2 & \multicolumn{2}{c|}{9} & \multicolumn{2}{C}{67} \\\cline{2-6}
    & 0.3 & \multicolumn{2}{c|}{9} & \multicolumn{2}{C}{890} \\\cline{2-6}
    & 0.4 & \multicolumn{2}{c|}{9} & \multicolumn{2}{C}{34} \\\cline{2-6}
    & 0.5 & \multicolumn{2}{c|}{9} & \multicolumn{2}{C}{23} \\\cline{2-6}
    & 0.6 & \multicolumn{2}{c|}{9} & \multicolumn{2}{C}{25} \\\cline{2-6}
    & 0.7 & \multicolumn{2}{c|}{69} & \multicolumn{2}{C}{54} \\\cline{2-6}
    & 0.8 & \multicolumn{2}{c|}{9} & \multicolumn{2}{C}{21} \\\cline{2-6}
    & 0.9 & \multicolumn{2}{c|}{69} & \multicolumn{2}{C}{55} \\\hline
    \multirowcell{9}{\rotatebox[]{-90}{\theadfont Different beta}} & 0.1 & \multicolumn{2}{c|}{19} & \multicolumn{2}{C}{69} \\\cline{2-6}
    & 0.2 & \multicolumn{2}{c|}{99} & \multicolumn{2}{C}{83} \\\cline{2-6}
    & 0.3 & \multicolumn{2}{c|}{99} & \multicolumn{2}{C}{35} \\\cline{2-6}
    & 0.4 & \multicolumn{2}{c|}{99} & \multicolumn{2}{C}{64} \\\cline{2-6}
    & 0.5 & \multicolumn{2}{c|}{99} & \multicolumn{2}{C}{46} \\\cline{2-6}
    & 0.6 & \multicolumn{2}{c|}{99} & \multicolumn{2}{C}{12} \\\cline{2-6}
    & 0.7 & \multicolumn{2}{c|}{99} & \multicolumn{2}{C}{16} \\\cline{2-6}
    & 0.8 & \multicolumn{2}{c|}{99} & \multicolumn{2}{C}{71} \\\cline{2-6}
    & 0.9 & \multicolumn{2}{c|}{99} & \multicolumn{2}{C}{38} \\\Xhline{\xlineht}
  \end{tabular}
\end{table}
\end{document}

Note, however, that professional-quality tables do not typically use vertical rules at all and use horizontal rules much more sparingly. For an informed, if possibly slightly extreme, discussion, see the manual of the booktabs package.

For example, I would probably use something like this.

less is more

This overdoes the rules a little by the standards of booktabs, but is much simpler and clearer than the original. As a bonus, the code is much, much simpler! Less really is more where tables are concerned and less is fortunately easier by far to typeset.

In this case, we load booktabs instead of makecell,array and we roll our own header font command with

\newcommand*\myheadfont{\bfseries}

Dropping the vertical rules means that the tabular specifier is just *{6}{c} for 6 centred columns.

Using booktabs gives us

  • \toprule for the rule at the top;
  • \bottomrule for the rule at the bottom;
  • \midrule for rules in the middle;
  • \cmidrule(){} for partial rules in the middle.

In particular, we can 'trim` partial rules to the left and/or right to create a gap between consecutive rules in the header.

gap

We aren't using makecell now, so we use \multirow for the cells spanning multiple columns, with * specifying the natural width.

Complete code:

\documentclass{article}
\usepackage{rotating,multirow,booktabs}
\newcommand*\myheadfont{\bfseries}
\begin{document}
\begin{table}
  \begin{tabular}{*{6}{c}}
    \toprule
    && \multicolumn{2}{c}{\myheadfont C1} & \multicolumn{2}{c}{\myheadfont C2}\\\cmidrule(lr){3-4}\cmidrule(lr){5-6}
    && \myheadfont P1 & \myheadfont P2 & \myheadfont P1 & \myheadfont P2 \\
    && {0.33} & {0.66} & {0.2} & {0.8} \\
    \midrule
    \multirow{9}*{\rotatebox[]{-90}{\myheadfont Different alpha}} & 0.1 & \multicolumn{2}{c}{9} & \multicolumn{2}{c}{25} \\
    & 0.2 & \multicolumn{2}{c}{9} & \multicolumn{2}{c}{67} \\
    & 0.3 & \multicolumn{2}{c}{9} & \multicolumn{2}{c}{890} \\
    & 0.4 & \multicolumn{2}{c}{9} & \multicolumn{2}{c}{34} \\
    & 0.5 & \multicolumn{2}{c}{9} & \multicolumn{2}{c}{23} \\
    & 0.6 & \multicolumn{2}{c}{9} & \multicolumn{2}{c}{25} \\
    & 0.7 & \multicolumn{2}{c}{69} & \multicolumn{2}{c}{54} \\
    & 0.8 & \multicolumn{2}{c}{9} & \multicolumn{2}{c}{21} \\
    & 0.9 & \multicolumn{2}{c}{69} & \multicolumn{2}{c}{55} \\\midrule
    \multirow{9}*{\rotatebox[]{-90}{\myheadfont Different beta}} & 0.1 & \multicolumn{2}{c}{19} & \multicolumn{2}{c}{69} \\
    & 0.2 & \multicolumn{2}{c}{99} & \multicolumn{2}{c}{83} \\
    & 0.3 & \multicolumn{2}{c}{99} & \multicolumn{2}{c}{35} \\
    & 0.4 & \multicolumn{2}{c}{99} & \multicolumn{2}{c}{64} \\
    & 0.5 & \multicolumn{2}{c}{99} & \multicolumn{2}{c}{46} \\
    & 0.6 & \multicolumn{2}{c}{99} & \multicolumn{2}{c}{12} \\
    & 0.7 & \multicolumn{2}{c}{99} & \multicolumn{2}{c}{16} \\
    & 0.8 & \multicolumn{2}{c}{99} & \multicolumn{2}{c}{71} \\
    & 0.9 & \multicolumn{2}{c}{99} & \multicolumn{2}{c}{38} \\
    \bottomrule
  \end{tabular}
\end{table}
\end{document}

I'll build this up gradually, I'd recommend you compile each snippet with pdflatex at each step:

\documentclass{article}
\begin{document}
hello world
\end{document}

Now add the tabular

\documentclass{article}
\begin{document}
\begin{tabular}{cccc}
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
\end{tabular}
\end{document}

add the horizontal and vertical lines

\documentclass{article}
\begin{document}
\begin{tabular}{|c|c|c|c|}
  \hline
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    \hline
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    \hline
\end{tabular}
\end{document}

add the column headers

\documentclass{article}
\begin{document}
\begin{tabular}{|c@{}|@{}c@{}|@{}c@{}|@{}c@{}|}
  \hline
  & & \begin{tabular}{@{}c@{}|c@{}}
    \multicolumn{2}{c}{C1}\\ \hline
    P1 & P2 \\ \hline
    0.33 & 0.62\\
  \end{tabular}
  &
  \begin{tabular}{c@{}|c@{}}
    \multicolumn{2}{c}{C2}\\ \hline
    P1 & P2 \\ \hline
    0.33 & 0.62\\
  \end{tabular}
  \\\hline
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    \hline
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    \hline
\end{tabular}
\end{document}

add the multirow magic, with rotatebox from graphicx

\documentclass{article}
\usepackage{multirow}
\usepackage{graphicx}
\begin{document}
\begin{tabular}{|c@{}@{}c@{}|@{}c@{}|@{}c@{}|}
  \hline
  & & \begin{tabular}{@{}c@{}|c@{}}
    \multicolumn{2}{c}{C1}\\ \hline
    P1 & P2 \\ \hline
    0.33 & 0.62\\
  \end{tabular}
  &
  \begin{tabular}{c@{}|c@{}}
    \multicolumn{2}{c}{C2}\\ \hline
    P1 & P2 \\ \hline
    0.33 & 0.62\\
  \end{tabular}
  \\\hline
  \multirow{6}{1cm}{\rotatebox{90}{different alpha}}  & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    \hline
  \multirow{6}{1cm}{\rotatebox{90}{different beta}}  & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    \hline
\end{tabular}
\end{document}

Notes:

  • @{} removes column space
  • you don't have to use a tabular for your header, but without it, you might have to do more multicolumn work
  • I'd enhance this table by using siuntix S column, and combine it with booktabs

Here's a version of how I'd consider presenting it:

screenshot

\documentclass{article}
\usepackage{multirow}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{siunitx}
\begin{document}
\begin{tabular}{ccSS}
  \toprule
  & & {\begin{tabular}{cc}
    \multicolumn{2}{c}{C1}\\ \midrule
    P1 & P2 \\ \midrule
    0.33 & 0.62\\
  \end{tabular}}
  &
  {\begin{tabular}{cc}
    \multicolumn{2}{c}{C2}\\ \midrule
    P1 & P2 \\ \midrule
    0.33 & 0.62\\
  \end{tabular}}
  \\\midrule
  \multirow{6}{1cm}{\rotatebox{90}{different alpha}}  & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    \midrule
  \multirow{6}{1cm}{\rotatebox{90}{different beta}}  & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    & 0.1 & 9 & 24 \\ 
    \bottomrule
\end{tabular}
\end{document}

Tags:

Tables