0

I get

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

when compiling

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

I changed the argument specifier to {O{{}}} and changed the incorrect braces in the \Id invocations to brackets. Better, but now the default value is take a {} rather than an empty clist, the _ is taken as a text character rather as a subscript and [A,B] is treated as a single token rather than as a list of two.

\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

You defined \Id with an optional argument defaulting to nothing so

 $\Id$

is

$\Id:n $

so gets $ as the argument after which everything goes wrong, you need to pass \Id:n{#1} with braces to ensure that #1 gets passed on intact.

I'm not sure of the intention of all the code but this runs without error other than the expected error when passed a list of three.

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

giving an error

! Package shmuel Error: more than two arguments for \Id:{too},{many},{parms}
  • I tried to fix the code to reflect your comments (see edit), but it's treating [A,B] and [{A,B}] as a list of one token, it's still not taking the default to an empty clist and it's not treating _ as a subscript request. Note that the calls to \Id are in Math mode. – shmuel Jun 18 at 16:21
  • @shmuel you need [A,B] as you defined an optional argument and \sub and \sup (or their l3 equivalents) for sub/super scripts as _ is a normal letter in expl3 code sections. – David Carlisle Jun 18 at 16:34
  • @shmuel no! your edit makes it worse, do not introduce \{ !! I meant you need \Id:n {#1} not \Id:n #1 but also please don't edit partial answers into the question it makes the post very hard to understand later, – David Carlisle Jun 18 at 16:36
  • It now sort of works, but Id renders in italic; wrapping it in \mathrm fixes that. – shmuel Jun 19 at 16:22
  • I tried testing my error code; pleas see my latest edit for the TeX error message I got. – shmuel Jun 19 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.