I don't like the ^
or _
notations for \overbrace
and similar commands. Anyway, you can use it thanks to the e
mbellishment argument type to \NewDocumentCommand
.
Your proposed syntax is unfortunately very awkward, because you can't specify the thickness and height parameters without specifying a color.
Thus I move the optional color argument after the base argument and before the optional ^
part.
\documentclass{article}
\usepackage{mathtools}
\usepackage{xcolor}
\NewDocumentCommand{\coverbracket}{O{1pt} O{0.7ex} m o e{^}}{{%
\IfNoValueTF{#4}{%
\overbracket[#1][#2]{#3}\IfValueT{#5}{^{#5}}%
}{%
\colorlet{overbracketcolor}{.}%
\mathcolor{#4}{%
\overbracket[#1][#2]{\mathcolor{overbracketcolor}{#3}}\IfValueT{#5}{^{#5}}%
}%
}%
}}
\begin{document}
Hello, $\coverbracket{x^2-2x+1}^{(x-1)^2}$
and $\coverbracket[2pt]{x^2-2x+1}^{(x-1)^2}$
with color... $\coverbracket{x^2-2x+1}[cyan]^{(x-1)^2}$
with color... $\coverbracket[2pt]{x^2-2x+1}[cyan]^{(x-1)^2}$
{\color{green!70!blue} Hello $\coverbracket{x^2-2x+1}[cyan]^{(x-1)^2}$}
\bigskip
Bad: $\overbracket{xxxxx}+y$
Good: $\coverbracket{xxxxx}+y$
\end{document}
I also enclose the whole construction in a braced group (see the initial {{
and final }}
): the reason should be apparent from looking at the last two lines and checking the spaces around +
, which are incorrect with \overbracket
, but correct with \coverbracket
.

One last thing: I changed the default 2pt for the thickness into a less heavy 1pt.
Key-value syntax
You can use a single optional argument. Here an unknown key is taken as a color name. However, color=
is necessary if you hide the color in a command.
If you frequently mix colors, you might want to abbreviate the key names, say to C
, T
and H
.
\documentclass{article}
\usepackage{mathtools}
\usepackage{xcolor}
\NewDocumentCommand{\coverbracket}{O{}me{^}}{%
{\coverunderbracket{#1}{#2}{#3}{\overbracket}{\sp}}%
}
\NewDocumentCommand{\cunderbracket}{O{}me{_}}{%
{\coverunderbracket{#1}{#2}{#3}{\underbracket}{\sb}}%
}
\ExplSyntaxOn
\NewDocumentCommand{\coverunderbracket}{mmmmm}
{
\mika_bracket:nnnNN {#1} {#2} {#3} #4 #5
}
\keys_define:nn {mika/bracket}
{
color .str_set_e:N = \l_mika_bracket_color_str,
color .initial:n = .,
thickness .dim_set:N = \l_mika_bracket_thickness_dim,
thickness .initial:n = 1pt,
height .tl_set:N = \l_mika_bracket_height_tl,
height .initial:n = 0.7ex,
unknown .code:n = \tl_set_eq:NN \l_mika_bracket_color_str \l_keys_key_str
}
\cs_new_protected:Nn \mika_bracket:nnnNN
{% #1 = options, #2 = main formula,
% #3 = sup/sub, #4 = \overbracket or \underbracketcommand,
% #5 = \sp or \sb
\keys_set:nn {mika/bracket} {#1}
\colorlet{main_color}{.}
\mathcolor{\l_mika_bracket_color_str}
{
#4
[\l_mika_bracket_thickness_dim]
[\l_mika_bracket_height_tl]
{\mathcolor{main_color}{#2}}
\tl_if_novalue:nF {#3} {#5{#3}}
}
}
\ExplSyntaxOff
\newcommand{\obcolor}{cyan}
\begin{document}
Hello, $\coverbracket{x^2-2x+1}^{(x-1)^2}$
and $\coverbracket[thickness=2pt,height=1ex]{x^2-2x+1}^{(x-1)^2}$
with color... $\coverbracket[color=\obcolor]{x^2-2x+1}^{(x-1)^2}$
with color... $\coverbracket[cyan,thickness=2pt]{x^2-2x+1}^{(x-1)^2}$
{\color{green!70!blue} Hello $\coverbracket[color=cyan]{x^2-2x+1}^{(x-1)^2}$}
\bigskip
Hello, $\cunderbracket{x^2-2x+1}_{(x-1)^2}$
and $\cunderbracket[thickness=2pt,height=1ex]{x^2-2x+1}_{(x-1)^2}$
with color... $\cunderbracket[color=cyan]{x^2-2x+1}_{(x-1)^2}$
with color... $\cunderbracket[color=cyan,thickness=2pt]{x^2-2x+1}_{(x-1)^2}$
{\color{green!70!blue} Hello $\cunderbracket[color=cyan]{x^2-2x+1}_{(x-1)^2}$}
\bigskip
Bad: $\overbracket{xxxxx}+y$
Good: $\coverbracket{xxxxx}+y$
\end{document}

e
argument type, with\NewDocumentCommand\foo{ e{_^} }
#1
is an optional argument that has data if_{...}
is used, and#2
is an optional argument that has data if^{...}
is used.xparse
documentation contains a nice summary (section 2.7 Embellishments).