# Environment with customizable number of table columns

I want to make macros for drawing group of questions with same answers with customizable text, number of answers and design (column widths, alignment, etc.), like this:

\begin{questionGroup}{Question group description}{3}{|p{100}|c|c|c|}
Questions& yes& no& maybe\\\hline
\groupQuestion{Question1}
\groupQuestion{Question2}
\end{questionGroup}


The number of columns in generated rows is dynamical and numbers in all cells except first are also generated.

The best I had managed to make work is:

\documentclass{article}
\usepackage{tikz}
% #1 - question group title
% #3 - table design
\newenvironment{questionGroup}[3]{
\noindent\QuestionID{}\textbf{#1}\\
\setlength{\tabcolsep}{2pt}
\setcounter{subQuestionID}{0}
\begin{tabular}{#3}
\hline
}{\end{tabular}\\}
\newcommand{\groupQuestion}[1]{
\subQuestionID{}.~#1&
\n
}
\\\hline
}
\newcounter{QuestionID}
\newcounter{subQuestionID}

\begin{document}
\begin{questionGroup}{Question group description}{3}{|p{100}|c|c|c|}
Questions& yes& no& maybe\\\hline
\groupQuestion{Question1}
\groupQuestion{Question2}
\end{questionGroup}
\end{document}


To complete this I need the following:

• [DONE] pass a number of answers (parameter of the environment) inside \groupQuestion{} command;
• [DONE] print incrementing numbers for each answer of each question in separate column from 1 to number given as environment parameter;
• output table column separators from \foreach - currently it returns error after inserting & before \n
-

After some time I had resolved this question by the following code:

\documentclass{article}
\usepackage{forloop}
\newcounter{QuestionID}
\newcounter{subQuestionID}
\newenvironment{questionGroup}[3]{
\noindent\QuestionID{}~\textbf{#1}
\setlength{\tabcolsep}{1pt}
\setcounter{subQuestionID}{0}
\\\noindent
\begin{tabular}{#3}
\hline
}
{\end{tabular}}
\newcommand{\groupQuestion}[1]{
\small\subQuestionID{}.~#1
\\\hline
}
\begin{document}
\begin{questionGroup}{Question group description}{3}{|p{50mm}|c|c|c|}
Questions& yes& no& maybe\\\hline
\groupQuestion{Question1}
\groupQuestion{Question2}
\end{questionGroup}
\end{document}


Hope someone could make a good use of it.

-

Can't say that I fully understand your question. But here is a version that compiles and should get you started. You had code to produce a letter for the questions so I left that, even though your image had shown numbers. If you want numbers there instead use:

\newcommand{\subQuestionID}{\addtocounter{subQuestionID}{1}{\textit{\arabic{subQuestionID}}}}


\documentclass{article}
% #1 - question group title
% #3 - table design
\newenvironment{questionGroup}[3]{%
\noindent\QuestionID~\textbf{#1}\par%
\setlength{\tabcolsep}{2pt}%
\setcounter{subQuestionID}{0}%
\tabular{#3}
\hline
}{\endtabular}
\newcommand{\groupQuestion}[4]{\subQuestionID{}.~#1&#2&#3&#4\\\hline}
\newcounter{QuestionID}
\newcounter{subQuestionID}

\begin{document}
\begin{questionGroup}{Question group description}{3}{|p{2cm}|c|c|c|}
Questions& Yes& No& Maybe\\\hline
\groupQuestion{Question1}{1}{2}{3}
\groupQuestion{Question2}{1}{2}{3}
\end{questionGroup}
\end{document}

-
I meant that numbers should be generated. So for example you have a group with 10 questions with 5 answers and a group of 5 questions with 7 answers. It is not handy to write every number or even copy it. This macros should work for all cases the same way. Updated question –  Yuriy Petrovskiy Sep 14 '11 at 20:20
@Yuri: Sorry, am not understanding what you want. What is a "group"? What does it mean for "10 questions with 5 answers"? –  Peter Grill Sep 14 '11 at 20:24
At the example in the question it is a group of 2 questions with 3 same answers for each of them. This macros should work for any number any number of questions with the same number answers. –  Yuriy Petrovskiy Sep 14 '11 at 20:32
So, based on your edits, this solves the first two points, but not the third? How many maximum number of columns do you want to be able to handle? If it is arbitrarily large then it'll be difficult. Also, it seems that they way you have set up the \foreach that each row will have the same data? –  Peter Grill Sep 14 '11 at 20:37
I think it should be from 2 (yes/no) to 11 (visual analog scale). It is no likely to be more answers in the line (at least it will be hard to read it then). –  Yuriy Petrovskiy Sep 14 '11 at 20:43