0

I would like to draw a brace under part of a mathematical expression. I can do it using the command \braceB defined as follows:

\documentclass[parskip]{scrartcl}

\usepackage{tikz}

\usetikzlibrary{decorations.pathreplacing} \usetikzlibrary{positioning}

% Brace under an expression: % #1: text of the expression % #2: comment \newcommand{\braceB}[3]{\begin{tikzpicture} \node(expr){#1};\ \draw[decorate, decoration={brace,amplitude=2mm,mirror,raise=-1mm}] (expr.south west) -- (expr.south east); \node[below=1mm of expr] (textnode) {\mbox{#2}}; \end{tikzpicture}}

\begin{document}

$1+2+\braceB{3+4}{Text under 3+4}+5+6$

\end{document}

My problem with this option is that:

  1. the text above the brace (3+4) is not aligned with the rest of the mathematical expression (1+2+ and +5+6);
  2. the length of the text under the brace "pushes" the rest of the expression left or right. More precisely, there are horizontal spaces between "1+2+" and "3" and between "4" and "+5+6" that I would like to remove.

Do you know how to solve these two problems?

Zach
  • 653
  • 6
  • 10

1 Answers1

2

If you really want to do that in TikZ, maybe to add some drawings under the brace, as a starting point, you must use [baseline=(expr.base)] to allow your text to be correctly aligned with the text. Then lies another issue, which is the length of the text under the brace which is icluded into the tikzpicture. To avoid that, you may creat another tikzpicture with [remember picture,overlay] to draw the text node under the brace.

underbrace node in text

Here's the code:

\documentclass[parskip]{scrartcl}

\usepackage{tikz}

\usetikzlibrary{decorations.pathreplacing} \usetikzlibrary{positioning}

% Brace under an expression: % #1: text of the expression % #2: comment \newcommand{\braceB}[2]{% \begin{tikzpicture}[baseline=(expr.base),remember picture] \node(expr){#1};\ \draw[decorate, decoration={brace,amplitude=2mm,mirror,raise=-1mm}] (expr.south west) -- (expr.south east); \end{tikzpicture} \begin{tikzpicture}[remember picture,overlay] \node[below=1mm of expr] (textnode) {\mbox{#2}}; \end{tikzpicture}}

\begin{document}

$1+2+\braceB{$3+4$}{Very long text under 3+4} +5+6$

\end{document}

May need to be compiled multiple times.

SebGlav
  • 19,186
  • Perfect, thanks! Do you have any idea why it returns an error in beamer? (Something wrong perhaps missing \item) – Zach Nov 30 '21 at 09:05
  • It should not return an error. Try using only this snippet in a slide. Could be from another aprt of your code. – SebGlav Nov 30 '21 at 15:04
  • Ok strange. If I take your example and replace scrartcl by beamer, I get the error. – Zach Feb 01 '22 at 16:42
  • 1
    No more error if I remove "\" after "{#1};". – Zach Feb 01 '22 at 16:49