2

I have this code.

\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows}
\usepackage{float}
\usepackage{mathtools}
\begin{document}

\[
\left|\underbrace{-6rO^2\left(\frac{ G_p}{r}\right)-3O^2\left(\frac{ G_p}{r}\right)}_{O^1\left(\frac{ G_p}{r}\right)}\right|
\end{document}
\]

Im trying to make that the underbrace appear outside the delimiter $||$ but if I colocate the underbrace outside the delimiter, like this

\[
\underbrace{\left|-6rO^2\left(\frac{ G_p}{r}\right)-3O^2\left(\frac{ G_p}{r}\right)}_{O^1\left(\frac{ G_p}{r}\right)\right|}
\]

I get an error. How to solve it?

4 Answers 4

4

You have to pay attention to proper nesting of braces and \left ... \right pairs, your second example is incorrect in this sense. Just avoid using \left ... \right since they are evil, and you'll be fine:

enter image description here

\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows}
\usepackage{float}
\usepackage{mathtools}
\begin{document}

\[
\underbrace{\biggl|-6rO^2\Bigl(\frac{ G_p}{r}\Bigr)-3O^2\Bigl(\frac{G_p}{r}\Bigr)\biggr|}_{O^1\bigl(\frac{ G_p}{r}\bigr)}
\]

\end{document}

Btw, if I just change \left ... \right to either \biggl ... \biggr or \Bigl ... \Bigr or \bigl ... \bigr depending on how large delimiters I want, it's immediately obvious where is the mistake:

enter image description here

\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows}
\usepackage{float}
\usepackage{mathtools}
\begin{document}

\[
\underbrace{\biggl|-6rO^2\Bigl(\frac{ G_p}{r}\Bigr)-3O^2\Bigl(\frac{G_p}{r}\Bigr)}_{O^1\bigl(\frac{ G_p}{r}\bigr)\biggr|}
\]

\end{document}
2
  • What do you mean @tohecz with "evil" when you refer to the \left and \right delimiters?
    – JuanMuñoz
    Commented Oct 21, 2014 at 1:45
  • @JuanMuñoz Unfortunately there is no canonical post about it, but some insight can be found in: tex.stackexchange.com/questions/2607/… and tex.stackexchange.com/questions/5612/… The biggest problems are: (1) incorrect spacing (can be fixed), (2) the inside of the \left...\right pair is a mathinner atom, which means that linebreaks are prevented, and spaces are fixed in lengths instead of being glues (allowing small stretch and shrink).
    – yo'
    Commented Oct 21, 2014 at 9:45
2

If you need flexibility in how the brace is drawn then you could use tikz as well:

enter image description here

Code:

\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows,calc,decorations.pathreplacing}
\usepackage{mathtools}
\usepackage{xparse}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}

% Tweak these as necessary
\newcommand*{\BraceAmplitude}{0.4em}%
\newcommand*{\VerticalOffset}{1.0ex}%  
\newcommand*{\HorizontalOffset}{0.0em}% 


