0

I have the following piece of code from this answer for generating a dashed table rule:

\documentclass{article}

\usepackage{booktabs}
\usepackage{arydshln}

\begin{document}

\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{\cdashrule}[2][]{%
    \noalign{\vskip\aboverulesep
        \global\let\@dashdrawstore\adl@draw
        \global\let\adl@draw\adl@drawiv}
    \cdashline{#2}
    \noalign{\global\let\adl@draw\@dashdrawstore
        \vskip\belowrulesep}}
\makeatother

\begin{table}
\begin{tabular}{lll}
A & B & C \\
\cdashrule{1-2}
D & E & F
\end{tabular}
\end{table}

\end{document}

I'd like to adapt \cdashrule to take an optional argument (empty by default) that it passes to \cdashline so that I can also control the formatting of the dashed line (line/gap width). However, even just defining an optional argument (and not using it anywhere) already causes the above example to throw errors:

\newcommand{\cdashrule}[2][]{%
    \noalign{\vskip\aboverulesep
        \global\let\@dashdrawstore\adl@draw
        \global\let\adl@draw\adl@drawiv}
    \cdashline{#2}
    \noalign{\global\let\adl@draw\@dashdrawstore
        \vskip\belowrulesep}}

I've also set up an Overleaf project that demonstrates this issue. How do I properly define the \cdashrule command so that it passes any optional formatting argumenets to the underlying \cdashline command?

1
  • as \cdashline original syntax is for optional argument after mandatory one, it would probably be better to do the same here.
    – user691586
    Commented Mar 28, 2023 at 12:31

2 Answers 2

1

(the other answer is more complete and was posted a bit earlier)

You can use the enhanced declarative interface of recent LaTeX.

\documentclass{article}

\usepackage{booktabs}
\usepackage{arydshln}

\begin{document}

\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}
\NewExpandableDocumentCommand\cdashrule{om}{%
    \noalign{\vskip\aboverulesep
        \global\let\@dashdrawstore\adl@draw
        \global\let\adl@draw\adl@drawiv}
    \IfNoValueTF{#1}{\cdashline{#2}}{\cdashline{#2}[#1]}%
    \noalign{\global\let\adl@draw\@dashdrawstore
        \vskip\belowrulesep}}
\makeatother

\begin{table}
\begin{tabular}{lll}
A & B & C \\
\cdashrule{1-2}
D & E & F \\
\cdashrule[.4pt/1pt]{1-2}
G & H & I
\end{tabular}
\end{table}

\end{document}

tabular with dashed lines

But I feel it would be better to implement the same syntax as original \cdashline and the following code does it:

\documentclass{article}

\usepackage{booktabs}
\usepackage{arydshln}

\begin{document}

\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\cdashrule[1]{%
    \noalign{\vskip\aboverulesep
        \global\let\@dashdrawstore\adl@draw
        \global\let\adl@draw\adl@drawiv\ifnum0=`}\fi
    \@ifnextchar[%]
       {\cdashrule@i[#1]}%
       {\cdashrule@i[#1][\dashlinedash/\dashlinegap]}%
}%
\def\cdashrule@i[#1][#2]{%
    \ifnum0=`{\fi}%
    \cdashline{#1}[#2]%
    \noalign{\global\let\adl@draw\@dashdrawstore
        \vskip\belowrulesep}}
\makeatother

\begin{table}
\begin{tabular}{lll}
A & B & C \\
\cdashrule{1-2}
D & E & F \\
\cdashrule{1-2}[.4pt/1pt]
G & H & I
\end{tabular}
\end{table}

\end{document}

Same output as before.

0
1

It has something to do with expandability.

Refer to the following question for some explanation on similar issue:

Misplaced \omit. \multispan with \newcommand defined with optional argument

or one of 1 2 3 4 5 6.

One (relatively easy) workaround is to use the following code, although it has certain limitations as documented in xparse's documentation:

xparse documentation

xparse documentation

\documentclass{article}

\usepackage{booktabs}
\usepackage{arydshln}

\begin{document}

\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}
\NewExpandableDocumentCommand{\cdashrule}{O{} m}{%
    \noalign{\vskip\aboverulesep
        \global\let\@dashdrawstore\adl@draw
        \global\let\adl@draw\adl@drawiv}
    \cdashline{#2}
    \noalign{\global\let\adl@draw\@dashdrawstore
        \vskip\belowrulesep}}
\makeatother

\begin{table}
\begin{tabular}{lll}
A & B & C \\
\cdashrule{1-2}
D & E & F
\end{tabular}
\end{table}

\end{document}

You must log in to answer this question.

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