3

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

PGF 会自动计算绘制的任何图片的最终宽度,因此用户不必明确设置它(例如在图片环境中使用乳胶构建时)。

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

4

1 回答 1

5

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.

于 2010-04-11T21:44:46.403 回答