\NewDocumentCommand{\InsertUnderBrace}{%
    O{} % #1 = draw options
    O{yshift=0.0cm} % #2 = optional brace shift options
    m   % #3 = left tikzmark
    m   % #4 = right tikzmark
    m   % #5 = text to place under brace
}{%
    \begin{tikzpicture}[overlay,remember picture]
    \draw [decoration={brace, amplitude=\BraceAmplitude}, decorate, thick, draw=blue, text=black, #1]
        ([#2]$(#4)+( \HorizontalOffset,-\VerticalOffset)$) -- 
        ([#2]$(#3)+(-\HorizontalOffset,-\VerticalOffset)$)
        node [below=\VerticalOffset, midway, align=center] {#5};
    \end{tikzpicture}%
}%

\NewDocumentCommand{\AbsWithUnderBrace}{%
    s%               #* = draw brace on outside
    O{}%             #2 = draw options
    O{yshift=0.0cm}% #3 = optional brace shift options
    m%               #4 = content
    m%               #4 = underbrace content
}{%
    \IfBooleanTF{#1}{%
        \tikzmark{StartBraceA}\left|#4\right|\tikzmark{EndBraceA}
    }{%
        \left|\tikzmark{StartBraceA}#4\tikzmark{EndBraceA}\right|
    }%
    \InsertUnderBrace[draw=red, ultra thick, text=blue][yshift=-1.5ex]{StartBraceA}{EndBraceA}
        {$O^1\left(\frac{ G_p}{r}\right)$}%
}

\begin{document}
\[
    \AbsWithUnderBrace*[draw=red, ultra thick, text=blue][yshift=-1.5ex]
        {-6rO^2\left(\frac{ G_p}{r}\right)-3O^2\left(\frac{ G_p}{r}\right)}
        {$O^1\left(\frac{ G_p}{r}\right)$}
\qquad
    \AbsWithUnderBrace[draw=red, ultra thick, text=blue][yshift=-1.5ex]
        {-6rO^2\left(\frac{ G_p}{r}\right)-3O^2\left(\frac{ G_p}{r}\right)}
        {$O^1\left(\frac{ G_p}{r}\right)$}
\]
\end{document}
2

A few possible improvements to previous (Tohecz's) solution: since you load mathtools (in that case, needless to load amsmath), you can define an \abs command that can have either an optional argument: \big, \Big and the like, or a star version that implicitly adds a pair of \left…\right. This will result in a cleaner syntax.

You also can use the \underbracket command, with an adjustable thickness.

Demo:

    \documentclass[12pt,a4paper]{article}

    \usepackage[utf8]{inputenc}
    \usepackage[spanish]{babel}
    \usepackage{amsfonts}
    \usepackage{amssymb}
    \usepackage{mathtools}
    \usepackage{etoolbox}


    \DeclarePairedDelimiterX{\abs}[1]{\lvert}{\rvert}{\ifblank{#1}{\: · \:}{#1}}
    \begin{document}

    \[
    \underbrace{\abs[\Big]{-6rO^2\Bigl(\frac{ G_p}{r}\Bigr)-3O^2\Bigl(\frac{G_p}{r}\Bigr)}}_{O^1\bigl(\frac{ G_p}{r}\bigr)}
    \qquad
    \underbracket[0.6pt]{\abs[\Big]{-6rO^2\Bigl(\frac{ G_p}{r}\Bigr)-3O^2\Bigl(\frac{G_p}{r}\Bigr)}}_{O^1\bigl(\frac{ G_p}{r}\bigr)}
    \]
    \[
    \underbrace{\abs*{-6rO^2\Bigl(\frac{ G_p}{r}\Bigr)-3O^2\Bigl(\frac{G_p}{r}\Bigr)}}_{O^1\bigl(\frac{ G_p}{r}\bigr)}
    \qquad
    \underbracket[0.6pt]{\abs*{-6rO^2\Bigl(\frac{ G_p}{r}\Bigr)-3O^2\Bigl(\frac{G_p}{r}\Bigr)}}_{O^1\bigl(\frac{ G_p}{r}\bigr)}
    \]
    \end{document} 

enter image description here

1

You want to typeset only the main part without the subscript and the underbrace to get the required height of the delimiters, which can be done with \vphantom.

The syntax of \dunderbrace is

\dunderbrace<left><body>_<subscript><right>

where <left> is the left delimiter (in your case |), <body> is to be underbraced, <subscript> is what goes under the brace and <right> is the right delimiter.

Note that _<subscript> can be omitted.

\documentclass[a4paper]{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mleftright}
\mleftright

\usepackage{lipsum} % just for the example

\makeatletter
\newcommand{\dunderbrace}[2]{%
  \@ifnextchar_{\dunder@next{#1}{#2}}{\dunder@next{#1}{#2}_{}}%
}
\def\dunder@next#1#2_#3#4{%
  \left#1\vphantom{\displaystyle#2}\right.\n@space
  \underbrace{#2}_{#3}%
  \n@space\left.\vphantom{#2}\right#4%
}
\makeatother

\begin{document}

\lipsum*[2]
\[
\dunderbrace|{-6rO^2\left(\frac{G_p}{r}\right)-3O^2\left(\frac{G_p}{r}\right)}
  _{O^1\left(\frac{G_p}{r}\right)}|
\]
\lipsum[3]

\end{document}

enter image description here

If your aim is that the underbrace also cover the delimiters, then it's much easier:

\[
\underbrace{\left|-6rO^2\left(\frac{G_p}{r}\right)-3O^2\left(\frac{G_p}{r}\right)\right|}
 _{O^1\left(\frac{G_p}{r}\right)}
\]

enter image description here

1
  • I suppose @egreg that this solution atack the problem from a point of view that can be generalized to other cases, however it is a very complicate solution to solve my simple answer that requires a lot use of package.
    – JuanMuñoz
    Commented Oct 21, 2014 at 1:48

You must log in to answer this question.

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