如何计算节点中最长单词的长度?

如何计算节点中最长单词的长度?

我想创建一个行内括号\inline{}使用命令。困难的部分是计算节点的文本宽度,而无需将其硬编码到环境xelatex中。TikZtikzpicture

我想:

  1. 变量 1文本宽度 = 节点内最长单词的长度,例如\node[decoration={brace},decoration,text centered,text width=VARIABLE1,]
  2. 变量 2括号与节点的距离应为最长单词的长度加上一点额外的空间以达到美观的效果。例如\p1=(bracetext.north), \p2=(bracetext.south) in ($(\x1+VARIABLE2,\y1)$) -- ($(\x2+VARIABLE2,\y2)$);
  3. 理想情况下,节点的基线将对齐,使得括号的尖头部分与句子的行对齐(也是为了美观)。

重要区域截图

\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}

示例输出: 示例输出

答案1

解释:

  • inner xsep = <distance between longest text and end of picture>
  • raise = <inner xsep> - <distance between longest text and brace>
  • baseline选项用于垂直对齐文本。

代码

\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}

输出

在此处输入图片描述

答案2

这是一个没有 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}

在此处输入图片描述

答案3

这个包装delarray非常好,因为它避免了对材料的高度进行任何猜测。

\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}

我们>{$}c<{$}说该列将处于文本模式而不是数学模式。

在此处输入图片描述

相关内容