The \NewDocumentCommand command uses a clever trick to make better error messages -- if the arguments of \mycommand are messed up, it would be undesirable for the resulting error to be something like "File ended while scanning use of \__xparse_grab_D:w", so whenever xparse is going to grab another argument, it defines \mycommand<space> to do the argument grabbing and then uses that to do the work so that if something goes wrong the error is instead something like "Paragraph ended before \mycommand<space> was complete".
I want a command like \NewDocumentCommandAs that takes two arguments -- one the name of the macro to be defined, and the second the name of the command to be used in error messages.
I have two use cases for this. One is that I have a package that defines commands that are only to be used inside a certain environment. So it defines the commands in the package's private namespace and \let's the public name for the command to the private version at the start of the environment. Obviously I want to base all the error messages around the public name of the command, so I would like to say something like
\NewDocumentCommandAs \__my_pkg_cmd:args \cmd {<args>}{<body>}
The second use case is that I have a command with an optional argument, an argument that is everything up to the first open parenthesis, and then an argument delimited by balanced parentheses starting with that first open parenthesis. Because the "until" argument type absorbs the tokens it is scanning up to, I need to reinsert the open parenthesis and use an auxilary:
\NewDocumentCommand \mycommand { o u( } { \mycommand_aux{#1}{#2}( }
\NewDocumentCommandAs \mycommand_aux \mycommand { m m r() } {
% do stuff here
}
Of course this could be fixed by making a version of u (maybe U?) that reinserts the tokens it removes, but that might be an unusual use case.
For formatting purposes, I guess I'll submit my two implementations as an answer. Has anyone else dealt with this issue? Is there some better approach that I am missing? Should I not do this and solve these problems in a different way? Is this a feature that might be added to a future version of xparse?
\__mypkg_cmd:argsis supposed to be a private version of a document command that can only be used in a specific environment. So in the startup code for that environment, I'll say\let\cmd\__my_pkg_cmd:args. I think it's appropriate to use this command to define a document-level macro in a private namespace? – Hood Chatham Feb 01 '17 at 17:25user levelcommands are very short and could quite well be used to do something else outside of my environment (e.g.,\class). I was following the design of tikz where it has a bunch of lines like\let\path=\tikz@command@pathto ensure that someone could define\pathto do something else outside of tikz drawing environments. This seems to me like a good way to do things (especially because code inside tikz environments is very different from code outside of them so there is limited risk of confusion). – Hood Chatham Feb 02 '17 at 18:46_auxfor a non LaTeX3 style command, could you explain what the "best practices" solution to that particular problem would be? You say that the way I did it is wrong, but haven't suggested an alternative. Honestly I don't see that it matters so much as long as name reasonably communicates its function. I would be perfectly happy deleting the_auxand throwing in an@. – Hood Chatham Feb 02 '17 at 19:01\NewDocumentCommandAsand then\let\mycommandto the outer one. Though I'm not sure what the argspec would be.\__mypkg_mycmd:wwand\__mypkg_mycmd_aux:mmw? – Hood Chatham Feb 02 '17 at 19:05