我的文档中有几张由 制作的图形TikZ
。它们的大小各不相同。如何获取每张图形的高度以进行一些复杂的计算?
编辑
我想在文本中访问图片的高度(而不是在图片中!),例如“上面的图片高度为\the\tikzheight
”。
平均能量损失
\documentclass{article}
\usepackage{tikz}
\begin{document}
% WHAT HEIGHT DO I HAVE?
\begin{tikzpicture}[scale=1.5]
% help lines
\draw[step=1,help lines,black!20] (-0.95,-0.95) grid (4.95,4.95);
% axis
\draw[thick,->] (-1,0) -- (5,0);
\draw[thick,->] (0,-1) -- (0,5);
% points
\foreach \Point/\PointLabel in {(1,1)/A, (3,1)/B, (1,4)/P_1, (3,4)/P_1}
\draw[fill=black] \Point circle (0.05) node[above right] {$\PointLabel$};
\end{tikzpicture}
\end{document}
答案1
有帮助吗?它绘制图片并将其大小写在其中心。打印值不考虑该scale
因素。在这种情况下,实际高度将是 1.5*171.27066pt。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
% WHAT HEIGHT DO I HAVE?
\begin{tikzpicture}[scale=1.5]
% help lines
\draw[step=1,help lines,black!20] (-0.95,-0.95) grid (4.95,4.95);
% axis
\draw[thick,->] (-1,0) -- (5,0);
\draw[thick,->] (0,-1) -- (0,5);
% points
\foreach \Point/\PointLabel in {(1,1)/A, (3,1)/B, (1,4)/P_1, (3,4)/P_1}
\draw[fill=black] \Point circle (0.05) node[above right] {$\PointLabel$};
\draw let \p1 = ($(current bounding box.north east)-(current bounding box.south west)$)
in node[red] at (current bounding box.center) {(\x1,\p1)};
\end{tikzpicture}
\end{document}
更新:
Leo Liu 的答案适合这个特定问题。它使用calc
包而不是calc
tikzlibrary。
\documentclass{article}
\usepackage{tikz}
\usepackage{calc}
\newsavebox\mytikz
\newlength\tikzheight
\begin{document}
\savebox{\mytikz}{%
% WHAT HEIGHT DO I HAVE?
\begin{tikzpicture}[scale=1.5, transform shape]
% help lines
\draw[step=1,help lines,black!20] (-0.95,-0.95) grid (4.95,4.95);
% axis
\draw[thick,->] (-1,0) -- (5,0);
\draw[thick,->] (0,-1) -- (0,5);
% points
\foreach \Point/\PointLabel in {(1,1)/A, (3,1)/B, (1,4)/P_1, (3,4)/P_1}
\draw[fill=black] \Point circle (0.05) node[above right] {$\PointLabel$};
\end{tikzpicture}%
}
\usebox{\mytikz}
\settototalheight\tikzheight{\usebox{\mytikz}}
The picture above has a height of \the\tikzheight.
\end{document}