TikZ:使用位图的尺寸作为全局边界框

TikZ:使用位图的尺寸作为全局边界框

我是使用 tikzpicture 环境在位图图像上绘图为了添加注释。我将位图的宽度设置为\textwidth.我想在页面的左边距或右边距添加注释。TikZ 通常首先完成tikzpicture,然后将其与左边距对齐。因此这里需要手动设置边界框以确保位图而不是整个绘图左对齐。

\documentclass{scrartcl}
\usepackage{tikz,lipsum}
\begin{document}
\raggedright
\begin{tikzpicture}
    \useasboundingbox (0,0) rectangle (\textwidth,12em);         % <------- !
    \node[anchor=south west,inner sep=0] (image) at (0,0) {%
    \rule{\textwidth}{12em}};
    \begin{scope}[x={(image.south east)},y={(image.north west)}]
        \draw[red,-latex] (-0.05,0.7) node[anchor=east] {abc} -- (0.5,0.9);
    \end{scope}
\end{tikzpicture}\par
\lipsum
\end{document}

只要我们知道位图的尺寸,特别是它的高度,这一切都没问题。但是——有没有办法让 tikz 使用位图的尺寸(在我将其缩放到之后width=\textwidth)作为整个绘图的边界框,而我们事先并不知道它们?

答案1

将以下代码添加到你的序言中

\newlength{\bitmapwd}

然后在 TikZ 图片前输入

\settowidth{\bitmapwd}{\includegraphics[...]{...}}

其中的\includegraphics命令应该与打印图像时使用的命令相同。在 TikZ 图片中,您可以使用\bitmapwd与所需长度相等的长度。

相关内容