TikZ-根据形状大小/锚点改变节点定位

TikZ-根据形状大小/锚点改变节点定位

我一般的问题是如何确保一个形状的东锚点和另一个形状的西锚点之间的最小 x 距离(如果有帮助,假设形状是 y 对齐的),或者类似地,南北锚点之间的最小 y 距离。

我更具体的需求是流程图,其中文本框(矩形)的文本内容可以有相当大的变化,我希望它们之间的间距不小于某个值,如果需要,可以通过推动中心节点来做到这一点。矩形的边框本身有几个宽度选项,但问题是(中心)节点位置忽略了矩形的宽度,这可能导致矩形重叠(并且也会弄乱它们之间的箭头)。

TikZ:根据文本宽度定位节点?可能会通过调用 pgf 来测量文本的大小来暗示我的特殊需求的解决方案,但是(1)我未能根据我的需要进行定制,(2)它似乎只适用于简单的文本标签(不是一般的“测量你的锚点位置”的东西,甚至不是自动文本流测量),(3)它是从 2011 年开始的,所以 TikZ 也许从那时起就发生了变化。

示例:(node distance选项与当前的行为无关,但它使问题出现在更现实的文本长度中)

\documentclass[tikz]{standalone}

\begin{document}

\tikzstyle{block} = [draw, rectangle]

\begin{tikzpicture}[node distance=3cm]
    \node [block] (A) {A};
    \node [block, right of=A] (B) {B};
    \node [block, right of=B] (C) {C};
    \draw[->] (A) edge node[above] {AB} (B);
    \draw[->] (B) edge node[above] {BC} (C);
\end{tikzpicture}

\begin{tikzpicture}[node distance=3cm]
    \node [block] (A) {A};
    \node [block, right of=A] (B) {Text in B};
    \node [block, right of=B] (C) {C};
    \draw[->] (A) edge node[above] {AB} (B);
    \draw[->] (B) edge node[above] {BC} (C);
\end{tikzpicture}

\begin{tikzpicture}[node distance=3cm]
    \node [block] (A) {A};
    \node [block, right of=A] (B) {Very long text in position B};
    \node [block, right of=B] (C) {C};
    \draw[->] (A) edge node[above] {AB} (B);
    \draw[->] (B) edge node[above] {BC} (C);
\end{tikzpicture}

\begin{tikzpicture}[node distance=3cm]
    \node [block] (A) {A};
    \node [block, right of=A] (B) {Very very long text in position B};
    \node [block, right of=B] (C) {C};
    \draw[->] (A) edge node[above] {AB} (B);
    \draw[->] (B) edge node[above] {BC} (C);
\end{tikzpicture}

\end{document}

输出:文本框的中心位置不变

答案1

您可以使用eqparbox来实现此效果。请使用该postioning库并注意\tikzstyle已被弃用。

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\usepackage{eqparbox}
\newbox\eqnodebox
\tikzset{equal size/.style={execute at begin
    node={\setbox\eqnodebox=\hbox\bgroup},
    execute at end node={\egroup\eqmakebox[#1][c]{\copy\eqnodebox}}},
    equal size/.default=A,}
\begin{document}

\tikzset{block/.style=draw, rectangle}

\begin{tikzpicture}[node distance=2cm]
    \node [block] (A) {A};
    \node [block, right=of A,equal size=B] (B) {B};
    \node [block, right=of B] (C) {C};
    \draw[->] (A) edge node[above] {AB} (B);
    \draw[->] (B) edge node[above] {BC} (C);
\end{tikzpicture}

\begin{tikzpicture}[node distance=2cm]
    \node [block] (A) {A};
    \node [block, right=of A,equal size=B] (B) {Text in B};
    \node [block, right=of B] (C) {C};
    \draw[->] (A) edge node[above] {AB} (B);
    \draw[->] (B) edge node[above] {BC} (C);
\end{tikzpicture}

\begin{tikzpicture}[node distance=2cm]
    \node [block] (A) {A};
    \node [block, right=of A,equal size=B] (B) {Very long text in position B};
    \node [block, right=of B] (C) {C};
    \draw[->] (A) edge node[above] {AB} (B);
    \draw[->] (B) edge node[above] {BC} (C);
\end{tikzpicture}

\begin{tikzpicture}[node distance=2cm]
    \node [block] (A) {A};
    \node [block, right=of A,equal size=B] (B) {Very very long text in position B};
    \node [block, right=of B] (C) {C};
    \draw[->] (A) edge node[above] {AB} (B);
    \draw[->] (B) edge node[above] {BC} (C);
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容