我正在开发我的ctable
LaTeX 包,计划允许、、key=value
的可选参数,这样就可以用 制作一个粗的红色顶部规则;此外,垂直规则应该可以扩展,不像当前使用的包 ,那里会被破坏。\FL
\ML
\LL
3pt
\FL[w=3pt,c=red]
booktabs
ctable
我目前的问题是我尝试使用 来\@ifnextchar
检测可选参数。
下面的测试从一个小演示开始,该演示显示将的可选参数\@ifnextchar
移交给。 但同样的构造在我手中什么也没有,并且可选参数被打印为文本,包括括号。有人能帮帮我吗? 这是我的测试:\BB
\AA
\FL
\documentclass[a5paper]{article}
\usepackage{charter,xkeyval,colortbl}
\pagestyle{empty}\parindent0pt
\makeatletter
% for a demo of \@ifnextchar
% If \BB see an optional argument, it calls \AA with it.
% With no optional argument, \AA[default] is called.
\def\AA[#1]{===#1===}
\def\BB{\@ifnextchar[{\AA}{\AA[default]}}
\newlength\savedwidth
\define@key{rules}{w}{\global\savedwidth\arrayrulewidth\global\arrayrulewidth#1}
\define@key{rules}{c}{\gdef\rulecolor{#1}}
\def\XX[#1]{\setkeys{rules}{#1}}
\def\FL{%
\noalign{%
\setkeys{rules}{w=2pt,c=blue} % set the defaults
\@ifnextchar[{\XX}{} % optional arg? call \XX with it
}%
\arrayrulecolor{\rulecolor}%
\hline%
\noalign{\global\arrayrulewidth\savedwidth} % restore width
\arrayrulecolor{black} % and color
\rule{0pt}{3ex} % strut for extra whitespace under the rule
}
\begin{document}
% Here's the demo:
\BB[argument]\\\BB
\medskip
% now a table with a default toprule
\begin{tabular}{|r|l|}\FL
Hello World & Here I am\\
Using FL with & no arg
\end{tabular}
\medskip
% and a table with a thick red toprule
\begin{tabular}{|r|l|}\FL[c=red,w=4pt]
Hello World & Here I am\\
Using FL with & c=red
\end{tabular}
\end{document}
答案1
永远\@ifnextchar
不会看到[
后面的\FL
,因为它总是会看到的右括号\noalign
。您需要打开\noalign
但在检测到任何可选参数后将其关闭。
看
\noalign{\ifnum0=`}\fi
正是出于这个原因,其中\hline
使用了特殊的括号技巧。
另外,您还想将%
注释移回到旁边,}
因为您的定义中注释之前的空间中有很多虚假的空间标记。
\documentclass[a5paper]{article}
\usepackage{charter,xkeyval,colortbl}
\pagestyle{empty}\parindent0pt
\makeatletter
% for a demo of \@ifnextchar
% If \BB see an optional argument, it calls \AA with it.
% With no optional argument, \AA[default] is called.
\def\AA[#1]{===#1===}
\def\BB{\@ifnextchar[{\AA}{\AA[default]}}
\newlength\savedwidth
\define@key{rules}{w}{\global\savedwidth\arrayrulewidth\global\arrayrulewidth#1}
\define@key{rules}{c}{\gdef\rulecolor{#1}}
\def\XX[#1]{\setkeys{rules}{#1}\XXX}
\def\XXX{\ifnum0=`{\fi}%
\arrayrulecolor{\rulecolor}%
\hline%
\noalign{\global\arrayrulewidth\savedwidth}% restore width
\arrayrulecolor{black}% % and color
\rule{0pt}{3ex}% % strut for extra whitespace under the rule
}
\def\FL{%
\noalign{\ifnum0=`}\fi
\setkeys{rules}{w=2pt,c=blue}% % set the defaults
\@ifnextchar[{\XX}{\XXX} % % optional arg? call \XX with it
}
\begin{document}
% Here's the demo:
\BB[argument]\\\BB
\medskip
% now a table with a default toprule
\begin{tabular}{|r|l|}\FL
Hello World & Here I am\\
Using FL with & no arg
\end{tabular}
\medskip
% and a table with a thick red toprule
\begin{tabular}{|r|l|}\FL[c=red,w=4pt]
Hello World & Here I am\\
Using FL with & c=red
\end{tabular}
\end{document}