PGF(Latex)中求全图宽度的方法

发布于 2024-08-28 02:28:37 字数 265 浏览 13 评论 0原文

我有一个乳胶宏,可以根据给定的参数使用 PGF 和 Tikz 绘制图片。绘制图片的宽度取决于这些参数。

PGF 会自动计算所绘制的任何图片的最终宽度,因此用户无需显式设置它(例如在图片环境中使用 Latex 构建时)。

但是我需要知道要绘制的图片的宽度。当然,我可以像 PGF 那样计算它,但这将是一项相当大的工作(很多 if 语句......)。有没有办法询问 PGF 要绘制的图片的宽度是多少(我期望一些命令)?是在 tikzpicture 环境内还是在它之后? 感谢您的帮助。

I've got a latex macro that draws a picture using PGF and Tikz according to given parameters. The width of picture drawn depends on these parameters.

PGF automatically calculates the resulting width of any picture drawn so the user does not have to set it explicitly(like for example when using latex build in picture environment).

However I need to know the width of picture that will be drawn. Of cause I could calculate it as the PGF does but this is going to be quite some work(a lot of if statements...). Is there a way to ask PGF what is the width of picture that is to be drawn (some command I expect)? Either inside tikzpicture environment or just after it?
Thanks for help.

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

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

发布评论

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

评论(1

泪意 2024-09-04 02:28:37

我可能会做的是将 tikzpicture 环境放入一个盒子中,然后找到盒子的宽度:

\setbox0=\vbox{\hbox{%
  \begin{tikzpicture}
    % ...
  \end{tizpicture}%
}}
The width of the following picture is {\the\wd0}.
\box0

请注意,运行 \box0 后,盒子将被插入写入文件,其内容将被销毁;因此,在执行此操作之前,您需要查询框的宽度。如果要保存宽度,可以将其存储在尺寸寄存器中:\dimen0=\wd0;或者,您可以使用 \copybox0,它会插入框但不会销毁它(尽管这可能会泄漏内存)。

另外,在之前尝试过其中一些内容后,我发现仅使用 \vbox 会导致该框始终为页面的整个宽度(如果您考虑一下,这是有道理的);然而,仅使用 \hbox 会因某种原因导致拆箱失败(它似乎是一只已知的哈巴狗)。然而,像这样使用两者都是有效的——我正在使用非常类似的东西将 TikZ 图片渲染为尺寸精确的 PDF 文件。

What I would probably do is put the tikzpicture environment in a box, and then find the width of the box:

\setbox0=\vbox{\hbox{%
  \begin{tikzpicture}
    % ...
  \end{tizpicture}%
}}
The width of the following picture is {\the\wd0}.
\box0

Note that after you run \box0, the box will be inserted into the document and its contents will be destroyed; you thus need to query the width of the box before you do that. If you want to save the width, you can store it in a dimension register with \dimen0=\wd0; alternatively, you can use \copybox0, which inserts the box but doesn't destroy it (although this might leak memory).

Also, having played with some of this before, I found that using just a \vbox caused the box to always be the full width of the page (which, if you think about it, makes sense); however, using just an \hbox caused the unboxing to fail for some reason (it seemed to be a known pug). Using both like this works, however—I'm using something very much like this to render TikZ pictures to PDF files of precisely the right size.

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