How to Extend a Line in a Table – Comprehensive Guide

arraysdata structuresmulticolumnrulestables

I wish to have a Table setting like the following graph with an extra line inserted between a and b. Namely, I wish to extend the line separating 1, 4 and 2, 5 to the line separating a and b.

The figure is the output from my code, however, it does not have an extra line inserted separating a, 1, 4 and b, 2, 5. How to do that? Is there an elegant way to do it? My code is rather ugly attached below. There may be an excellent beautiful way to do it.

enter image description here

  \documentclass[aps]{revtex4} 
\usepackage{amsmath,amssymb}


\begin{document}


  \begin{table}[h]
    \begin{tabular}{c||c|c }
      & $K$ & $H$ \\ \hline\hline
    A \vline~~$a$ & 1& 4    \\ \cline{2-3}
    \;B \vline~~ $b$ & $2$ & $5$\\ \hline
    C& $3$ &  $6$\\   
    \end{tabular}
    \end{table}

\end{document}

Best Answer

table

\documentclass[aps]{revtex4}
\usepackage{array,mathtools,amssymb}
\newcolumntype{C}{>{$}c<{$}}
\begin{document}

  \begin{table}[h]
    \begin{tabular}{c|C||C|C }
      \multicolumn{2}{c||}{} & K & H \\ \hline\hline
      A & a & 1& 4    \\ \cline{2-4}
      \;B & b & 2 & 5\\ \hline
      \multicolumn{2}{c||}{C}& 3 &  6\\
    \end{tabular}
  \end{table}

\end{document}

Note that it is not recommended to typeset tables like this. This many rules make the table look crowded and does not help readers parse it easily. Indeed, professional-quality tables do not typically include any vertical rules, use horizontal rules sparingly and use variations in weight rather than doubling. They also include rather more space to help separate distinct cells. The documentation of booktabs has a lot to say on this subject and the package can be used to implement these recommendations.

For example:

booktabs version

However, booktabs seems to disagree with revtex so I had to switch to article for demonstration purposes:

\documentclass{article}
\usepackage{array,mathtools,amssymb,booktabs}
\newcolumntype{C}{>{$}c<{$}}
\begin{document}
    \begin{tabular}{cCCC}
      \toprule
      \multicolumn{2}{c}{} & K & H\\\midrule
      A & a & 1& 4\\\cmidrule(lr){2-4}
      B & b & 2 & 5\\\cmidrule(lr){1-4}
      \multicolumn{2}{c}{C}& 3 & 6\\\bottomrule
    \end{tabular}
\end{document}

In any case, if you are submitting to a journal or conference and they want a particular style, you are stuck with it and typographic aesthetics is irrelevant. (Although the documentation suggests that you should perhaps be using revtex's versions of these commands in relevant places. Without, naturally, loading booktabs, which would mess them up.)