我有这个代码。
\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}
\]
我试图让下括号出现在分隔符之外$||$
,但如果我将下括号放在分隔符之外,就像这样
\[
\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|}
\]
我收到错误。该如何解决?
答案1
您必须注意括号和\left ... \right
括号对的正确嵌套,从这个意义上讲,您的第二个示例是不正确的。只要避免使用它们,\left ... \right
因为它们是邪恶的,您就会没事:
\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}
顺便说一句,如果我只是根据我想要的分隔符的大小将其更改\left ... \right
为\biggl ... \biggr
或\Bigl ... \Bigr
或\bigl ... \bigr
,那么错误在哪里就会立即显现出来:
\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
如果您需要灵活地绘制括号,那么您也可以使用tikz
:
代码:
\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}
答案3
对以前的(Tohecz 的)解决方案有几个可能的改进:由于您加载mathtools
(在这种情况下,无需加载amsmath
),您可以定义一个\abs
可以具有可选参数的命令:\big
,\Big
等等,或者隐式添加一对的星号版本\left…\right
。这将导致更清晰的语法。
您还可以使用\underbracket
命令,调整厚度。
演示:
\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}
答案4
您想仅排版主要部分,而不排版下标和下括号,以获得所需的分隔符高度,这可以使用 来完成\vphantom
。
语法\dunderbrace
是
\dunderbrace<left><body>_<subscript><right>
其中<left>
,左分隔符(在您的情况下为|
),<body>
需要用下括号括起来,<subscript>
是位于括号下方的分隔符,而<right>
是右分隔符。
注意_<subscript>
可以省略。
\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}
如果您的目标是让下括号也覆盖分隔符,那么就容易多了:
\[
\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)}
\]