打算制作一组环境(共两个 Exercises,solution),代码如下:
\begin{Exercises}[<Options>]
\begin{question}
...
\end{question}
\begin{solution}
S1
\end{solution}
\begin{question}
...
\end{question}
\begin{solution}
S2
\end{solution}
\begin{question}
...
\end{question}
\begin{solution}
S3
\end{solution}
\begin{question}
...
\end{question}
\begin{solution}
S4
\end{solution}
\end{Exercises}具体需求如下:
Exercises 环境中,除 solution 环境内的内容,其余内容均不受影响。Exercises 没有给出可选参数,那么显示所有的 solution 环境。Exercises 中 [<Options>] 为 1,4 ,则仅显示第一个和第四个的 solution 环境。若 Exercises 中 [<Options>] 为 2,3,4 ,则仅显示第二个、第三个和第四个的 solution 环境。其余内容类似。\documentclass{article}
\usepackage[tt=false]{libertine}
\usepackage{framed}
\usepackage{lipsum}
\newcounter{question}
\newcounter{solution}
\ExplSyntaxOn
\clist_new:N \g_ljh_answer_clist
\cs_generate_variant:Nn \clist_if_in:NnT { NeT }
% 辅助宏 from: https://mp.weixin.qq.com/s/Mc3QawPiD6WnREZejFxigg
\cs_set_nopar:Npn \__module_range_to_list:nN #1#2 {
\seq_set_split:Nnn \l__module_tmpa_seq { , } { #1 }
\seq_map_inline:Nn \l__module_tmpa_seq
{
\tl_if_in:nnTF { ##1 }{ - }
{
\seq_set_split:Nnn \l__module_tmpb_seq { - } { ##1 }
\int_step_inline:nnn
{ \seq_item:Nn \l__module_tmpb_seq { 1 } }
{ \seq_item:Nn \l__module_tmpb_seq { 2 } }
{
\clist_put_right:Nn #2 { ####1 }
}
}
{
\clist_put_right:Nn #2 { ##1 }
}
}
}
\NewDocumentEnvironment{question}{+b}{
\refstepcounter{question}
\begin{leftbar}
\noindent \textsf{Question\ \thequestion :}\quad {#1}
\end{leftbar}
}{}
\NewDocumentEnvironment{solution}{+b}{
\refstepcounter{solution}
% why not \clist_if_in:NVT(?)
\clist_if_in:NeT \g_ljh_answer_clist {\thesolution}
{
\begin{leftbar}
\noindent \textsf{Solution\ \thesolution :}\quad {#1}
\end{leftbar}
}
}{}
\NewDocumentEnvironment{Exercises}{ o +b }{
\group_begin:
\setcounter{question}{0}
\setcounter{solution}{0}
\clist_gclear:N \g_ljh_answer_clist
\IfNoValueTF{#1}
{%这里的逻辑没处理太好,不太容易获取这个Exercises一共有多少题目
%大胆一点...不过效率会不会变低呢(・∀・(・∀・(・∀・*)
% \int_step_inline:nn {99}{\clist_gput_right:Nn \g_ljh_answer_clist {##1}}
\__module_range_to_list:nN {1-99} \l_tmpa_clist
}
{
\__module_range_to_list:nN {#1} \l_tmpa_clist
}
\clist_gset_eq:NN \g_ljh_answer_clist \l_tmpa_clist
#2
\group_end:
}{}
\ExplSyntaxOff
\begin{document}
\begin{Exercises}[1,3-4]
\begin{question}
What is the meaning of life?
\end{question}
\begin{solution}
The meaning of life is a philosophical question regarding the significance of life or existence in general. Many people and cultures have different interpretations of this concept.
\end{solution}
\lipsum[1]
\begin{question}
What is the meaning of life?
\end{question}
\begin{solution}
The meaning of life is a philosophical question regarding the significance of life or existence in general. Many people and cultures have different interpretations of this concept.
\end{solution}
\begin{question}
What is the meaning of life?
\end{question}
\begin{solution}
The meaning of life is a philosophical question regarding the significance of life or existence in general. Many people and cultures have different interpretations of this concept.
\end{solution}
\begin{question}
What is the meaning of life?
\end{question}
\begin{solution}
The meaning of life is a philosophical question regarding the significance of life or existence in general. Many people and cultures have different interpretations of this concept.
\end{solution}
\end{Exercises}
\newpage
\begin{Exercises}
\begin{question}
What is the meaning of life?
\end{question}
\begin{solution}
The meaning of life is a philosophical question regarding the significance of life or existence in general. Many people and cultures have different interpretations of this concept.
\end{solution}
\lipsum[2]
\begin{question}
What is the meaning of life?
\end{question}
\begin{solution}
The meaning of life is a philosophical question regarding the significance of life or existence in general. Many people and cultures have different interpretations of this concept.
\end{solution}
\begin{question}
What is the meaning of life?
\end{question}
\begin{solution}
The meaning of life is a philosophical question regarding the significance of life or existence in general. Many people and cultures have different interpretations of this concept.
\end{solution}
\end{Exercises}
\end{document}