8

I'm redefining beamer blocks to use tcolorboxes.

I want to make the title optional, but want to keep the curly braces.

How can I achieve this?

I tried this:

\documentclass{article}

\usepackage{fontspec}
\usepackage{xparse}

\NewDocumentCommand\test{d{}}{Hello\IfValueT{#1}{~#1}!}

\begin{document}
  \test
  \test{Bernd}
\end{document}

And this

\documentclass{article}

\usepackage{fontspec}
\usepackage{xparse}

\NewDocumentCommand\test{d\{\}}{Hello\IfValueT{#1}{~#1}!}

\begin{document}
  \test
  \test{Bernd}
\end{document}

Both do not work, the first raises an error (use a single token as delimiter) the second one simply ignores the argument.

  • 1
    There's a good reason that LaTeX sticks to one syntax for optional arguments: as noted in the answer, grabbing using a brace group is doable but that's not the same as recommended! – Joseph Wright Jul 17 '15 at 12:41
  • 1
    I'm writing a wrapper around existing code, not designing my own. I fully agree with you that the delimiters should show the kind of the argument – MaxNoe Jul 17 '15 at 12:43
  • 1
    Just wanted to make sure it was here for the general reader: we added it as of course there are places it makes sense, particularly if the syntax is already fixed. – Joseph Wright Jul 17 '15 at 12:47
13

Read the xparse manual more carefully, hint: the g specifier

\documentclass[a4paper]{memoir}
\usepackage{xparse}
\NewDocumentCommand\test{ g }{
  \IfNoValueTF{#1}{Test}{#1}
}
\begin{document}

\test

\test{Hmm}

\end{document}

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.