8

I want to get the equivalent dashed version of \cmidule(lr){1-2}. Looks like the solution is not trivial.

What is the easiest way you think of?

Werner
  • 603,163
rph
  • 359
  • 2
    Perhaps these answers can help: http://tex.stackexchange.com/questions/169098/dotted-line-instead-of-hline-in-table-environment/229334#229334 and http://tex.stackexchange.com/questions/245020/draw-dash-like-midrule-in-table-environment/245046#245046 – Steven B. Segletes Jul 13 '16 at 10:13

2 Answers2

12

The following implements a new "draw type", equating (somewhat) \cdashlinelr to \cmidrule(lr) the regular vertical spacing:

enter image description here

\documentclass{article}

\usepackage{booktabs,arydshln}

\makeatletter
\def\adl@drawiv#1#2#3{%
        \hskip.5\tabcolsep
        \xleaders#3{#2.5\@tempdimb #1{1}#2.5\@tempdimb}%
                #2\z@ plus1fil minus1fil\relax
        \hskip.5\tabcolsep}
\newcommand{\cdashlinelr}[1]{%
  \noalign{\vskip\aboverulesep
           \global\let\@dashdrawstore\adl@draw
           \global\let\adl@draw\adl@drawiv}
  \cdashline{#1}
  \noalign{\global\let\adl@draw\@dashdrawstore
           \vskip\belowrulesep}}
\makeatother

\begin{document}

\begin{tabular}{lll}
  \toprule
  This is a & nice table  & with three columns \\
  \cmidrule(lr){2-3}
  Some      & row content & in the second row  \\
  \bottomrule
\end{tabular}
\begin{tabular}{lll}
  \toprule
  This is a & nice table  & with three columns \\
  \cdashlinelr{2-3}
  Some      & row content & in the second row  \\
  \bottomrule
\end{tabular}

\end{document}
Werner
  • 603,163
  • Hi @werner, the function you've provided works great for a single dashed line, but breaks if I try to introduce several of them in one line (see code below). I've tried to fix it, but I don't understand enough to be able to do it. Could you help? \begin{tabular}{lll} \toprule This is a & nice table & with three columns \\ \cdashlinelr{1-1} \cdashlinelr{2-2} \cdashlinelr{3-3} Some & row content & in the second row \\ \bottomrule \end{tabular} – tales Apr 26 '19 at 16:55
  • Thank you, @Werner! This is exactly what I was looking for. It would be awesome if you could give a brief explanation of the parameters inside the new definition, and how we can tweak them. – Henrique Ferrolho Oct 06 '20 at 13:50
  • 1
    @HenriqueFerrolho: You can adjust the lengths \dashlinedash (to change the dashed line length) and \dashlinegap (to change the gap length). Defaults for both are 4.0pt. Try, for example, with \setlength{\dashlinedash}{2\dashlinedash}. – Werner Oct 06 '20 at 18:57
1

Here is what you can do with {NiceTabular} of nicematrix.

That environment is similar to the classical environment {tabular} (of array) but creates PGF/Tiks nodes under the cells, rows and columns.

It's possible to use those nodes to draw whatever rule you want with Tikz after the construction of the array.

\documentclass{article}
\usepackage{nicematrix,tikz}
\usepackage{booktabs}

\begin{document}

\begin{NiceTabular}{lll} \toprule This is a & nice table & with three columns \ Some & row content & in the second row \ \bottomrule \CodeAfter \tikz \draw [dashed, shorten < = 4pt, shorten > = 4pt] (2-|2) -- (2-|4) ; \end{NiceTabular}

\end{document}

Output of the above code

F. Pantigny
  • 40,250