# New document environment and the tabularx package

I am trying to create a new environment but it gives me some trouble. The compiler keeps telling me that I am missing a } but I don't see where it could be. Here is a code sample. Thanks for your help.

\documentclass{article}
\usepackage{multirow, tabularx}
\usepackage{xparse}

\NewDocumentEnvironment{afskriftFolketaelling}{mm}
{%
\tabularx{\textwidth}{|X|l|c|c|l|l|l|}
\hline
\multicolumn{7}{|c|}{\textbf{#1}}\\
\multicolumn{7}{|c|}{#2}\\\hline
\textbf{title} & \textbf{title} & \textbf{title} & \textbf{title} & \textbf{title} & \textbf{title} & \textbf{title}\\\hline
}
{
\endtabularx
}
\begin{document}
\begin{afskriftFolketaelling}{test1}{test2}
{
1&2&3&4&5&6&7\\\hline
}
\end{afskriftFolketaelling}
\end{document}


Your environment takes two mandatory arguments, and you additionally specify the entire environment contents as being inside a group of its own. This is not a valid syntax within a tabular (or tabularx) as the group now spans multiple cells. That is, you open the group in one cell, and then close it in another, which is incorrect. The "missing }" error relates to the fact that the first group opened hasn't been properly closed.

The solution is to remove the group inside your environment:

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

\NewDocumentEnvironment{afskriftFolketaelling}{ m m }
{%
\noindent\tabularx{\textwidth}{ | X | l | c | c | l | l | l | }
\hline
\multicolumn{7}{|c|}{\textbf{#1}} \\
\multicolumn{7}{|c|}{#2} \\ \hline
\textbf{title} & \textbf{title} & \textbf{title} & \textbf{title} & \textbf{title} & \textbf{title} & \textbf{title} \\ \hline
}
{%
\endtabularx
}
\begin{document}

\begin{afskriftFolketaelling}{test1}{test2}
1 & 2 & 3 & 4 & 5 & 6 & 7 \\ \hline
\end{afskriftFolketaelling}

\end{document}