9

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}
6
  • 1
    But why? Do you have tables next to each other where one uses hline or cline, and the other uses cmidrule? Or are you wanting to remove a bit of vertical space from around the cmidrule?
    – Teepeemm
    Feb 4, 2022 at 19:23
  • @Teepeemm: Several reasons: 1. My table barely fits on a page, so don't want to add additional vertical space. My first column is a label for two rows of data. This rule in the first column would serve as a clear divider that the first column is a \multirow label. The MWE is not illutrative of my actual use case, but to make it easier to see/test the desired outcome. Feb 4, 2022 at 19:30
  • use cmidrul but set the vertical space (to 0pt for example) Feb 4, 2022 at 19:59
  • @DavidCarlisle: Is that a built in option? Will look into it. Feb 4, 2022 at 21:15
  • all booktabs rules have \aboverulesep and\belowrulesep so you can set that to 0 then use \addinespace where you want to add some eg around a toprule or ... Feb 4, 2022 at 21:48

2 Answers 2

2

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.

0
5

It is easier to use tblr environment of tabularray package:

\documentclass[border=5mm]{standalone}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\SetTblrInner{rowsep=1pt}
\begin{document}
  \begin{tblr}{ l c r }
    \toprule
    1 & 2 & 3 \\
    \hline
    4 & 5 & 6 \\
    7 & 8 & 9 \\
    \bottomrule
  \end{tblr}
  \begin{tblr}{ l c r }
    \toprule
    1 & 2 & 3 \\
    \cline{1-1}
    4 & 5 & 6 \\
    7 & 8 & 9 \\
    \bottomrule
  \end{tblr}
  \begin{tblr}{ l c r }
    \toprule
    1 & 2 & 3 \\
    \cmidrule[lr]{1-1}
    4 & 5 & 6 \\
    7 & 8 & 9 \\
    \bottomrule
  \end{tblr}
\end{document}

enter image description here

1
  • Sorry can't test this yet as it needs an update to the TeXLive 2021 packages. Feb 5, 2022 at 5:45

You must log in to answer this question.

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