![在表格環境中擴充 \@ifnextchar 時出現問題](https://rvso.com/image/305898/%E5%9C%A8%E8%A1%A8%E6%A0%BC%E7%92%B0%E5%A2%83%E4%B8%AD%E6%93%B4%E5%85%85%20%5C%40ifnextchar%20%E6%99%82%E5%87%BA%E7%8F%BE%E5%95%8F%E9%A1%8C.png)
我想定義一個根據下一個字元擴展到表格環境\hline
並在表格環境內部擴展的巨集:\cline
\documentclass{standalone}
\makeatletter
\def\mycline[#1]{\cline{#1}}
\def\myline{\@ifnextchar[{\mycline}{\hline}} % Does not work. Either misplaced \noalign or \omit
\makeatother
\begin{document}
\begin{tabular}{cc}
a1 & a2 \\
\myline
b1 & b2 \\
\myline[1-2]
\end{tabular}
\end{document}
不幸的是,這會導致錯誤放置\noalign
或\omit
(或兩者)。似乎\@ifnextchar
沒有及時完全擴展。任何想法?
答案1
由於掃描的\noalign
工作方式,不可展開的命令(此處\futurelet
由\@ifnextchar
)啟動一個新的對齊單元格,並且插入行為時已晚。
您必須掃描\noalign
群組內的選用參數:
\documentclass{standalone}
\makeatletter
\def\myline{%
\noalign\bgroup
\@ifnextchar[%
{\aftergroup\mycline\egroup}%
{\aftergroup\hline\egroup}%
}
\def\mycline[#1]{\cline{#1}}
\makeatother
\begin{document}
\begin{tabular}{cc}
a1 & a2 \\
\myline
b1 & b2 \\
\myline[1-1]
c1 & c2
\end{tabular}
\end{document}
為什麼要找麻煩呢?;-)