具有可自定义表列数量的环境

具有可自定义表列数量的环境

我想制作宏来绘制具有相同答案的问题组,并带有可自定义的文本、答案数量和设计(列宽、对齐方式等),如下所示:

在此处输入图片描述

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

生成的行中的列数是动态的,并且除第一个之外的所有单元格中的数字也会生成。

我所能做的最好的工作是:

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

为了完成此任务我需要以下内容:

  • [DONE]在命令内部传递一些答案(环境参数) ;\groupQuestion{}
  • [完成]在单独的列中打印每个问题的每个答案的递增数字,从 1 到给出的环境参数的数字;
  • 输出表格列分隔符\foreach- 目前它在插入&之前返回错误\n

答案1

过了一段时间我通过以下代码解决了这个问题:

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

希望有人可以好好利用它。

答案2

我不能说我完全理解你的问题。但这里有一个可以编译的版本,应该可以帮助你入门。你有代码来为问题生成字母,所以我保留了它,尽管你的图片显示了数字。如果你想在那里显示数字,请使用:

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

在此处输入图片描述

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

相关内容