1

The following Latex fails to build with mysterious errors, the first one being Argument of \\bred has an extra }.

\documentclass{article}

\usepackage{xparse}
\usepackage{amsmath}

\newcommand{\restr}[2]{{\ifx\\#2\\ #1 \else #1|#2 \fi}}
\newcommand{\opensg}[2][]{\restr{\mathcal{T}\ifx\\#2\\ \else_{#2}\fi}{#1}}
\newcommand{\opens}[1][]{\opensg[#1]{}}
\NewDocumentCommand{\closureg}{ O{\opens} O{P} m }{\overline{#3}(#1)_{#2}}
\NewDocumentCommand{\closure}{ O{} O{P} m }{\closureg[{\opens[#1]}][#2]{#3}}
\newcommand{\bred}[2][\opens]{#2^{#1}}

\begin{document}

$\closure[{\bred[A]{B}}]{C}$

\end{document}

It can be made to build with $\closure[{\bred{B}}]{C}$ instead.

What is the problem, and how can I get the optional argument [A] to work here?

1 Answer 1

2

It seems quite a complicated set of commands… Anyway, stick to \NewDocumentCommand.

\documentclass{article}

\usepackage{amsmath}

\NewDocumentCommand{\restr}{mm}{#1\IfBlankF{#2}{|#2}}
\NewDocumentCommand{\opensg}{O{}m}{\restr{\mathcal{T}\IfBlankF{#2}{_{#2}}}{#1}}
\NewDocumentCommand{\opens}{O{}}{\opensg[{#1}]{}}
\NewDocumentCommand{\closureg}{ O{\opens} O{P} m }{\overline{#3}(#1)_{#2}}
\NewDocumentCommand{\closure}{ O{} O{P} m }{\closureg[{\opens[#1]}][#2]{#3}}
\NewDocumentCommand{\bred}{O{\opens}m}{#2^{#1}}

\begin{document}

$\closure[\bred[A]{B}]{C}$

\end{document}

Loading xparse hasn't been necessary since October 2020.

enter image description here

1
  • Thanks, this has been bugging me for years! The problem seems to be the use of \ifx specifically. Replacing it with \IfBlankF gets rid of the build errors, and those additional braces in optional arguments get the look right. I'll convert all my macros to \NewDocumentCommand to be sure.
    – kaba
    Feb 23, 2023 at 22:47

You must log in to answer this question.

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