0

I want to define a command to abstract a complexe table environment with vairable rows. Here is a simple demo:

\documentclass{article}%
\usepackage{tabularx}%

\begin{document}
\begin{tabularx}{\textwidth}{|X|X|}\hline
name 1 & description 1\\
\hline
name 2 & description 2\\
\hline
\end{tabularx}
\end{document}

According the answer of @egreg in this question: New command with variable number of parameters

I define a macros like that in the answer https://tex.stackexchange.com/a/72915 :

\makeatletter %
\newcommand{\members}{
\tabularx{\textwidth}{|X|X|} \hline
\@stepone}
\newcommand\@stepone{\@ifnextchar\stopmembers{\@membersend}{\@steptwo}}
\newcommand\@steptwo[2]{
#1 & #2 \\ \hline
\@stepone}
\newcommand\@membersend[1]{
\endtabularx
}
\makeatother

I want to use it like:

\begin{document}
\members
  {name 1}{description 1}
  {name 2}{description 2}
\stopmembers
\end{document}

But, something wrong when compile:

Misplaced \crcr. \end{document}
Extra }, or forgotten $. \end{document}
Extra }, or forgotten $. \end{document}
Undefined control sequence. \end{document}
Package array Error: Illegal pream-token (\AtEndOfPackage): `c' used. \end{document}
Extra }, or forgotten $. \end{document}
Missing $ inserted. \end{document}

Someone can help me? Thanks avance!

1
  • A better solution might be to put your data in a .csv and then simply use something like datatool or csvsimple. You likely find such a solution easier in virtually every respect.
    – jon
    Dec 3, 2016 at 15:05

2 Answers 2

0

I wouldn't use foreign markup such as \members...\stopmembers.

I'm also not sure why

{name 1}{description 1}
{name 2}{description 2}

would be easier than

\member{name 1}{description 1}
\member{name 2}{description 2}

Anyway, the problem is that tabularx is quite fragile.

\documentclass{article}
\usepackage{tabularx,environ}

\makeatletter
\NewEnviron{members}{%
  \par\noindent
  \toks@={}%
  \expandafter\start@member\BODY\start@member
  \edef\BODY{\noexpand\begin{tabularx}{\textwidth}{|l|X|}
  \noexpand\hline
  \the\toks@
  \noexpand\end{tabularx}}%
  \BODY
}

\newcommand\start@member{\@ifnextchar\start@member{\@gobble}{\@member}}
\newcommand{\@member}[2]{%
  \toks@=\expandafter{\the\toks@ #1 & #2 \\ \hline}%
  \start@member
}
\makeatother

\begin{document}

\begin{members}
  {name 1}{description 1}
  {name 2}{description 2}
\end{members}

\end{document}

enter image description here

0

I think this does what you want:

\documentclass{article}
\usepackage{tabularx}
\makeatletter 
\newcommand{\members}{\def\@body{}\@stepone}
\newcommand\@stepone{\@ifnextchar\stopmembers{}{\@steptwo}}
\newcommand\@steptwo[2]%
   {\expandafter\def\expandafter\@body\expandafter{\@body#1 & #2 \\ \hline}%
    \@stepone
   }
\newcommand\stopmembers
   {\begin{tabularx}{\textwidth}{|X|X|} \hline
    \@body
    \end{tabularx}%
   }
\makeatother

\begin{document}
\noindent
\members
  {name 1}{description 1}
  {name 2}{description 2}
\stopmembers
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .