1

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:

enter image description here

\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
% #2 - answer count
% #3 - table design
\newenvironment{questionGroup}[3]{
\noindent\QuestionID{}\textbf{#1}\\
\setlength{\tabcolsep}{2pt}
\setcounter{subQuestionID}{0}
\newcounter{answerCount}
\setcounter{answerCount}{#2}
\begin{tabular}{#3}
\hline
}{\end{tabular}\\}
\newcommand{\groupQuestion}[1]{
\subQuestionID{}.~#1&
\foreach \n in {1,...,\value{answerCount}}{
  \n 
}
\\\hline
}
\newcounter{QuestionID}
\newcommand{\QuestionID}{\addtocounter{QuestionID}{1}\textit{\arabic{QuestionID}.}}
\newcounter{subQuestionID}
\newcommand{\subQuestionID}{\addtocounter{subQuestionID}{1}{\textit{\alph{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

2 Answers 2

1

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

\documentclass{article}
\usepackage{forloop}
\newcounter{QuestionID}
\newcommand{\QuestionID}{\addtocounter{QuestionID}{1}\textit{\arabic{QuestionID}.}}
\newcounter{subQuestionID}
\newcommand{\subQuestionID}{\addtocounter{subQuestionID}{1}{\textit{\alph{subQuestionID}}}}
\newcounter{answerCount}
\newcounter{answerID}
\newcommand{\answerID}{\addtocounter{answerID}{1}{\textit{\arabic{answerID}}}}
\newcommand{\answerIDn}{\textit{\arabic{answerID}}}
\newenvironment{questionGroup}[3]{
    \noindent\QuestionID{}~\textbf{#1}
    \setlength{\tabcolsep}{1pt}
    \setcounter{subQuestionID}{0}
    \setcounter{answerCount}{#2}
    \\\noindent
    \begin{tabular}{#3}
    \hline
}
{\end{tabular}}
\newcommand{\groupQuestion}[1]{
    \small\subQuestionID{}.~#1
    \setcounter{answerID}{0}
    \forLoop{1}{\value{answerCount}}{answerID}{
        &\answerIDn{}}
    \\\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.

0

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}}}}

enter image description here

\documentclass{article}
% #1 - question group title
% #2 - answer count
% #3 - table design
\newcounter{answerCount}
\newenvironment{questionGroup}[3]{%
    \noindent\QuestionID~\textbf{#1}\par%
    \setlength{\tabcolsep}{2pt}%
    \setcounter{subQuestionID}{0}%
    \setcounter{answerCount}{#2}%
    \tabular{#3}
    \hline
}{\endtabular}
\newcommand{\groupQuestion}[4]{\subQuestionID{}.~#1&#2&#3&#4\\\hline}
\newcounter{QuestionID}
\newcommand{\QuestionID}{\addtocounter{QuestionID}{1}\textit{\arabic{QuestionID}.}}
\newcounter{subQuestionID}
\newcommand{\subQuestionID}{\addtocounter{subQuestionID}{1}{\textit{\alph{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}
5
  • 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 Sep 14, 2011 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"? Sep 14, 2011 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. Sep 14, 2011 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? Sep 14, 2011 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). Sep 14, 2011 at 20:43

You must log in to answer this question.

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