3

I seem to have trouble putting an underbrace into a macro and I don't know why. Here is the MWE

\documentclass{standalone}
\usepackage{xstring}
\newcommand{\URD}[1]
{
 \IfEq{#1}{}
 {\textsc{urd}}
 {\textsc{urd}\ensuremath{\left(#1\right)}}
}
\begin{document}
$\URD{K_{\underbrace{g,\ldots,g}_7}}$
\end{document}

It returns the error:

! Illegal parameter number in definition of \@xs@arg@i.

when compiled. \URD works fine on simple expressions that do not contain underbrace.

What am I to do?

  • xstring has the bad habit of fully expanding its arguments. Look for \noexpandarg in the manual. There are cheaper ways to test for an empty argument. – egreg Sep 27 '17 at 15:54
  • @egreg So what would be a better way to write such commands? I do this alot. – Don Kreher Sep 27 '17 at 15:58
4

You should use \noexpandarg or fragile commands such as \textsc and \underbrace would not survive \IfEq.

However there are much better ways. For this case I suggest an optional argument, rather than a mandatory one.

\documentclass{article}
\usepackage{xparse}

\NewDocumentCommand{\URD}{o}{%
  \textnormal{\scshape urd}%
  \IfValueT{#1}{(#1)}%
}

\begin{document}

$\URD$

$\URD[K_{\underbrace{g,\ldots,g}_7}]$

\end{document}

enter image description here

I'd avoid both \ensuremath and \left\right.

An extended version so you can choose the size of the delimiters, if you so wish. With \URD*{...} you get \left and \right, the last example shows why it would be bad to use them by default.

\documentclass{article}
\usepackage{xparse}

\NewDocumentCommand{\URD}{soo}{%
  \textnormal{\scshape urd}%
  \IfBooleanTF{#1}%
    {\IfValueT{#2}{\left(#2\right)}}%
    {\IfValueT{#2}{\IfValueTF{#3}%
      {\mathopen{#2(}#3\mathclose{#2)}}%
      {(#2)}%
    }}%
}

\begin{document}

$\URD$

$\URD[K_{\underbrace{g,\ldots,g}_7}]$

$\URD[\big][K_{\underbrace{g,\ldots,g}_7}]$

$\URD*[K_{\underbrace{g,\ldots,g}_7}]$

\end{document}

enter image description here

  • But \left( and \right) will be necessary for some expressions. – Don Kreher Sep 27 '17 at 17:08
  • @DonKreher Did you look at the output in this case with \left and \right? – egreg Sep 27 '17 at 17:18
  • @DonKreher I added an extended version with two optional arguments, also showing what you'd get with automatic \left and \right. – egreg Sep 27 '17 at 17:28
  • Thanks. I did not look at the output in this case with \left and \right, but I see your example of why it may look bad. It was a different case. It looks like I will have to learn xparse. It seems I discover a new package to learn every day. And I have been at this since the beginning. – Don Kreher Sep 28 '17 at 17:14
  • @DonKreher The current count of subdirectories of the .../tex/latex directory is 2352, it takes more than six years to learn a package a day. ;-) – egreg Sep 28 '17 at 17:39

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.