11

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.

MaxNoe
  • 6,136
  • 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

1 Answers1

17

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}
daleif
  • 54,450