0

I'm seeing a rather unexpected result with \NewDocumentCommand. Suppose I have a custom-defined command \mybold{}, like this:

\NewDocumentCommand{\mybold}{m}{\mathbf{#1}}

Or \oybold[]:

\NewDocumentCommand{\oybold}{O{a}}{\mathbf{#1}}

I would expect to be able to refer to these in another \NewDocumentCommand, like any of the following:

\NewDocumentCommand{\mmyvec}{m}{\hat\mybold{#1}}
\NewDocumentCommand{\moyvec}{m}{\hat\oybold[#1]}
\NewDocumentCommand{\omyvec}{O{a}}{\hat\mybold{#1}}
\NewDocumentCommand{\ooyvec}{O{a}}{\hat\oybold[#1]}

The first and the third just produce a bunch of errors (missing $ inserted, missing } inserted, extra } etc.); while \moyvec[k] and \ooyvec[k] produce the following output:

enter image description here

(rather than the desired \hat\mathbf{k})

I assume this has something to do with it entering math mode twice. How can I fix this?

3
  • 3
    do not show only code snippets. Always show a small but complete example that can be used for a test. Commented Nov 13, 2023 at 16:04
  • 1
    \hat\mybold is the same as \hat{\mybold} so naturally raises errors as the base of the accent is \mybold with no argument supplied. Commented Nov 13, 2023 at 16:07
  • 1
    This isn't related to math mode or \NewDocumentCommand you would see same if you defined \mybold simply as \def\mybold#1{#1} Commented Nov 13, 2023 at 16:08

1 Answer 1

5

You want the argument of \hat to be \mybold{#1} not \mybold so the markup should be \hat{\mybold{#1}}

with the optional argument version it is similar \hat{\oybold}[k] the base of the accent is \oybold with no optional argument so you get \hat{a} then the [k] is just typeset as text, it is not an argument to the command.

You want \hat{\oybold[#1]}

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .