定理环境中的括号、大括号和无边界变量冲突

定理环境中的括号、大括号和无边界变量冲突

我正在制定一个名为“练习”的定理,我希望能够通过以下方式定义它:

\begin{exercise} ...           % Regular exercise with numbering from other exercises
\begin{exercise}[alt] ...      % Regular exercise with alternate name
\begin{exercise}{num} ...      % Exercise with special numbering
\begin{exercise}{num}[alt] ... % Exercise with special name and numbering

以下是我目前从该站点搜集到的信息:

\documentclass{article}
\usepackage{xparse}
\usepackage{ifmtarg}
\usepackage{amsthm}
\newtheorem{innerexercise}{Exercise}

\makeatletter
\newenvironment{exercise}[1]
{
\IfNoValueTF{#1}{}{\@ifmtarg{#1}{}{\renewcommand\theinnerexercise{#1}}}
\innerexercise
}
{\endinnerexercise}
\makeatother

\begin{document}
\end{document}

但是,问题是,当我不想重新编号练习时,备用名称中的左括号会被算作输入。我得到的不是图片中的第一个,而是第二个: 在此处输入图片描述 现在我可以随时添加空括号,例如\begin{exercise}{}[alt],但我想保留简单书写的能力\begin{exercise}[alt],因为这是我经常使用的东西。

那么有没有办法可以省去将下一个字符解释为输入的过程?谢谢!

PS 如果我这样做\begin{exercise} Text of exercise,这也会中断,因为它输出“锻炼 T.锻炼的扩展”,由于同样的原因,这不是我想要的。

PPS 我认为这与我的输入是强制性的事实有关。如果我将其设为可选,那么如果我必须标记该参数,我就可以了,例如\begin{exercise}[num=3.14,alt-name="Title"]

答案1

您可以在此处对进一步的可选参数使用不同的分隔符(...)

\documentclass{article}
\usepackage{amsthm}
%\usepackage{xparse} % not needed after 2020-10-01

\newtheorem{innerexercise}{Exercise}

\NewDocumentEnvironment{exercise}{d()}
 {%
  \IfValueT{#1}{\renewcommand{\theinnerexercise}{#1}\addtocounter{innerexercise}{-1}}%
  \innerexercise
 }
 {\endinnerexercise}

\begin{document}

\begin{exercise}
Regular exercise with numbering from other exercises
\end{exercise}

\begin{exercise}[alt]
Regular exercise with alternate name
\end{exercise}

\begin{exercise}(num)
Exercise with special numbering
\end{exercise}

\begin{exercise}(num)[alt]
Exercise with special name and numbering
\end{exercise}

\begin{exercise}
Regular exercise with numbering from other exercises
\end{exercise}

\end{document}

在此处输入图片描述

但是,多个可选参数很麻烦,因为总是很难记住它们的相对位置并且[alt](num)肯定会失败。

这是带有键值语法的版本。定义起来更复杂,使用起来更方便。

\documentclass{article}
\usepackage{amsthm}
%\usepackage{xparse} % not needed after 2020-10-01

\newtheorem{innerexercise}{Exercise}

\ExplSyntaxOn
\NewDocumentEnvironment{exercise}{O{}}
 {
  \keys_set:nn { gintz/exercise } { #1 }
  \gintz_exercise_setup:
 }
 {\endinnerexercise}

% the allowed keys
\keys_define:nn { gintz/exercise }
 {
  num .tl_set:N = \l__gintz_exercise_num_tl,
  alt .tl_set:N = \l__gintz_exercise_alt_tl,
 }

\cs_new_protected:Nn \gintz_exercise_setup:
 {
  \tl_if_empty:NF \l__gintz_exercise_num_tl
   {
    \renewcommand{\theinnerexercise}{\l__gintz_exercise_num_tl}
    \addtocounter{innerexercise}{-1}
   }
  \tl_if_empty:NTF \l__gintz_exercise_alt_tl
   {
    \innerexercise
   }
   {
    \innerexercise[\l__gintz_exercise_alt_tl]
   } 
 }
\ExplSyntaxOff

\begin{document}

\begin{exercise}
Regular exercise with numbering from other exercises
\end{exercise}

\begin{exercise}[alt=alt]
Regular exercise with alternate name
\end{exercise}

\begin{exercise}[num=num]
Exercise with special numbering
\end{exercise}

\begin{exercise}[alt=alt,num=num]
Exercise with special name and numbering
\end{exercise}

\begin{exercise}
Regular exercise with numbering from other exercises
\end{exercise}

\end{document}

相关内容