\newcommand for tabular entries using keyval
Here is how it works:
\documentclass{article}
\usepackage{keyval}
\makeatletter
\define@key{tabularEntryKeys}{a}[\dots]{\gdef\A{#1}}
\define@key{tabularEntryKeys}{b}[\dots]{\gdef\B{#1}}
\makeatother
\newcommand\tabularEntry[1]{\setkeys{tabularEntryKeys}{#1} \A & \B}
\begin{document}
\begin{tabular}{c|c}\hline
\tabularEntry{a=1,b=2}
\end{tabular}
\end{document}
Here is one more approach. It does neither global definitions nor use of e-TeX
extensions and \edef
.
\documentclass{article}
\usepackage{keyval}
\def\TAB{&}
\makeatletter
\define@key{tabularEntryKeys}{a}[\dots]{\def\tek@rowA{#1}}
\define@key{tabularEntryKeys}{b}[\dots]{\def\tek@rowB{#1}}
% initialize
\def\tek@rowA{}
\def\tek@rowB{}
\newcommand{\tabularEntry}[1]{\setkeys{tabularEntryKeys}{#1}%
\tek@rowA \expandafter\TAB\tek@rowB }
\makeatother
\begin{document}
\begin{tabular}{c|c}
\hline
\tabularEntry{a=1,b=2} \\
\tabularEntry{a=1} \\
\tabularEntry{b=2} \\
\tabularEntry{a,b} \\
\tabularEntry{a} \\
\tabularEntry{b} \\
\tabularEntry{} \\
\end{tabular}
% \begin{tabular}{c|c}\hline
% \tabularEntry{a=1,b=2}\\
% \tabularEntry{a=3,b=7}\\
% \tabularEntry{a=10,b=20}
% \end{tabular}
\end{document}
In case of more cells in one row, one needs a variant to avoid many many \expandafter
's: (notice the extra spaces added before #1
and in the initial defaults values except for \tek@rowA
and \tekrowD
)
\usepackage{keyval}
\def\TAB{&}
\makeatletter
\define@key{tabularEntryKeys}{a}[\dots]{\def\tek@rowA{#1}}
\define@key{tabularEntryKeys}{b}[\dots]{\def\tek@rowB{ #1}}
\define@key{tabularEntryKeys}{c}[\dots]{\def\tek@rowC{ #1}}
\define@key{tabularEntryKeys}{d}[\dots]{\def\tek@rowD{#1}}
% initialize
\def\tek@rowA{}
\def\tek@rowB{ }
\def\tek@rowC{ }
\def\tek@rowD{}
\newcommand{\tabularEntry}[1]{\setkeys{tabularEntryKeys}{#1}%
\tek@rowA \expandafter\TAB
\romannumeral0\expandafter\tek@rowB\expandafter\TAB
\romannumeral0\expandafter\tek@rowC\expandafter\TAB
\tek@rowD
}
\makeatother
Your \setkeys
command can't straddle two cells. On the other hand globally defining \A
and \B
doesn't seem a good idea.
\documentclass{article}
\usepackage{keyval,etoolbox}
\makeatletter
\define@key{tabularEntryKeys}{a}[\dots]{\def\tek@rowA{\unexpanded{#1}}}
\define@key{tabularEntryKeys}{b}[\dots]{\def\tek@rowB{\unexpanded{#1}}}
% initialize
\def\tek@rowA{}
\def\tek@rowB{}
\newcommand\tabularEntry[1]{%
\setkeys{tabularEntryKeys}{#1}%
\edef\tek@row{\tek@rowA & \tek@rowB}\tek@row
}
\makeatother
\begin{document}
\begin{tabular}{c|c}
\hline
\tabularEntry{a=1,b=2} \\
\tabularEntry{a=1} \\
\tabularEntry{b=2} \\
\tabularEntry{a,b} \\
\tabularEntry{a} \\
\tabularEntry{b} \\
\tabularEntry{} \\
\end{tabular}
\end{document}
Note that the optional argument in \define@key
is the value assigned to the key when the key is specified without =<value>
following, not an initial value.
If you want the values to repeat if a key is not specified, then say
\define@key{tabularEntryKeys}{a}[\dots]{\gdef\tek@rowA{\unexpanded{#1}}}
\define@key{tabularEntryKeys}{b}[\dots]{\gdef\tek@rowB{\unexpanded{#1}}}
but don't use “easy” macro names such as \A
or \B
.