编译环境的一部分并收集主体

编译环境的一部分并收集主体

环境中有一个问题和一个答案。我排版了问题。然后排版了问题和答案。我该怎么做。我的文件有 100 多个问题。

\documentclass[12pt]{article}
\newcounter{exc}
\newenvironment{ex}{\refstepcounter{exc}\par\noindent\textbf{Ex \theexc.}}{\ignorespacesafterend}
\newcommand{\loigiai}[1]{}
\begin{document}
  \begin{ex}
  Content of the question 1
  \loigiai{Answer of the question 1}
  \end{ex}
  \begin{ex}
    Content of the question 1
    \loigiai{Answer of the question 1}
   \end{ex}
%\showanswer collect the environment's bodies and inpunt as below
\begin{center}
\textbf{Answer}
\end{center}
\setcounter{exc}{0}
\renewcommand{\loigiai}[1]{\par \textit{Answer} #1}
  \begin{ex}
  Content of the question 1
  \loigiai{Answer of the question 1}
  \end{ex}
  \begin{ex}
    Content of the question 1
    \loigiai{Answer of the question 1}
   \end{ex}
\end{document}

#更新 6/8/2021

在 tokcycle 包的帮助下,问题基本解决了。ex 中有一个选项时,会出现非法定义中的参数个数的错误。

\documentclass[12pt]{article}
\usepackage{tokcycle}
\usepackage{tcolorbox}
\usepackage{ifthen}
\newcounter{exc}
\newenvironment{ex}[1][]{\refstepcounter{exc}\par\noindent\textbf{Ex \theexc.} }{\ignorespacesafterend}
\newcommand{\loigiainul}[1]{}
\newcommand{\eloigiai}{}
\newcommand{\loigiaifull}[1]{\gdef\eloigiai{\par \textit{Answer} #1}}
\xtokcycleenvironment\Tracnghiem
  {\addcytoks{##1}}
  {\processtoks{##1}}
  {\addcytoks{##1}}
  {\addcytoks{##1}}
  {\let\loigiai\loigiainul}
  {\tcafterenv{%
    \begin{center}\textbf{Answer}\end{center}
    \setcounter{exc}{0}
    \let\loigiai\loigiaifull
\renewenvironment{ex}[1][]{\refstepcounter{exc}\par\noindent
    \begin{tcolorbox}
    \ifthenelse{\equal{#1}{}}{\textbf{Ex \theexc.}}
    {\textbf{Ex \theexc (\textit{#1}).}}
}
{\end{tcolorbox}\ignorespacesafterend\eloigiai}    
    \the\cytoks}%
  }
\begin{document}
\Tracnghiem
  \begin{ex}
  Content of the question 1
  \loigiai{Answer of the question 1}
  \end{ex}
  \begin{ex}
    Content of the question 2
    \loigiai{Answer of the question 2}
   \end{ex}
\endTracnghiem
\end{document}

答案1

在这里,我使用扩展的标记循环来收集\Exercise伪环境标记(进入标记列表\cytoks)并使用适当的输出设置两个朗诵。

事实上,这些标记被循环一次并被收集起来。然后它们被输出两次,第二次输出的是修订后的定义\loigiai

\documentclass[12pt]{article}
\usepackage{tokcycle}
\newcounter{exc}
\newenvironment{ex}{\refstepcounter{exc}\par\noindent\textbf{Ex \theexc.}}{\ignorespacesafterend}
\newcommand{\loigiainul}[1]{}
\newcommand{\loigiaifull}[1]{\par \textit{Answer} #1}
\xtokcycleenvironment\Exercises
  {\addcytoks{##1}}
  {\processtoks{##1}}
  {\addcytoks{##1}}
  {\addcytoks{##1}}
  {\let\loigiai\loigiainul}
  {\tcafterenv{%
    \begin{center}\textbf{Answer}\end{center}
    \setcounter{exc}{0}
    \let\loigiai\loigiaifull
    \the\cytoks}%
  }
\begin{document}
\Exercises
  \begin{ex}
  Content of the question 1
  \loigiai{Answer of the question 1}
  \end{ex}
  \begin{ex}
    Content of the question 2
    \loigiai{Answer of the question 2}
   \end{ex}
\endExercises
\end{document}

在此处输入图片描述


附录

对已编辑问题的回答

#1在已编辑的问题中,在 tokcycle 环境中使用\Tracnghiem是不正确的,因为 OP 希望#1引用ex环境的参数。将##1其推入 的参数中\@aftertokcycle...仍然不够好。 ####1使其成为 的参数\tcendgroup。最后,根据需要,########1使其成为 的参数ex

\documentclass[12pt]{article}
\usepackage{tokcycle}
\usepackage{tcolorbox}
\usepackage{ifthen}
\newcounter{exc}
\newenvironment{ex}[1][]{\refstepcounter{exc}\par\noindent\textbf{Ex \theexc.} }{\ignorespacesafterend}
\newcommand{\loigiainul}[1]{}
\newcommand{\eloigiai}{}
\newcommand{\loigiaifull}[1]{\gdef\eloigiai{\par \textit{Answer} #1}}
\xtokcycleenvironment\Tracnghiem
  {\addcytoks{##1}}
  {\processtoks{##1}}
  {\addcytoks{##1}}
  {\addcytoks{##1}}
  {\let\loigiai\loigiainul}
  {\tcafterenv{%
    \begin{center}\textbf{Answer}\end{center}
    \setcounter{exc}{0}
    \let\loigiai\loigiaifull
\renewenvironment{ex}[1][]{\refstepcounter{exc}\par\noindent
    \begin{tcolorbox}
    \ifthenelse{\equal{########1}{}}{\textbf{Ex \theexc.}}
    {\textbf{Ex \theexc (\textit{########1}).}}
}
{\end{tcolorbox}\ignorespacesafterend\eloigiai}    
    \the\cytoks}%
  }
\begin{document}
\Tracnghiem
  \begin{ex}[Hi Mom]%
  Content of the question 1
  \loigiai{Answer of the question 1}
  \end{ex}
  \begin{ex}
    Content of the question 2
    \loigiai{Answer of the question 2}
   \end{ex}
\endTracnghiem
\end{document}

在此处输入图片描述

但是,我可能不完全理解 OP 的用例,但可能不需要ex在 tokcycle 环境中重新定义。在这种情况下,人们可以避免所有的####...胡言乱语,也许这就足够了:

\documentclass[12pt]{article}
\usepackage{tokcycle}
\usepackage{tcolorbox}
\usepackage{ifthen}
\newcounter{exc}
\newcommand{\loigiainul}[1]{}
\newcommand{\eloigiai}{}
\newcommand{\loigiaifull}[1]{\gdef\eloigiai{\par \textit{Answer} #1}}
\xtokcycleenvironment\Tracnghiem
  {\addcytoks{##1}}
  {\processtoks{##1}}
  {\addcytoks{##1}}
  {\addcytoks{##1}}
  {\let\loigiai\loigiainul}
  {\tcafterenv{%
    \begin{center}\textbf{Answer}\end{center}
    \setcounter{exc}{0}
    \let\loigiai\loigiaifull
    \the\cytoks}%
  }
\newenvironment{ex}[1][]{\refstepcounter{exc}\par\noindent
    \begin{tcolorbox}
    \ifthenelse{\equal{#1}{}}{\textbf{Ex \theexc.}}
    {\textbf{Ex \theexc (\textit{#1}).}}
}
{\end{tcolorbox}\ignorespacesafterend\eloigiai}    

\begin{document}
\Tracnghiem
  \begin{ex}[Hi Mom]%
  Content of the question 1
  \loigiai{Answer of the question 1}
  \end{ex}
  \begin{ex}
    Content of the question 2
    \loigiai{Answer of the question 2}
   \end{ex}
\endTracnghiem
\end{document}

相关内容