TikZ:水平移动节点文本

TikZ:水平移动节点文本

将节点的文本West稍微向左移动,以便节点的文本West和之间有合理的间隙,最直接/惯用的方法是什么East?我想保留节点的文本East原处,而对于所有箭头保持其当前方向(我没有找到这个问题尽管标题如此,但特别有用。)

在此处输入图片描述

我想我可以使用xshift,如上所述这里,但我无法让它工作。可能是因为West该节点是相对于其他节点放置的?

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[node distance=1.5cm, auto]
  \node (West) {
    $\Gamma \vdash A, B, C, D, E, F, G$
  };
  \node (North) [right of=West, above of=West] {
    $\Gamma’$
  };
  \node (South) [below of=West, right of=West] {
    $\Delta’$
  };
  \node (East) [right of=North, below of=North] {
    $\Delta \vdash I, J, K, L, M$
  };
  \draw[->] (West) to node [yshift=-1ex] {$f$} (North);
  \draw[->] (West) to node [yshift=1ex,swap] {$g$} (South);
  \draw[dotted,->] (North) to node [yshift=-1ex] {$f’$} (East);
  \draw[dotted,->] (South) to node [yshift=1ex,swap] {$g’$} (East);
\end{tikzpicture}
\end{document}

答案1

只需添加\hspace{-2em}到节点文本即可West

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[node distance=1.5cm, auto]
  \node (West) {
    \hspace{-2em}$\Gamma \vdash A, B, C, D, E, F, G$
  };
  \node (North) [right of=West, above of=West] {
    $\Gamma'$
  };
  \node (South) [below of=West, right of=West] {
    $\Delta'$
  };
  \node (East) [right of=North, below of=North] {
    $\Delta \vdash I, J, K, L, M$
  };
  \draw[->] (West) to node [yshift=-1ex] {$f$} (North);
  \draw[->] (West) to node [yshift=1ex,swap] {$g$} (South);
  \draw[dotted,->] (North) to node [yshift=-1ex] {$f'$} (East);
  \draw[dotted,->] (South) to node [yshift=1ex,swap] {$g'$} (East);
\end{tikzpicture}
\end{document}

答案2

如果您不想移动节点本身(因为这会移动箭头),那么您必须尝试为节点文本添加空间(左侧为负数或右侧为正数),或者您必须添加另一个包含文本的节点。第二种选择的优点是您可以直接指定移动量,而不依赖于对齐。

一种可能性是使用以下label选项:

\begin{tikzpicture}[node distance=1.5cm, auto]
  \node [label={[xshift=-2em]center:$\Gamma \vdash A, B, C, D, E, F, G$}] (West) {\phantom{$\Gamma \vdash A, B, C$}};
  \node (North) [right of=West, above of=West] {
    $\Gamma'$
  };
  \node (South) [below of=West, right of=West] {
    $\Delta'$
  };
  \node (East) [right of=North, below of=North] {
    $\Delta \vdash I, J, K, L, M$
  };
  \draw[->] (West) to node [yshift=-1ex] {$f$} (North);
  \draw[->] (West) to node [yshift=1ex,swap] {$g$} (South);
  \draw[dotted,->] (North) to node [yshift=-1ex] {$f'$} (East);
  \draw[dotted,->] (South) to node [yshift=1ex,swap] {$g'$} (East);
\end{tikzpicture}

label与选项一起使用center可在原始节点的同一位置添加第二个节点。然后可以使用 移动此节点xshift。请注意,原始节点的文本不应为空,否则箭头fg起点与标签文本太近。phantom原始节点上的文本可确保正确的距离。

在此处输入图片描述

答案3

我回来了 :-)。 经过重新思考,在所需图像中应该保留什么以及应该移动什么之后,我决定再次编辑我的答案...

编辑(3): 由于这个问题对我来说并不清楚,在之前的答案中(我删除了,因为它们对你没有帮助),我再次从你的解决方案猜测你想要得到什么。以下是我的(最后)建议 :-)。

我对您的答案的分析表明,它并没有正确使用 TikZ。让我们看看您的解决方案真正获得了什么:

[![enter image description here][1]][1]

为了得到上面的图片,我只添加了节点选项draw=red,可以看到,西节点中的文本从中突出。我只想指出,这不是好的做法,应该避免。

可能的解决方案: 经过长时间的猜测,我假设您希望获得这样的结果:箭头(从北节点和南节点看)与西节点的东北角相交的距离与东节点与西北角相交的距离相同,即箭头的斜率将保持不变。获得此属性有更多可能性。例如:

  1. 猜测这个距离,然后使用它作为

    \draw ([xshift=-guessed distance>] West.north east) to node ... (North);

  2. 测量这个距离

  3. 其他解决方案(我没有想到)

对于第二种可能性,可以利用三个 TikZ 库:calcintersectionspositioning。最后一个用于正确使用节点或坐标放置语法(right=of ...而不是right of = ...). With help交点,确定北节点箭头与东节点边界的交点,并calc计算从西节点和东节点之间的中点到该交点的距离(仅在 x 方向):

\documentclass[border=3mm,tikz]{standalone}
    \usetikzlibrary{calc,intersections,positioning}

    \begin{document}
\begin{tikzpicture}[
    node distance=1.5cm, 
                    ]
\node (West) {$\Gamma \vdash A, B, C, D, E, F, G$};
\node (East) [right=4mm% <-- distance between nodes West and East
                        of West] {$\Delta \vdash I, J, K, L, M$};
% coordinate in the middle of the West and EAST node
\coordinate (a) at ($(West.east)!0.5!(East.west)$);
%
\node (North) [above=of a] {$\Gamma'$};
\node (South) [below=of a] {$\Delta'$};
% determination of intersection between arrow from North to East
% with node East
    \path[name path=curve 1] (East.north west) -- (East.north);% auxiliary path
    \path[name path=curve 2] (North) -- (East);% auxiliary path
    \path[name intersections={of=curve 1 and curve 2, by={b}}] (b);
% caluculation of distance to intersection
\path   let \p1 = (a),
            \p2 = (b |- a),
            \n1 = {veclen(\y2-\y1,\x2-\x1)} in
        coordinate[left=\n1 of a ] (c);
% arrows
\draw[->]   (c |- West.north) to node[above] {$f$} (North) ;
\draw[->]   (c |- West.south) to node[below] {$g$}  (South) ;
\draw[dotted,->]    (North) to node[above] {$f'$}  (East);
\draw[dotted,->]    (South) to node[below] {$g'$}  (East);%,
\end{tikzpicture}
    \end{document}

通过这种解决方案,节点之间的箭头的斜率保持其斜率,西节点和东节点之间的距离可以明确设置,北节点和西节点始终位于西节点和东节点之间中点的北面和南面。

下面是两张图,第一张西节点和东节点之间的距离为 4 毫米,第二张为 16 毫米。上面的 MWE 是第一张图的。

在此处输入图片描述在此处输入图片描述

相关内容