可选参数导致放错 \noalign

可选参数导致放错 \noalign

我有以下代码这个答案生成虚线表规则:

\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}

我想适应\cdashrule接受传递给它的可选参数(默认为空),\cdashline以便我也可以控制虚线的格式(线/间隙宽度)。但是,即使只是定义一个可选参数(而不是在任何地方使用它)也会导致上述示例抛出错误:

\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}}

我还设立了一个Overleaf 项目这证明了这个问题。如何正确定义\cdashrule命令,以便它将任何可选的格式参数传递给底层\cdashline命令?

答案1

(这其他答案更完整,发布时间稍早)

您可以使用最新的 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}

带虚线的表格

但我觉得最好实现与原始语法相同的语法\cdashline,以下代码可以实现这一点:

\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}

与之前相同的输出。

答案2

这与可扩展性有关。

有关类似问题的一些解释请参阅以下问题:

放错了 \omit。\multispan 和 \newcommand 定义了可选参数

或以下之一1 2 3 4 5 6

一个(相对容易的)解决方法是使用下面的代码,尽管它有一定的限制,如xparse文档中所述:

xparse 文档

xparse 文档

\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}

相关内容