如何获取 tikz/PGF 坐标的一个分量?

发布于 2024-08-08 09:35:22 字数 488 浏览 7 评论 0原文

我正在尝试在图表上画一条水平线。直线的 Y 坐标应位于 a 点和 b 点之间的中间(a 在 b 下方)。线的左右端点位于 tikzpicture 的边界框上。下面是我现在使用交集运算符执行此操作的方法:

\coordinate (h0) at ($(a.north)!0.5!(b.south)$);
\draw (h0 -| current bounding box.west) -- (h0 -| current bounding box.east);

这让我觉得相当迂回。我宁愿做的是获取(h0)的Y坐标和边界框东西两侧的X坐标,并自己组合坐标。我想这样做,但它不受支持的语法:

\coordinate (h0) at ($(a.north)!0.5!(b.south)$);
\draw (current bounding box.west.x,h0.y) -- (current bounding box.east.x,h0.y);

有没有办法引用我缺少的坐标的各个组件?

I'm trying to draw a horizontal line across my diagram. The Y coordinate of the line should be halfway between points a and b (a is below b). The left and right endpoints of the line are on the bounding box of the tikzpicture. Here's how I'm doing this now, using the intersection operator:

\coordinate (h0) at ($(a.north)!0.5!(b.south)$);
\draw (h0 -| current bounding box.west) -- (h0 -| current bounding box.east);

This strikes me as rather roundabout. What I'd rather do is get the Y coordinate of (h0) and the X coordinates of the east and west sides of the bounding box, and compose the coordinates myself. I'd like to do this, but it isn't supported syntax:

\coordinate (h0) at ($(a.north)!0.5!(b.south)$);
\draw (current bounding box.west.x,h0.y) -- (current bounding box.east.x,h0.y);

Is there a way to reference individual components of coordinates that I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

被你宠の有点坏 2024-08-15 09:35:22

您可以在 let 操作中获取组件。在 PGF 手册中查找它的工作原理,但从记忆中:

\draw
  let
    \p1=($(a.north)!0.5!(b.south)$),
    \p2=(current bounding box.west),
    \p3=(current bounding box.east)
  in
    (\x2,\y1) -- (\x3, \y1);

这可能需要调试... 编辑:现在感谢提问者。

You can get at the components inside a let operation. Look it up in the PGF manual for the works, but from memory:

\draw
  let
    \p1=($(a.north)!0.5!(b.south)$),
    \p2=(current bounding box.west),
    \p3=(current bounding box.east)
  in
    (\x2,\y1) -- (\x3, \y1);

That'll probably need debugging... EDIT: and now has been thanks to the questioner.

且行且努力 2024-08-15 09:35:22

或者,使用

\pgfextractx{<dimension>}{<point>}
\pgfextracty{<dimension>}{<point>}

这些是原始 PGF 命令,因此使用它们可能不太方便。

Alternatively, use

\pgfextractx{<dimension>}{<point>}
\pgfextracty{<dimension>}{<point>}

These are raw PGF commands, so it may be less convenient to use them.

流星番茄 2024-08-15 09:35:22

我刚刚从 this 找到的另一个选项表明,您可以通过以下方式从节点获取相对定位这样做:

\node at (2, 1) (a) {};
\draw (a) -- ++(right:2);

这将从节点 a 到点 a 右侧 2 个单位的点绘制一条线。就像我链接的示例一样,当您不知道节点的确切坐标并且需要绘制相对于它的事物时,这非常有用。

Another option I just found from this shows that you can get relative positioning from a node by doing this:

\node at (2, 1) (a) {};
\draw (a) -- ++(right:2);

This will draw a line from node a to the point 2 units to the right of point a. Like in the example I linked, this is useful when you don't know the exact coordinates of a node, and need to draw things relative to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文