2

I would like to use a DeclareDocumentCommand in order to be able to define a different command (using arguments from the first). I would like to make the internal arguments long. Here is the code I have as well as the output.

\documentclass{article}
\usepackage{xparse}
\begin{document}


\DeclareDocumentCommand{\DC}{m}{%
\DeclareDocumentCommand{#1}{+m}
{%
##1
}
}

\DC{test}

\test{Will this

break?}
\end{document}

and I get an "unknown argument type e" error.

However if I do

\documentclass{article}
\usepackage{xparse}
\begin{document}


\DeclareDocumentCommand{\test}{+m}{%
#1
}

\test{Will this

break?}
\end{document}

It works fine.

  • 1
    Your passed argument is a string, not a command name. You need to do \DC{\test} – Loop Space Jul 8 '14 at 9:26
  • The error looks a bit odd as internally the first token (t) is being picked up as the name then e is parsed as an arg spec. (Arguably we should trap this.) As @LoopSpace says, if you want to define one command using another you need to give the command name. However, this looks borderline for using xparse: without context, it's hard to see how this is a 'well-defined' document command. – Joseph Wright Jul 8 '14 at 9:38
  • What's the advantage of the obscure \DC{\test} over \NewDocumentCommand{\test}{+m}{#1}? – egreg Jul 8 '14 at 10:48
2

Your code produces

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! LaTeX error: "xparse/not-one-token"
! 
! First argument of '\DeclareDocumentCommand' must be a command.
! 
! See the LaTeX3 documentation for further information.
! 
! For immediate help type H <return>.
!...............................................  

which is due to the fact that you should call

\DC{\test}

Older versions of xparse would produce the error message

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! LaTeX error: "xparse/unknown-argument-type"
! 
! Unknown argument type 'e' replaced by 'm'.
! 
! See the LaTeX3 documentation for further information.
! 
! For immediate help type H <return>.
!...............................................  

but it's due to the same problem.

1

Without knowing more about what you are up to, I can't say I endorse this solution, though I often do find it useful to create a 'command maker' in order to create a series of commands that do the same general thing without actually writing out the full \newcommand sequence every time.

So maybe you have in mind this:

\documentclass{article}
\usepackage{xparse}

\DeclareDocumentCommand{\DC}{m}
{%
 \expandafter\DeclareDocumentCommand\expandafter{\csname #1\endcsname}{+m}
%  compare:
%  \expandafter\long\expandafter\def\csname #1\endcsname##1%
  {%
    {\bfseries ##1}% for example
  }%
}

\begin{document}

\DC{test}

\test{Will this

break?}

Again:
\DC{xtest}

\xtest{Will this

break?}

\end{document}

I hope you are being careful with \DeclareDocumentCommand!

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.