# How can I calculate the length of the longest word in a node?

I would like to create an inline brace command \inline{} for xelatex using TikZ. The difficult part is calculating the text width for the node without hardcoding it into the tikzpicture environment.

I would like:

1. VARIABLE 1 Text width = Length of the Longest Word within node e.g. \node[decoration={brace},decoration,text centered,text width=VARIABLE1,]
2. VARIABLE 2 Distance of brace from node to be the length of the longest word plus a little extra space for aesthetics. e.g. \p1=(bracetext.north), \p2=(bracetext.south) in ($(\x1+VARIABLE2,\y1)$) -- ($(\x2+VARIABLE2,\y2)$);
3. Ideally the baseline of the node will be aligned such that the pointy part of the braces are aligned with the lines of the sentence (also for aesthetics).

\documentclass[handout]{beamer}
\usepackage{fontspec}

\usepackage{tikz}
\usetikzlibrary{chains,calc,trees,positioning,arrows,shapes.geometric,%
decorations.pathreplacing,decorations.pathmorphing,shapes,%
matrix,shapes.symbols}
\tikzset{%
decoright/.style={decoration={brace},decorate},
decoleft/.style={decoration={brace,mirror},decorate},
}
\newcommand{\inbrace}[1]{%
\tikz{
\node[decoration={brace},decoration,text centered,text width=3cm,](bracetext){\small#1};
%\draw[decotop] (bracetext.north) to (bracetext.south);
\draw[decoright] let
\p1=(bracetext.north), \p2=(bracetext.south) in
($(\x1+2.5em,\y1)$) -- ($(\x2+2.5em,\y2)$);
\draw[decoleft] let
\p1=(bracetext.north), \p2=(bracetext.south) in
($(\x1-2.5em,\y1)$) -- ($(\x2-2.5em,\y2)$);
}%
}%

\begin{document}

\begin{frame}
\frametitle{Bound Morphemes versus Free Morphemes and Bound Roots}
A \inbrace{root\\stem\\base} is a morpheme, which is more specifically a lexeme, to which we can add other morphemes in the form of affixes. In most cases, we are connecting affixes to the front (prefix) or back (suffix) of a lexeme, however, occassionally there are instances when we add them within a lexeme (infix).

\end{frame}

\end{document}


Sample Output:

• If you usually have three words like that you can use a single multipart node. Regarding the item 3 you can use \tikz[baseline=....] for adjusting the position where the picture meets the line. With all respect, it will never look good if you insist on inline version. Apr 16, 2013 at 18:57
• I am not sure what the multipart node is and I will have to read up on that. I did have a failed baseline attempt and I should have included that in my code and examples. Inline tikz is pretty important for flexibility. I think it will always look better than anything Powerpoint or Word could produce—though I am not sure what you mean here. It would be useful to have the baseline also calculated based on how many words are within the node. Apr 16, 2013 at 19:03

### Explanation:

• inner xsep = <distance between longest text and end of picture>
• raise = <inner xsep> - <distance between longest text and brace>
• The baseline option is used to align the text vertically.

## Code

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,decorations.pathmorphing}
\tikzset{%
decoright/.style={
decoration={
brace,
raise=-0.5em
},
decorate
},
decoleft/.style={
decoration={
brace,
mirror,
raise=-0.5em
},
decorate},
}
\newcommand{\inbrace}[2][middle]{%
\tikz[baseline={(#1)}]{
\node[
draw=gray,% for debug
align=center,
inner xsep=1em,
outer sep=+0pt,
font=\small,
name=bracetext
] {#2};
\draw[decoleft]
(bracetext.north west) -- coordinate (middle)
(bracetext.south west);
\draw[decoright]
(bracetext.north east) -- (bracetext.south east);
}%
}

\begin{document}
A \inbrace{root\\stem\\base\\test} or
\inbrace[{[yshift=-.5ex]middle}]{root\\stem\\base\\test}?
\end{document}


## Output

• Excellent work. Quite flexible when the quantity of words changes or the length of the words changes. The 2nd option \inbrace[{[yshift=-.5ex]middle}] looks better. Couldn't that be put in the top-matter such that the document code is "cleaner"? Also, I like how you managed to get rid of the calc library. Nicely done. Apr 17, 2013 at 12:14
• @macmadness86 Sure just use \newcommand{\inbrace}[2][{[yshift=-.5ex]middle}]{% (the { } braces are important!) or for less flexibility use only one mandatory argument and set \tikz[baseline={([yshift=-.5ex]middle)}]{. Apr 17, 2013 at 17:28

here is a solution without tikZ

\documentclass[handout]{beamer}
\usepackage{fontspec}
\newcommand\inbrace[1]{%
\raisebox{\dimexpr0.5\height+0.5ex}{$\left\{\begin{tabular}{c}#1\end{tabular}\right\}$}}
\begin{document}

\begin{frame}
\frametitle{Bound Morphemes versus Free Morphemes and Bound Roots}
A \inbrace{root\\stem\\base} is a morpheme, which is more specifically a lexeme, to which we can add other morphemes in the form of affixes. In most cases, we are connecting affixes to the front (prefix) or back (suffix) of a lexeme, however, occassionally there are instances when we add them within a lexeme (infix).

\end{frame}
\end{document}


The package delarray is very good for this, as it avoids doing any guess about the height of the material.

\documentclass[handout]{beamer}
\usepackage{delarray,array}
\newcommand{\inbrace}[1]{%
$\begin{array}[b]\{{@{}>{$}c<{$}@{}}\}#1\end{array}$%
}

\begin{document}
\begin{frame}
\frametitle{Bound Morphemes versus Free Morphemes and Bound Roots}

Something before.

A \inbrace{root\\stem\\base} is a morpheme, which is more specifically a lexeme, to which
we can add other morphemes in the form of affixes. In most cases, we are connecting affixes
to the front (prefix) or back (suffix) of a lexeme, however, occassionally there are
instances when we add them within a lexeme (infix).

\end{frame}
\end{document}


With >{$}c<{$} we are saying that the column will be in text mode rather than in math mode.