2

I would like to use underbrace in order to clarify some text and after some searching I found a solution using the Tikz package. However I am having some trouble with the vertical alignment. Here is a MWE of illustrating my problem:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{decorations.pathreplacing}

\tikzstyle{underbrace style}=[decorate,decoration={brace,raise=0.5mm,amplitude=3pt,mirror,pre=moveto,pre length=1pt,post=moveto,post length=1pt}]
\tikzstyle{underbrace text style}=[font=\tiny, below, pos=.5, yshift=-1mm]

\newcommand{\tikzunderbrace}[2]{\draw [underbrace style] (#1.south west) -- (#1.south east) node [underbrace text style] {#2};}

\begin{document}

Begining text
\begin{tikzpicture}
\matrix[matrix of nodes, inner sep=0pt, column sep=0pt, nodes={anchor=south}, text height=1.5ex,text depth=.25ex]{
\node (text) {some underlined text};\\
};
\tikzunderbrace{text}{explanation}
\end{tikzpicture}
and some more text

\end{document}

Here is the result that I get:

enter image description here

As you can see, the text is vertically not aligned. After searching some more, I realized that I have to add an option to the tikzpicture environment.

I tried the following option [baseline={(text.south)}] which gave me the following result:

enter image description here

as well as [baseline={([yshift={-\ht\strutbox}]text.north)}], which gave me the following result:

enter image description here

As you can see, in both cases the alignment is not good. I would like the entire text to appear on the same line.

Could someone please help me out with what parameters I need to use in order to achieve proper vertical alignment? Thank you very much in advance.

PS. I am using the matrix environment because my actual document is more complicated than what I am showing here. I've just stripped it down for the case of a MWE.

7
  • 1
    Try baseline={(text.base)}
    – Wulle
    Commented Mar 25, 2021 at 17:01
  • @jopeto do you need the tikzpicture environment? Otherwise the amsmath package and underbrace will do the job.
    – Excelsior
    Commented Mar 25, 2021 at 17:04
  • @Wulle Thank you very much! That works perfectly!
    – jopeto
    Commented Mar 25, 2021 at 17:21
  • @Excelsior Yes, I am aware of the underbrace command in the amsmath module, however for my actual example and not the MWE, it cannot meet my needs. Thank you.
    – jopeto
    Commented Mar 25, 2021 at 17:22
  • I think that tikzmark library provides a more legible solution, and allows you to mark multiple nodes with content that you refer to afterwards. We don't know exactly your project requirements but I suggest that you take a look at it. See my solution below.
    – SebGlav
    Commented Mar 25, 2021 at 21:59

3 Answers 3

1
\documentclass{article}
\usepackage{amsmath}
\newcommand{\explan}[2]{$\underbrace{\text{#1}}_\text{#2}$}

\begin{document}

Begining text \explan{some underlined text}{explanation} and some more text

\end{document}

enter image description here

1
  • Thank you! For this MWE you solution works perfectly, however it does not meet my needs for the full-blown document that I have. Thank you anyway.
    – jopeto
    Commented Mar 25, 2021 at 17:24
1

tikzmark underbrace

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usetikzlibrary{matrix}
\usetikzlibrary{decorations.pathreplacing}

\tikzstyle{underbrace style}=[decorate,decoration={brace,raise=0.5mm,amplitude=3pt,mirror,pre=moveto,pre length=1pt,post=moveto,post length=1pt}]
\tikzstyle{underbrace text style}=[font=\tiny, below, pos=.5, yshift=-1mm]

\newcommand{\tikzunderbrace}[2]{\draw [underbrace style] (#1.south west) -- (#1.south east) node [underbrace text style] {#2};}

\begin{document}

You should use  \tikzmarknode{N1}{the tikzmark library} to underbrace your text.

\begin{tikzpicture}[overlay,remember picture]
    \tikzunderbrace{N1}{explanation}
\end{tikzpicture}

\end{document}

Please note that you will have to compile at least twice in order to get your picture correctly positionned.

4
  • Yes, this solution works fine and seems to very elegant. However if the text after the underbrace is longer and wraps around to the next line, the next line goes over the underbrace. What is the way to make latex recognize the underbrace and leave enough space to the next line, so that the next line does not overlap with the underbrace?
    – jopeto
    Commented Mar 26, 2021 at 8:15
  • What you're asking for in this comment is far from easy to implement. You may want to ask this in a new topic, once you got your prefered solution here (the one you will accept).
    – SebGlav
    Commented Mar 26, 2021 at 17:42
  • 1
    Thank you for your reply. I think that for now I will go with the tikzpicture version, since it automatically gives me the required spacing for the next line. Apart from that you solution seems to be a lot easier and concise.
    – jopeto
    Commented Mar 29, 2021 at 7:11
  • Thanks. Feel free to accept your preferd solution, so ;)
    – SebGlav
    Commented Mar 29, 2021 at 17:21
1

enter image description here

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{decorations.pathreplacing}

\tikzstyle{underbrace style}= [decorate,decoration={brace,raise=0.5mm,amplitude=3pt,mirror,pre=moveto,pre length=1pt,post=moveto,post length=1pt}]
\tikzstyle{underbrace text style}=[font=\tiny, below, pos=.5, yshift=-1mm]

\newcommand{\tikzunderbrace}[2]{\draw [underbrace style] (#1.south west) -- (#1.south east) node [underbrace text style] {#2};}

\begin{document}
    
    Begining text
    \begin{tikzpicture}[baseline=-0.12cm]
        \matrix[matrix of nodes, inner sep=0pt, column sep=0pt, ]{
            \node (text) {some underlined text};\\
        };
        \tikzunderbrace{text}{explanation}
    \end{tikzpicture}
    and some more text
    
\end{document}
1
  • Thank you! Where does the 0.12cm come from though? I think a better solution would be the one provided by @wulle and use {(text.base)}.
    – jopeto
    Commented Mar 26, 2021 at 7:41

You must log in to answer this question.

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