I am trying to define a command that simplifies building tables. I want the last entry to be automatically preceded by a horizontal line. I tried to do a minimal working example, and it worked (surprisingly), but it does not work in my more elaborate code.
\documentclass{article}
\usepackage{xparse}
%%% Works
\newcommand{\foo}[2]{#1 & #2\\}
\newcommand{\foolast}[2]{\hline
#1 & #2}
\newcounter{premn}
\setcounter{premn}{0}
%%% Does not work if \hline is present
\newcommand{\premform}{\arabic}
\NewDocumentCommand{\preml}{O{\premform{premn}} m o}{%
\IfNoValueTF{#3}%
{%
\refstepcounter{premn}%
(#1) & #2 & \\
}
{%
\refstepcounter{premn}%
(#1) & #2 & #3\\
}
}
\NewDocumentCommand{\conl}{O{\premform{premn}} m o}{%
\IfNoValueTF{#3}%
{%\hline
\refstepcounter{premn}%
(#1) & #2 & %
\setcounter{premn}{0}%
\ignorespaces%
}
{%\hline
\refstepcounter{premn}%
(#1) & #2 & #3 %
\setcounter{premn}{0}%
\ignorespaces%
}
}
%%% MWE
\begin{document}
\begin{tabular}{ll}
\foo{a}{b}
\foolast{a}{b}
\end{tabular}
\begin{tabular}{lll}
\preml{a}
\conl{b}
\end{tabular}
\end{document}
I am sure there is an answer to this, but it is beyond my knowledge.