Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm working on my ctable LaTeX package, planning to allow for optional key=value arguments for \FL, \ML, \LL, such that one can make a 3pt thick, red-colored toprule with \FL[w=3pt,c=red]; also, vertical rules should extend, unlike in the package booktabs, currently used by ctable, where get broken.

My current problem is that I try to use \@ifnextchar to detect the optional argument.
My test below starts with a little demo that shows that \@ifnextchar hands over \BB's optional argument to \AA.
But the same construct in my \FL hands over nothing, and the optional argument is printed as text, including the brackets. Could someone help me out please?
Here is my test:

\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}
share|improve this question

1 Answer 1

up vote 6 down vote accepted

The \@ifnextchar will never see the [ after \FL because it will always see the closing brace of the \noalign. You need to open the \noalign but close it after detecting any optional arguments.

See

 \noalign{\ifnum0=`}\fi

in \hline which uses the special brace trick for exactly this reason.

Also you want to move your % comments back next to the } as you have lots of spurious space tokens in your definition from the space before the comments.

enter image description here

\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}
share|improve this answer
    
thank you very much! –  Wybo Dekker Aug 12 '12 at 13:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.