3

I was just wondering if anyone out there was familiar with some apparently less-than-conventional syntax for creating a \newcommand...

Sorry if this is kind of basic, but I just wanted to make small modifications to \mathtool's \overbracket and \underbracket commands to test out different options on some formulas I've written. And the easiest way to switch commands in-and-out and compare modifications with each other or the original is to maintain the same kind of syntax and Ctrl+F/replace any command names, so I would like to be able to write something along the lines of \overbracketmod{...}^{...} in the same spirit as the stock \overbracket{...}^{...} command

Is anyone familiar with how to write a \newcommand that will allow me to preserve this kind of syntax (or any references that mention this)? I'm pretty sure I saw some example code online a few days ago and thought "huh that's cool, I want to use that", but have since lost the resource and none of my recent searches (e.g. newcommand with caret/superscript, newcommand with raised/lower arguments, etc) have been remotely successful...(if it helps, I think I vaguely recall it being of the form \newcommand...#1^#2?)

Thanks in advance

2
  • 2
    Start reading on embellishments in texdoc xparse.
    – user202729
    Commented Aug 26, 2022 at 0:27
  • 1
    @user202729 or better texdoc usrguide3 (since xparse package is no longer required) Commented Aug 26, 2022 at 0:34

2 Answers 2

4

mathtool's \underbracket is defined to be using \mathop outer wrapper that essentially allows to perform such a behavior with superscript and subscript — it puts arguments above and below accordingly.

Here's a simple example

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\[
\mathop{\pi}^{up}_{down}
\]

\end{document}

enter image description here

So you might define your \overbracketmod as the following

\newcommand{\overbracketmod}[1]{\mathop{<command definition>}}

However, as it was mentioned in the comments, the more versatile way is using \NewDocumentCommand from LaTeX3 that has a big variety of arguments type (see the manual). Namely, you're looking for e embellishment type, so that if you specify e{^_} in the definition, #1 will be exactly what you type after ^ and #2 is what you passed via _. For example:

\documentclass{article}

\NewDocumentCommand{\overbracketmod}{ m e{^_} }{arg: \emph{#1} superscript: \emph{#2}, subscript: \emph{#3}}

\begin{document}

\overbracketmod{test}^{up}_{down}

\end{document}

enter image description here

4

The syntax \overbracket{<math>}^{<math>} is foreign to LaTeX and it stems from a low-level trick used by Knuth for \overbrace.

My suggestion is to not use it.

Let's see an example:

\documentclass{article}
\usepackage{mathtools}

\begin{document}

\begin{gather*}
\overbracket{1+1+\dots+1}^{n}+1 \\
\mspace{6mu} 1+1+\dots+1+1
\end{gather*}

\end{document}

The small space is used to align the digits in both lines so as to make the issue clearer.

enter image description here

Can you see the problem? Yes, the space around the final + sign is utterly wrong in the top row. Why? Because, eventually, the \overbracket construction becomes a \mathop atom and the rules of TeX end up with the final + to be considered as an ordinary atom rather than a binary operation.

You can fix the issue by making the whole thing an ordinary atom.

\documentclass{article}
\usepackage{mathtools}

\NewDocumentCommand{\obracket}{oomm}{{% note the second brace
  \IfNoValueTF{#2}{%
    \IfNoValueTF{#1}{%
      \overbracket{#3}^{#4}%
    }{%
      \overbracket[#1]{#3}^{#4}%
    }%
  }{%
    \overbracket[#1][#2]{#3}^{#4}%
  }%
}}  

\begin{document}

\begin{gather*}
1+\obracket{1+1+\dots+1}{n}+1 \\
1+1+1+\dots+1+1
\end{gather*}

\end{document}

enter image description here

Now you see that all the symbols are correctly spaced, because the additional braces around the whole construction makes it into an ordinary atom.

If you want to keep the foreign syntax, you can use the “embellishment” argument type:

\documentclass{article}
\usepackage{mathtools}

\NewDocumentCommand{\obracket}{oome{^}}{{% note the second brace
  \IfNoValueTF{#2}{%
    \IfNoValueTF{#1}{%
      \overbracket{#3}\IfValueT{#4}{^{#4}}%
    }{%
      \overbracket[#1]{#3}\IfValueT{#4}{^{#4}}%
    }%
  }{%
    \overbracket[#1][#2]{#3}\IfValueT{#4}{^{#4}}%
  }%
}}  

\begin{document}

\begin{gather*}
1+\obracket{1+1+\dots+1}^{n}+1 \\
1+1+1+\dots+1+1
\end{gather*}

\end{document}

The output is the same as before. Define your own command in a similar fashion (perhaps without the optional arguments).

You must log in to answer this question.

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