I'd like to create a LaTeX3 function, \my_func, that has a single parameter, #1, which is expected to be initialized to a LaTeX3 string. \my_func is to create a new document command using xparse's \NewDocumentCommand, whose name is the one passed as argument to \my_func. How can I do so?

To give a concrete usage example, there's a bunch of mathematical functions that I wish to define. I'm still not sure what symbols I wish to use to represent them, though. So I define macros \func1, \func2, etc. with rather long and expressive names, e.g. \LeftProjectionOfTheSecondDegree. These names won't change, but the symbols that represent them on paper may change later on.

Each of these functions can appear either standalone, or inside a bigger expression. When used inside a bigger expression, the functions always take arguments. However when they are used on their own, I need to express how many arguments they take, so I use subscripts, but these subscripts should not appear when the functions are part of a larger expression, because then the number of arguments can be easily deduced from the context.

  • 1
    Could you please add an example of supposed usage? – egreg Oct 6 '17 at 9:36
  • 2
    However, \exp_args:Nc \NewDocumentCommand {#1}{...}{...} should do as code in \my_func:n – egreg Oct 6 '17 at 9:37
  • @egreg: I've added a supposed usage example. It doesn't compile, but hopefully it conveys the gist of what I'm after. – Evan Aad Oct 6 '17 at 10:03
  • @EvanAad egreg already answered your question in his comment. Is that what you are after? By the way, this is a weird option, you plan to use \use:c {foo} inside the \foo command? That gives you an infinite loop. And, apart from that, are you looking precisely for a way of having a macro that works out and in of math mode? You have \ensuremath for that. – Manuel Oct 6 '17 at 10:09

You are precisely looking for \ensuremath, I think. \ensuremath ensures that its contents is typeset inside math mode: are you in math mode? ok, just put the code; are you out of math mode? ok, then type $..$ around the expression.




\savelongexpression\nameone{x^2 + y^2}
\savelongexpression\nametwo{\sin x \cos x}


\nameone{} and \nametwo. Also $\nameone + \nametwo$, and
  \nameone - \nametwo


Note that you need to add {} or any alternative after the command if you want the space tokenized.

  • 1
    There's no need to use xparse here in this part since we don't use any of its features. But nothing wrong with using it, either. – Manuel Oct 6 '17 at 10:42
  • Well, there's some piece of the puzzle I didn't mention: when the expression is used standalone, it looks slightly differently than when it is used inside a bigger expression. Specifically, when used standalone, the superscript ^{S,T} should be appended to it; this superscript can be deduced from the context when the expression is used inside a bigger expression, so it can be left off under those circumstances. This is why I wanted to use an optional star. – Evan Aad Oct 6 '17 at 10:45
  • 1
    Then we can't make use of \ensuremath, we need to substitute \ensuremath{#2} by \mode_if_math:TF { #2 } { $ \relax #2^{S,T} $ }. – Manuel Oct 6 '17 at 10:50
  • 1
    It's present in \ensuremath and it has to do probably with the fact that $ expands things to look for another $, but I really don't remember right now the exact reason. Or may be because if it wasn't present, \ensuremath{} would output $$ which would open display math, rather than giving an empty math $\relax$. – Manuel Oct 6 '17 at 10:57
  • 1
    Probably yes... – Manuel Oct 6 '17 at 11:07

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.