Tables Booktabs Cline – Finding \cline Equivalent that Allows (LR) Trim

booktabsclinetables

I am looking for the equivalent of \cline{1-1} that does not affect the vertical spacing, but with the option to be able to apply a trim such as (lr) that \cmidrule allows.

The MWE below produces:

enter image description here

I would like the third table to align vertically with the first two but with the short "\cmidrule(lr){1-1}" equivalent horizontal line.

Code:

\documentclass[border=5mm]{standalone}
\usepackage{booktabs}

\begin{document}
  \begin{tabular}{ l c r }
    \toprule
    1 & 2 & 3 \\
    \hline
    4 & 5 & 6 \\
    7 & 8 & 9 \\
    \bottomrule
  \end{tabular}
  \begin{tabular}{ l c r }
    \toprule
    1 & 2 & 3 \\
    \cline{1-1}
    4 & 5 & 6 \\
    7 & 8 & 9 \\
    \bottomrule
  \end{tabular}
  \begin{tabular}{ l c r }
    \toprule
    1 & 2 & 3 \\
    \cmidrule(lr){1-1}
    4 & 5 & 6 \\
    7 & 8 & 9 \\
    \bottomrule
  \end{tabular}
\end{document}

Best Answer

Putting David Carlisle's ideas in to practise, by adding two new dimensions \abovecrulesep and \belowcrulesep for \cmidrules, gives

enter image description here

\documentclass[border=5mm]{standalone}

\usepackage{booktabs}
\usepackage{etoolbox}

\newdimen\abovecrulesep
\newdimen\belowcrulesep
\abovecrulesep=0pt
\belowcrulesep=0pt
\makeatletter
\patchcmd{\@@@cmidrule}{\aboverulesep}{\abovecrulesep}{}{}
\patchcmd{\@xcmidrule}{\belowrulesep}{\belowcrulesep}{}{}
\makeatother

\begin{document}
  \begin{tabular}{ l c r }
    \toprule
    1 & 2 & 3 \\
    \hline
    4 & 5 & 6 \\
    7 & 8 & 9 \\
    \bottomrule
  \end{tabular}
  \begin{tabular}{ l c r }
    \toprule
    1 & 2 & 3 \\
    \cline{1-1}
    4 & 5 & 6 \\
    7 & 8 & 9 \\
    \bottomrule
  \end{tabular}
  \begin{tabular}{ l c r }
    \toprule
    1 & 2 & 3 \\
    \cmidrule(lr){1-1}
    4 & 5 & 6 \\
    7 & 8 & 9 \\
    \bottomrule
  \end{tabular}
\end{document}

To provide a new command whilst keeping the old version of \cmidrule one can do the following

Second sample

\documentclass[border=5mm]{standalone}

\usepackage{booktabs}
\usepackage{etoolbox}

\newdimen\myaboverulesep
\newdimen\mybelowrulesep
\newdimen\mycmidrulewidth
\myaboverulesep=0pt
\mybelowrulesep=0pt
\mycmidrulewidth=\cmidrulewidth

\makeatletter
\def\mycmidrule{\noalign{\ifnum0=`}\fi
    \@ifnextchar[{\@mycmidrule}{\@mycmidrule[\mycmidrulewidth]}}
\def\@mycmidrule[#1]{\@ifnextchar({\@@mycmidrule[#1]}{\@@mycmidrule[#1]()}}
\def\@@mycmidrule[#1](#2)#3{\@@@mycmidrule[#3]{#1}{#2}}
\let\@@@mycmidrule\@@@cmidrule
\let\@myxcmidrule\@xcmidrule
\patchcmd{\@@@mycmidrule}{\aboverulesep}{\myaboverulesep}{}{}
\patchcmd{\@@@mycmidrule}{\@xcmidrule}{\@myxcmidrule}{}{}
\patchcmd{\@myxcmidrule}{\belowrulesep}{\mybelowrulesep}{}{}
\makeatother

\begin{document}
  \begin{tabular}{ l c r }
    \toprule
    1 & 2 & 3 \\
    \hline
    4 & 5 & 6 \\
    \cmidrule(lr){1-1}
    7 & 8 & 9 \\
    \bottomrule
  \end{tabular}
  \begin{tabular}{ l c r }
    \toprule
    1 & 2 & 3 \\
    \cline{1-1}
    4 & 5 & 6 \\
    \cmidrule(lr){1-1}
    7 & 8 & 9 \\
    \bottomrule
  \end{tabular}
  \begin{tabular}{ l c r }
    \toprule
    1 & 2 & 3 \\
    \mycmidrule(lr){1-1}
    4 & 5 & 6 \\
    \cmidrule(lr){1-1}
    7 & 8 & 9 \\
    \bottomrule
  \end{tabular}
\end{document}

The three \defs are copies of the internal \cmidrule commands that refer to \my... versions instead. They deal with argument parsing for \cmidrule and are so short it is easy to copy and modify the code. We then copy the other two internal commands from \cmidrule with \let and patch them to use our new commands and parameters. These definitions are longer, so patching is shorter than rewriting.