关于 \int_case:nn 中缺少 } 的消息

关于 \int_case:nn 中缺少 } 的消息

我明白了

! Missing } inserted.
<inserted text> 
                }
l.47     Test $\Id$
                   , $\Id{A}$ and $\Id{A,B}$ inline.

编译时

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse} % loads expl3
%See interface3.pdf

\ExplSyntaxOn

\NewDocumentCommand \Id {O{}}
  {
     \Id:n #1
  }

\cs_new:Npn \Id:n #1
  {
    \clist_set:Nn \l_tmpa_clist {#1}
    \int_case:nn
      {\clist_count:N \l_tmpa_clist}
      {
        {0}
        {
          \mathop{Id}
        }
        {1}
        {
          \mathop{Id}_{#1}
        }
        {2}
        {
          \mathop{Id}
            ^{\clist_item:Nn \l_tmpa_clist 1}
            _{\clist_item:Nn \l_tmpa_clist 2}
        }
      }
      {
          msg_error:nn {shmuel} {More than one argument for {\backslash}Id}
      }
  }

\begin{document}

\ExplSyntaxOn
\msg_new:nnn {shmuel} {short} {\#1}
\msg_new:nnnn {shmuel} {long} {\#1} {\#2}
%Should it be ## rather than \#?
\ExplSyntaxOff

Test $\Id$, $\Id{A}$ and $\Id{A,B}$ inline.
\begin{equation}
  Test \Id, \\
   \Id{A}, \\
   \Id{A,B}
\end{equation}

\end{document}

我将参数说明符更改为 {O{{}}},并将 \Id 调用中的不正确括号更改为方括号。这样更好,但现在默认值是采用 {} 而不是空 clist,_ 被视为文本字符而不是下标,并且 [A,B] 被视为单个标记而不是两个列表。

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse} % loads expl3
%See interface3.pdf

\ExplSyntaxOn

\NewDocumentCommand \Id {O{\{\}}}
  {
     \Id:n #1
  }

\cs_new:Npn \Id:n #1
  {
    \clist_set:Nn \l_tmpa_clist {#1}
    \int_case:nnF
      {\clist_count:N \l_tmpa_clist}
      {
        {0}
        {
          \mathop{Id}
        }
        {1}
        {
          \mathop{Id}_{#1}
        }
        {2}
        {
          \mathop{Id}
            ^{\clist_item:Nn \l_tmpa_clist 1}
            _{\clist_item:Nn \l_tmpa_clist 2}
        }
      }
      {
          msg_error:nn {shmuel} {More than two arguments for {\backslash}Id}
      }
  }

\begin{document}

\ExplSyntaxOn
\msg_new:nnn {shmuel} {short} {\#1}
\msg_new:nnnn {shmuel} {long} {\#1} {\#2}
%Should it be ## rather than \#?
\ExplSyntaxOff

Test $\Id$, $\Id[A]$ and $\Id[A,B]$ inline.
\begin{equation}
  Test \Id, \\
   \Id[A], \\
   \Id[A,B]
\end{equation}

\end{document}

答案1

你定义了\Id一个可选参数,默认为无,因此

 $\Id$

$\Id:n $

因此,$当获取参数后,一切都会出错,您需要\Id:n{#1} 用括号传递以确保#1获取参数完好无损地传递。

我不确定所有代码的意图,但除了传递三个列表时预期的错误之外,它运行时没有错误。

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse} % loads expl3
%See interface3.pdf

\ExplSyntaxOn

\NewDocumentCommand \Id {O{}}
  {
     \Id:n {#1}
  }

\cs_new:Npn \Id:n #1
  {
    \clist_set:Nn \l_tmpa_clist {#1}
    \int_case:nnTF
      {\clist_count:N \l_tmpa_clist}
      {
        {0}
        {
          \mathop{\mathrm{Id}}
        }
        {1}
        {
          \mathop{\mathrm{Id}}\sb{#1}
        }
        {2}
        {
          \mathop{\mathrm{Id}}
            \sp{\clist_item:Nn \l_tmpa_clist 1}
            \sb{\clist_item:Nn \l_tmpa_clist 2}
        }
      }
      {}
      {
          \msg_error:nnn {shmuel}{long} {#1}
      }
  }

\begin{document}

\ExplSyntaxOn
\msg_new:nnnn {shmuel} {long} {more\ than\ two\ arguments\ for\ \string\Id: #1} {help}
\ExplSyntaxOff

Test $\Id$, $\Id[A]$ and $\Id[A,B]$ $\Id[{too},{many},{parms}] $ inline.
\begin{equation}
  Test \Id, \\
   \Id[A], \\
   \Id[A,B]
\end{equation}

\end{document}



\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse} % loads expl3
%See interface3.pdf

\ExplSyntaxOn

\NewDocumentCommand \Id {O{}}
  {
     \Id:n {#1}
  }

\cs_new:Npn \Id:n #1
  {
    \clist_set:Nn \l_tmpa_clist {#1}
    \int_case:nnF
      {\clist_count:N \l_tmpa_clist}
      {
        {0}
        {
         \operatorname{Id}
        }
        {1}
        {
         \operatorname{Id} \sb {#1}
        }
        {2}
        {
         \operatorname{Id}
           \sp {\clist_item:Nn \l_tmpa_clist 1}
           \sb {\clist_item:Nn \l_tmpa_clist 2}
        }
      }
      {
          \msg_error:nn {shmuel} {More than two arguments for {backslash}Id}
      }
  }

\ExplSyntaxOff

\ExplSyntaxOn
\msg_new:nnn {shmuel} {short} {##1}
\msg_new:nnnn {shmuel} {long} {##1} {##2}
\ExplSyntaxOff

\begin{document}

Test $\Id$, $\Id[A]$ and $\Id[{A,B}]$ inline.
\begin{equation}
  \text{Test} \ \Id, \\
   \Id[A], \\
   \Id[{A,B}]\\
   \Id[A,B,C]\\
\end{equation}

%Test $\Id[{too},{many},{parms}]$

\ExplSyntaxOn
\msg_error:nn {shmuel} {More than two arguments for {backslash}Id}

\end{document}

给出错误

! Package shmuel Error: more than two arguments for \Id:{too},{many},{parms}

相关内容