TikZ 中正确的分隔符高度

TikZ 中正确的分隔符高度

当使用left delimiterright delimiter选项时,我遇到的问题在于它们的高度不等于它们周围的节点。

以下是一个例子:

\documentclass{article}

\usepackage{fullpage}
\usepackage{tikz}
\usetikzlibrary{shapes, matrix}

\begin{document}

\tikzset{
  withparens/.style = {draw, outer sep=0pt,
    left delimiter=(, right delimiter=),
    align=center},
  withbraces/.style = {draw, outer sep=0pt,
    left delimiter=\{, right delimiter=\},
    align=center}
}

\begin{tikzpicture}
    \node[withparens] {make};
    \node[right=1.5cm, withparens] {make \\ node};
    \node[right=3.5cm, withparens] {make \\ node \\ higher};
\end{tikzpicture}

\vspace{2em}

\begin{tikzpicture}
    \node[withbraces] {make};
    \node[right=1.5cm, withbraces] {make \\ node};
    \node[right=3.5cm, withbraces] {make \\ node \\ higher};
\end{tikzpicture}

\end{document}

由此产生了如下结果:

分隔符高度不正确

分隔符的高度是否以某种方式近似,而用户无法控制?或者它们仅在特定高度可用,并选择最佳(最近)选项?

答案1

我查看了left delimiter和的相关代码right delimiter,它位于 $TEXMF/tex/generic/pgf/frontendlayer/tikz/libraries/tikzlibrarymatrix.code.tex并调用中\tikz@delimiter

它将一个节点放置在主节点的左侧或右侧,与下层对齐,测量节点的高度,然后执行:

$\left#6\vcenter{\hrule height .5#8 depth .5#8 width0pt}\right#7$%

其中#6#7是取决于边的分隔符(另一个是.)。这#8是框的高度。如您所见,它使用数学代码来绘制分隔符。我假设这些仅在某些尺寸下可用,因此它们并不总是合适的。这里没有什么可配置的。但是,可以重新定义相关宏来缩放分隔符,例如使用\resizebox*{!}{#8}{ .. }

\documentclass{article}

\usepackage{fullpage}
\usepackage{tikz}
\usetikzlibrary{shapes, matrix}

\makeatletter

\def\tikz@delimiter#1#2#3#4#5#6#7#8{%
  \bgroup
    \pgfextra{\let\tikz@save@last@fig@name=\tikz@last@fig@name}%
    node[outer sep=0pt,inner sep=0pt,draw=none,fill=none,anchor=#1,at=(\tikz@last@fig@name.#2),#3]
    {%
      {\nullfont\pgf@process{\pgfpointdiff{\pgfpointanchor{\tikz@last@fig@name}{#4}}{\pgfpointanchor{\tikz@last@fig@name}{#5}}}}%
      \delimitershortfall\z@% as suggested by morbusg (maximum space not covered by a delimiter = 0)
      \resizebox*{!}{#8}{$\left#6\vcenter{\hrule height .5#8 depth .5#8 width0pt}\right#7$}%
    }
    \pgfextra{\global\let\tikz@last@fig@name=\tikz@save@last@fig@name}%
  \egroup%
}
\makeatother

\begin{document}

\tikzset{
  withparens/.style = {draw, outer sep=0pt,
    left delimiter=(, right delimiter=),
    above delimiter=(, below delimiter=),
    align=center},
  withbraces/.style = {draw, outer sep=0pt,
    left delimiter=\{, right delimiter=\},
    above delimiter=\{, below delimiter=\},
    align=center}
}

\begin{tikzpicture}
    \node[withparens] {make};
    \node[right=1.5cm, withparens] {make \\ node};
    \node[right=3.5cm, withparens] {make \\ node \\ higher};
\end{tikzpicture}

\vspace{2em}

\begin{tikzpicture}
    \node[withbraces] {make};
    \node[right=1.5cm, withbraces] {make \\ node};
    \node[right=3.5cm, withbraces] {make \\ node \\ higher};
\end{tikzpicture}

\end{document}

结果

我还添加了水平分隔符,以便您可以看到它们也能起作用。

相关内容