6

How to create a new command with ^ or _ instead of [][][]{}{}? to be coherent/analogous with \overbracket{}^{}

Creating a new command for give color to \overbracket (mathtools package), I would like to write $\coverbracket[cyan]{x^2-2x+1}^{(x-1)^2}$ instead of $\coverbracket[cyan]{x^2-2x+1}{(x-1)^2}$

enter image description here

How could you code that?

\documentclass[14pt]{article}
\usepackage{mathtools}
\usepackage{xcolor}

\NewDocumentCommand{\coverbracket}{o O{2pt} O{0.7ex} m m}{
    \IfNoValueTF{#1}
    {\overbracket[#2][#3]{#4}^{#5}}
    {{\color{#1}\overbracket[#2][#3]{{\normalcolor{#4}}}^{#5}}}
}

\begin{document}
Hello, $\overbracket{x^2-2x+1}^{(x-1)^2}$

with color... $\coverbracket[cyan]{x^2-2x+1}{(x-1)^2}$
  
\end{document}

P.D.- It´s a pity that many usefull packages have no color option. Example: mathtools, ulem, ...

3
  • 1
    Sorry to say this but did you read the manual?, e.g. usrguide.pdf, see the 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.
    – daleif
    Commented Aug 21 at 7:31
  • @daleif , thank you, where is the manual?. I read a little PDFs but not a manual.
    – Mika Ike
    Commented Aug 21 at 8:18
  • 1
    @MikaIke: The xparse documentation contains a nice summary (section 2.7 Embellishments).
    – Werner
    Commented Aug 21 at 17:07

4 Answers 4

6

Use the e argument type:

\NewDocumentCommand{\coverbracket}{o O{2pt} O{0.7ex} m e{^}}{
    \IfNoValueTF{#1}
    {\overbracket[#2][#3]{#4}^{#5}}
    {{\color{#1}\overbracket[#2][#3]{{\normalcolor{#4}}}^{#5}}}
}

Then the new syntax of usage changes to:

$\coverbracket[cyan]{x^2-2x+1}^{(x-1)^2}$

Related post: Usage of e and E argument types in xparse?

5
  • I'm not sure that is the syntax the user was after
    – daleif
    Commented Aug 21 at 7:37
  • @daleif The OP said: "I would like to write $\coverbracket[cyan]{x^2-2x+1}^{(x-1)^2}$ instead of $\coverbracket[cyan]{x^2-2x+1}{(x-1)^2}$"
    – Stephen
    Commented Aug 21 at 7:39
  • Right my bad, but I still think one ought to be testing for the ^ argument before adding it.
    – daleif
    Commented Aug 21 at 7:41
  • @daleif both answers are fine for my question, I think. Thank you.
    – Mika Ike
    Commented Aug 21 at 8:29
  • @Stephen both answers are fine for my question, I think. Thank you.
    – Mika Ike
    Commented Aug 21 at 8:29
8

I don't like the ^ or _ notations for \overbrace and similar commands. Anyway, you can use it thanks to the embellishment 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.

output

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}

output with key-value

4

Here is a use of the e type, as you're using \overbracket I've only added ^. I also add a test for contents on the ^ var, such that we avoid adding ^{} to the output when the ^ is not being used.

As for why no color support: It hasn't been something users have asked about (I personally don't see many documents that uses text with color as most people find it annoying to read). Plus it would end up giving really strange interfaces. If the macros had used key-val options to begin with, then it would probably have been easy to to add support.

\documentclass[14pt]{article}
\usepackage{mathtools}
\usepackage{xcolor}

\NewDocumentCommand{\coverbracket}{o O{2pt} O{0.7ex} m e{^}}{
    \IfNoValueTF{#1}
    {
      \overbracket[#2][#3]{#4}
      \IfNoValueF{#5}{\sp{#5}}
    }
    {
      {
        \color{#1}
        \overbracket[#2][#3]{{\normalcolor{#4}}}
        \IfNoValueF{#5}{\sp{#5}}
      }
    }
}

\begin{document}
Hello, $\overbracket{x^2-2x+1}$

 with color... $\coverbracket[cyan]{x^2-2x+1}^{(x-1)^2}$
  
\end{document}
1
\usepackage{mathtools,xcolor}

\makeatletter
% \coverbracket[<color>]{<expr>}^{<sup>}_{<sub>}  (sup/sub optional, in any order)
\newcommand{\coverbracket}[2][]{%
  \begingroup\def\cb@col{#1}%
  {\color{\cb@col}\overbracket{#2}}%
  \cb@next
}
\def\cb@next{%
  \@ifnextchar^{\cb@sup}{\@ifnextchar_{\cb@sub}{\endgroup}{\endgroup}}}
\def\cb@sup^#1{{}^{\color{\cb@col}{#1}}\cb@next}
\def\cb@sub_#1{{}_{\color{\cb@col}{#1}}\cb@next}
\makeatother

use like:

$\coverbracket[cyan]{x^2-2x+1}^{(x-1)^2}$

$\coverbracket[teal]{x^2-2x+1}_{\text{disc.}}^{(x-1)^2}$

New contributor
simon Raphael is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
2
  • 1
    pls use "code block", and make your code complete and compilable instead of just post "snippets".
    – Explorer
    Commented 2 days ago
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented 2 days ago

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.