循环枚举标签

循环枚举标签

我正在尝试制作一个科学练习模板,就像我们中学学生做这些练习时那样,在页边空白处使用关键字“给定”、“要求”和“解决方案”。几年前我经常使用 Latex,但后来忘记了很多。

首先,我想找到一种方法来自动循环这三个标签(每个问题只需要使用每个标签一次)。是否可以根据计数器从列表或数组中选择一个字符串,或者类似的东西?

其次,我不太喜欢下面构建的工作流程,需要在环境中创建环境等等。我想构建类似的东西

\begin{question}{3}
    Question Three in words
    \given Something automatically in math mode
    \asked Something automatically in math mode
    \sol (No bullet when only a single step)
        \step First step, math mode
        \step First step, math mode
\end{question}

这是我的代码。如你所见,到目前为止,我只得到了关键字“given:”

\documentclass[a4paper,12pt]{article}
\usepackage{enumitem}
\usepackage[marginparwidth=1in, reversemp]{geometry}

\newlist{exercise}{enumerate}{3}
\setlist[exercise]{wide = 0pt, listparindent=\parindent,labelsep = 0pt,leftmargin =\labelwidth}
\setlist[exercise, 1]{label =\llap{\textbf{Ex~\arabic*.}\hskip\marginparsep}}
\setlist[exercise, 2]{label = \itshape Given:, labelwidth = 3.5em, leftmargin =\labelwidth}
\setlist[exercise, 3]{label=$\bullet$,labelwidth = 1.333em, leftmargin =\labelwidth}
\let\cat\item
\let\step\item

\begin{document}    
\begin{exercise}
\item Calculate the volume of a cylinder with a radius of 2m and a height of 3m.
    \begin{exercise}
        \cat $r = 2 \qquad h = 3$
        \cat $V$
        \cat
        \begin{exercise}
            \step $A = 2 \pi r = 2 \cdot \pi \cdot 2 \Rightarrow A = 12.57 m^2$
            \step $V = A h = 12.57 \cdot 3 \Rightarrow V = 37.70 m^3$
        \end{exercise}
    \end{exercise}
\end{exercise}
\end{document}

在此处输入图片描述

答案1

该解决方案基于一些假设:

  1. 只有一个\asked项目
  2. 之后没有文字\sol
  3. 如果只有一个步骤,则不\step使用任何命令

这是代码,带有注释。

\errorcontextlines=100
\documentclass{article}
\usepackage{siunitx,enumitem,environ,expl3}

\newcounter{exercise}

\ExplSyntaxOn
\NewEnviron{exercise}
 {
  \refstepcounter{exercise}
  \begin{itemize}[label=\textbf{Ex.\@~\theexercise},leftmargin=*]
  \exercise_process:V \BODY
  \end{itemize}
 }
\cs_new_protected:Nn \exercise_process:n
 {
  % split the input at \sol
  \seq_set_split:Nnn \l_exercise_body_seq { \sol } { #1 }
  % get the part before \sol
  \seq_pop_left:NN \l_exercise_body_seq \l_exercise_body_tl
  % get the part after \sol
  \seq_pop_left:NN \l_exercise_body_seq \l_exercise_sol_tl
  % split the input at \asked
  \seq_set_split:NnV \l_exercise_body_seq { \asked } \l_exercise_body_tl
  % get the part before \asked
  \seq_pop_left:NN \l_exercise_body_seq \l_exercise_body_tl
  % get the asked part
  \seq_pop_left:NN \l_exercise_body_seq \l_exercise_asked_tl
  % split the input at \given
  \seq_set_split:NnV \l_exercise_body_seq { \given } \l_exercise_body_tl
  % get the part before \given
  \seq_pop_left:NN \l_exercise_body_seq \l_exercise_text_tl
  % save the \given part
  \seq_set_eq:NN \l_exercise_given_seq \l_exercise_body_seq

  %%% Now put the pieces together
  % issue the text
  \item \tl_use:N \l_exercise_text_tl
  \begin{itemize}[leftmargin=*]
  % process the \given part
  \seq_map_inline:Nn \l_exercise_given_seq { \item[given:] $##1$ }
  % process the \asked part
  \item[asked:] $\tl_use:N \l_exercise_asked_tl$
  % process the \sol part
  \item[solution:]
  \seq_set_split:NnV \l_exercise_body_seq { \step } \l_exercise_sol_tl
  \int_compare:nTF { \seq_count:N \l_exercise_body_seq = 1 }
   { % only one step
     $\tl_use:N \l_exercise_sol_tl$
   }
   { % more steps
    % throw away the first (empty) item
    \seq_pop_left:NN \l_exercise_body_seq \l_tmpa_tl
    \begin{itemize}[label=\textbullet]
    \seq_map_inline:Nn \l_exercise_body_seq { \item $##1$ }
    \end{itemize}
   }
  \end{itemize}
 }
\tl_new:N \l_exercise_text_tl
\tl_new:N \l_exercise_asked_tl
\tl_new:N \l_exercise_sol_tl
\seq_new:N \l_exercise_body_seq
\seq_new:N \l_exercise_given_seq
\cs_generate_variant:Nn \exercise_process:n { V }
\ExplSyntaxOff

\begin{document}

\begin{exercise}
Calculate the length of a circle of radius \SI{1}{m}
  \given r=1
  \asked L
  \sol L = 2 \pi r = 2\cdot\pi\cdot 1\Rightarrow L = \SI{6.28}{m}
\end{exercise}

\begin{exercise}
Calculate the volume of a cylinder with a 
radius of \SI{2}{m} and a height of \SI{3}{m}.
  \given r = 2
  \given h = 3
  \asked V
  \sol
    \step A = 2 \pi r = 2 \cdot \pi \cdot 2 \Rightarrow A = \SI{12.57}{m^2}
    \step V = A h = 12.57 \cdot 3 \Rightarrow V = \SI{37.70}{m^3}
\end{exercise}

\end{document}

在此处输入图片描述

答案2

稍有不同如何你要求这样做,但这就是我的做法。

\documentclass[a4paper,12pt]{article}
\usepackage{enumitem}
\usepackage[marginparwidth=1in, reversemp]{geometry}

\newcommand{\given}[1]{\item[Given:] $#1$}
\newcommand{\asked}[1]{\item[Asked:] $#1$}
\let\step\item
\newcounter{exercise}
\newenvironment{exercise}[1]{\stepcounter{exercise}
                                \begin{itemize}
                                \item[\bfseries Ex \theexercise.] #1
                                \begin{itemize}
                        }{
                        \end{itemize}
                        \end{itemize}
                        }
\newenvironment{sol}{\begin{itemize}[label={}]}{\end{itemize}}

\begin{document}    
\begin{exercise}{Calculate the volume of a cylinder with a radius of 2m and a height of 3m.}
        \given{r = 2 \qquad h = 3}
        \asked{V}
        \begin{sol}
            \step $A = 2 \pi r = 2 \cdot \pi \cdot 2 \Rightarrow A = 12.57 m^2$
            \step $V = A h = 12.57 \cdot 3 \Rightarrow V = 37.70 m^3$
        \end{sol}
\end{exercise}
\end{document}

环境exercise有一个参数,即问题的文本,练习编号是自动的。sol它不是命令,而是环境,因此可以有许多步骤。

相关内容