消息“!缺少 \endcsname 插入。”调用 \msg_error:nn

消息“!缺少 \endcsname 插入。”调用 \msg_error:nn

我明白了

! Missing \endcsname inserted.
<to be read again> 
                   \delimiter 
l.58 ...ore than two arguments for {\backslash}Id}

? 

对于本文档

\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}]
\end{equation}

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

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

\end{document}

我更新了代码以反映注释,并对其进行了修改以支持两个类似的命令 \Id 和 \ID,它们仅在生成的文本大小写上有所不同。但是,我无法在消息文本中获取反斜杠,而不会获取无关文本。

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

\ExplSyntaxOn

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

\NewDocumentCommand \ID {O{}}
  {
     \Id:nn {ID} {#1}
  }

\cs_new:Npn \Id:nn #1 #2
  {
    \clist_set:Nn \l_tmpa_clist {#2}
    \int_case:nnF
      {\clist_count:N \l_tmpa_clist}
      {
        {0}q
        {
         \operatorname{#1}
        }
        {1}
        {
         \operatorname{#1} \sb {#2}
        }
        {2}
        {
         \operatorname{#1}
           \sp {\clist_item:Nn \l_tmpa_clist 1}
           \sb {\clist_item:Nn \l_tmpa_clist 2}
        }
      }
      {
         \msg_error:nnnn {shmuel} {toomany} {two} {#1: [#2]}
      }
  }

\msg_new:nnn {shmuel} {short} {#1}
\msg_new:nnnn {shmuel} {long} {#1} {#2}
\msg_new:nnnn
  {shmuel}
  {toomany}
  {More \ than \ #1  \ items \ for \ \string\\#2}
  %                                  {\textbackslash}#2?

\ExplSyntaxOff

\begin{document}

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

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

\end{document}

答案1

这个问题在上一个问题的答案中已经得到解答,尽管该问题中的错误突出显示位于代码的不同部分。您不希望出现导致错误的情况,这是在错误消息中使用以显示过长列表的\backslash版本。请注意,应该仅通过模块和名称来引用消息,而不是文本。#1\msg_error

\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}

相关内容