1

I'd like to have a dotted rule like \midrulefrom the booktabs package.

I took code from the booktabs.sty, simplified it and have:

    \documentclass{article}

\usepackage{array}
\usepackage{booktabs}

\makeatletter{}
\def\dotrule{\noalign{\ifnum0=`}\fi
  \@aboverulesep=\aboverulesep
  \global\@belowrulesep=\belowrulesep
  \global\@thisruleclass=\@ne
  \@BTdotted}

\def\@BTdotted{%
{\CT@arc@\hrule\@height\@thisrulewidth}%
    \futurenonspacelet\@tempa\@BTendrule}

\makeatother{}
  
\begin{document}

Text

\begin{tabular}{lr}\toprule
  Huu& Haa \\\dotrule
\end{tabular}

\end{document}

And now I'm stuck replacing the central \hrule\@height\@thisrulewidthwith something that makes not a line, but dots. I've been struggling with \leaders, but didn't get it. Maybe somebody has an idea.

I found lots of similar questions, of course. But the trick is to have a command with parameters of the booktabs package!

4
  • 1
    It will be difficult. \hrule is a primitive which inserts an 'rule' in the vertical list and the computation of the length of the rule is done by TeX very late. On the other side, leaders need a box (and are able to fill that box). But we can't construct a horizontal box of the width of the array during the construction of the array... Sep 16, 2020 at 13:50
  • 1
    It's possible to do something with \multispan but you will have to give the total number of columns of the array as argument of your command \midrule. We can also compute the width of the array (with PGF/Tikz) and store it in the aux file in order to use it in the next run. Sep 16, 2020 at 13:50
  • @F.Pantigny So why did you delete the plain-tex tag? I know that there are solutions, but as you describe your insight in the contruction of \hrule, a replacement with dots seems a real TeX issue.
    – Keks Dose
    Sep 16, 2020 at 13:58
  • 2
    plain TeX is a format (that is to say a set of constructions with TeX primitives which is, in some way, pre-compiled). LaTeX is another format. When you use LaTeX, you don't use plain TeX: you use TeX. Sep 16, 2020 at 14:15

2 Answers 2

5
+100

Here is a command \dotrule which respects the syntax and the parameters of booktabs (aboverulesep, belowrulesep and lightrulewidth) but which is available only in the environment {NiceTabular} of nicematrix. The dotted line is drawn by Tikz (it's possible to change the characteristics of that dotted line with the tools of Tikz).

\documentclass{article}
\usepackage{nicematrix}
\usepackage{booktabs}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{xcolor}


\ExplSyntaxOn
\makeatletter

\cs_set:Npn \dotrule 
  { 
    \noalign \bgroup 
    \peek_meaning:NTF [ 
      { \__dose_dotrule: } 
      { \__dose_dotrule: [ \lightrulewidth ] } 
  }

\cs_set:Npn \__dose_dotrule: [ #1 ]
  {
    \skip_vertical:n { \aboverulesep + \belowrulesep + #1 } 
    \egroup 
    \tl_gput_right:Nx \g_nicematrix_code_after_tl 
      { \__dose_dotrule:nn { \int_use:N \c@iRow } { #1 } }
  }

\cs_new_protected:Nn \__dose_dotrule:nn 
  {
    {
      \dim_set:Nn \l_tmpa_dim { \aboverulesep + ( #2 ) / 2 }
      \CT@arc@
      \tikz \draw [ dotted , line~width = #2 ]
        ([yshift=-\l_tmpa_dim]#1-|1) 
        -- 
        ([yshift=-\l_tmpa_dim]#1-| \int_eval:n { \c@jCol + 1 }) ;
    }   
  }

\makeatother
\ExplSyntaxOff


\begin{document}

\begin{NiceTabular}{cc}
\toprule
Header 1 & Header 2 \\
\dotrule
text & text \\
some text & other text \\
\bottomrule
\end{NiceTabular}
%
\hspace{2cm}
%
\begin{NiceTabular}{cc}
\toprule
Header 1 & Header 2 \\
\midrule
text & text \\
some text & other text \\
\bottomrule
\end{NiceTabular}


\vspace{1cm}


\arrayrulecolor{blue}

\begin{NiceTabular}{cc}
\toprule
Header 1 & Header 2 \\
\dotrule[3pt]% <-- mandatory
text & text \\
some text & other text \\
\bottomrule
\end{NiceTabular}
%
\hspace{2cm}
%
\begin{NiceTabular}{cc}
\toprule
Header 1 & Header 2 \\
\midrule[3pt]
text & text \\
some text & other text \\
\bottomrule
\end{NiceTabular}

\end{document}

Output of the above code

3
  • Instead of improving booktabs you basically improved nicematrix: with your patch it will recognize the booktabs commands with a new feature: dotted lines. I'm skimming through the manual of nicematrix and that package looks really great; booktabs has even been implemented yet! So I'll have to teach the commands of nicematrix to Emacs. I often use datatool and numprint, so maybe I'll come back with a question on that later. However, thank you very much for (a) the nice package and (b) this answer!
    – Keks Dose
    Sep 21, 2020 at 9:38
  • The command \dotrule[3pt] needs a % sign at the end, otherwise the next row in the tabular will begin with a space. Write: \dotrule[1pt]% or whatever value is inside the bracket.
    – Keks Dose
    Sep 30, 2020 at 10:03
  • You are right. I have added a % in the code. Sep 30, 2020 at 11:30
1

An easy solution with booktabs environment of tabularray package:

\documentclass{article}

\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}

\begin{booktabs}{lll}
\toprule
 Alpha   & Beta  & Gamma   \\
\midrule[dashed]
 Epsilon & Zeta  & Eta     \\
\midrule[dotted]
 Iota    & Kappa & Lambda  \\
\bottomrule 
\end{booktabs}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.