将附加变量传递到宏中

将附加变量传递到宏中

这是一个来自在文档的不同部分打印 \newcommand 中的参数

在得到大力帮助后,我成功地让该问题的 MWE 发挥作用。我以为将解决方案扩展到我的实际问题会很简单,但看起来并不是那么简单。

对于每个问题,都有一个零件编号,这是要传递到 \printQA 的附加参数。目前无法正确读取此参数 #2。

我当前更复杂的 MWE 看起来是这样的,

\documentclass{article}
\usepackage{tabularx}
\usepackage{xifthen}% provides \isempty test
\usepackage{etoolbox}
\usepackage{enumitem}
\usepackage{comment}
\usepackage{environ}
\usepackage[margin=3cm]{geometry}
\linespread{1.7}
\setlength\parindent{0pt}

\newcounter{questionnumber}
\newcounter{questionpartnumber}[questionnumber]

\let\ea=\expandafter
\long\def\addto#1#2{\ea\def\ea#1\ea{#1#2}}

\def\answertext{}
\long\def\printQA#1#2#3{\par
   \ifthenelse{\isempty{#3}}{}{\hbox{\hbox to .07\hsize{\hss\bf#1(#2)\quad}\vtop{\hsize=0.9\hsize #3}}}
   \bigskip
}

\newcommand{\questiontext}[3]{
\stepcounter{questionnumber}
\ifthenelse{\isempty{#1}}{\noindent\textbf{\arabic{questionnumber}.}\\}
{\begin{tabularx}{\textwidth}{p{0.5cm} p{12.5cm} @{\extracolsep{\fill}}r}
\textbf{\arabic{questionnumber}.} & \noindent\parbox[t]{\hsize}{#1} & \ifx\empty#2\else[#2]\fi\\\\
\end{tabularx}}
\ea\addto\ea\answertext\ea{\ea\printQA\ea{\the\value{questionnumber}}{\the\value{questionpartnumber}}{#3}}
}

\newcommand{\questionpart}[3]{
\stepcounter{questionpartnumber}
\begin{tabularx}{\textwidth}{p{0.5cm} p{0.5cm} p{11cm} @{\extracolsep{\fill}}r}
& \textbf{\alph{questionpartnumber})} & \noindent\parbox[t]{\hsize}{#1} & \ifx\empty#2\else[#2]\fi\\\\
\end{tabularx}
\ea\addto\ea\answertext\ea{\ea\printQA\ea{\the\value{questionnumber}}{\the\value{questionpartnumber}}{#3}}
}

\newcommand{\citeschool}[3]{
\hspace*{1cm}[#1/#2/#3]\\
}

\NewEnviron{questionTopic}
{
\section*{Questions}
\BODY
\section*{Answers}
\answertext
}


\begin{document}

\begin{questionTopic}

\questiontext{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.}{}{}
\questionpart{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{6}{answer one}
\questionpart{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{5}{answer two}

% question 2
\questiontext{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.}{}{}
\questionpart{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{6}{answer three}

% question 3
\questiontext{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.}{6}{answer four}

\end{questionTopic}

\end{document}

我的输出看起来像这样 在此处输入图片描述 应该很清楚,当我尝试扩展 MWE 时,我无法输入问题部分编号。理想情况下,答案应该显示 1(a)、1(b)、2(a) 和 3。如果 #2 可以正确输入 \printQA,我认为这可以解决这个问题。

再次感谢您的帮助!

答案1

这是我的建议,特别是\questionprolog对于有部分的问题,因此无需检查空参数。

\documentclass{article}
\usepackage{etoolbox}

\newcounter{questionnumber}
\newcounter{questionpartnumber}[questionnumber]
\renewcommand{\thequestionpartnumber}{\alph{questionpartnumber}}

\newenvironment{questionTopic}
 {\section*{Questions}%
  \setlength{\parindent}{0pt}%
  \setlength{\rightskip}{2em}}
 {\par\setlength{\leftskip}{0pt}\setlength{\rightskip}{0pt}%
  \section*{Answers}\answertext}

\newcommand{\question}[3]{%
  \par\stepcounter{questionnumber}%
  \setlength{\leftskip}{2em}%
  \hspace{-2em}%
  \makebox[0pt][l]{\makebox[\columnwidth][r]{[#2]}}% the points
  \makebox[2em][l]{\bfseries\thequestionnumber.}#1\par
  \eappto{\answertext}{%
    \noexpand\textbf{\thequestionnumber}\unexpanded{ #3\par}%
  }%
}
\newcommand{\questionprolog}[1]{%
  \par\stepcounter{questionnumber}%
  \setlength{\leftskip}{2em}%
  \hspace{-2em}%
  \makebox[2em]{\bfseries\thequestionnumber.\quad}#1\par
}
\newcommand{\questionpart}[3]{%
  \par\stepcounter{questionpartnumber}%
  \setlength{\leftskip}{4em}%
  \hspace{-4em}%
  \makebox[0pt][l]{\makebox[\columnwidth][r]{[#2]}}% the points
  \makebox[4em][l]{\qquad\bfseries\thequestionpartnumber)}#1\par
  \eappto{\answertext}{%
    \noexpand\textbf{\thequestionnumber(\thequestionpartnumber)}\unexpanded{ #3\par}%
  }%
}

\begin{document}

\begin{questionTopic}

\questionprolog{Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris 
nisi ut aliquip ex ea commodo consequat.}
\questionpart{Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{6}{answer one}
\questionpart{Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{5}{answer two}

% question 2
\questionprolog{Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris 
nisi ut aliquip ex ea commodo consequat.}
\questionpart{Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{6}{answer three}

% question 3
\question{Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris 
nisi ut aliquip ex ea commodo consequat.}{6}{answer four}

\end{questionTopic}

\end{document}

在此处输入图片描述

相关内容