In my earlier solution for Multivariate polynomial long division using LaTeX I had wrapped \cmidrule
in a macro as its syntax alowing to shorten the end points was not standard:
\newcommand*{\CMidRule}[3]{\cmidrule[\cmidrulewidth](l{#1}r{#2}){#3}}%
I wanted to use xparse
to to allow for the two shift parameters to be optional, but then ran into problems.
Using this only once per line works just fine as illustrated in the above linked question. However, an attempt to use this for two separate
\cmidrule
is problematic in that a vertical shift is produced between subsequent uses (as illustrated in the third table below).Furthermore my attempt to use
\NewDocumentCommand
from thexparse
package to allow for two optional parameters does not even compile (hence have commented out the line in the fourth table). The error message is:Misplaced \noalign. \cmidrule ->\noalign {\ifnum 0=`}\fi \@ifnextchar [{\@cmidrule }{\@cmidrule ... l.40 \CMidRuleX{0.0ex} {0.0ex}{1-1}\CMidRuleX{0.0ex}{0.0ex}{2-2}
References:
Question:
- How do I properly define
\NewDocumentCommand{\CMidRuleX}{O{0.0ex} O{0.0ex} m}{}
?
Code:
\documentclass{article}
\usepackage{booktabs}
\usepackage{xparse}
\newcommand*{\CMidRule}[3]{\cmidrule[\cmidrulewidth](l{#1}r{#2}){#3}}%
\NewDocumentCommand{\CMidRuleX}{%
O{0.0ex}% #1 = left adjust
O{0.0ex}% #1 = right adjust
m% #3 = columns to span
}{%
\cmidrule[\cmidrulewidth](l{#1}r{#2}){#3}%
}%
\begin{document}
\section*{Direct use of cmidrule works just fine}
\begin{tabular}{rc}
Left & Right \\
\cmidrule{1-1}\cmidrule{2-2}
2 & 3 \\
\end{tabular}
\hspace{1.0cm}
\begin{tabular}{rc}
Left & Right \\
\cmidrule[\cmidrulewidth](l{0.0ex}r{0.5ex}){1-1}\cmidrule[\cmidrulewidth](l{0.5ex}r{0.0ex}){2-2}
2 & 3 \\
\end{tabular}
%
\bigskip
\section*{Wrapping cdmidrule in a macro does not work}
\begin{tabular}{rc}
Left & Right \\
\CMidRule{0.0ex}{0.5ex}{1-1}\CMidRule{0.5ex}{0.0ex}{2-2}
2 & 3 \\
\end{tabular}
\hspace{1.0cm}
\begin{tabular}{rc}
Left & Right \\
%\CMidRuleX[0.0ex][0.0ex]{1-1}\CMidRuleX[0.0ex][0.0ex]{2-2}
2 & 3 \\
\end{tabular}
\end{document}
\def\test{\cmidrule[\cmidrulewidth](l{0.5ex}r{0.0ex}){2-2}}
). This will fail to line up, even though it 'should', as the first\cmidrule
does not 'look' for a hidden following one.