4

I have several graphics (produced by TikZ) in my document. They all have different sizes. How do I get the height of each graphic to do some nasty calculations?

Edit

I want to access the picture height in the text (not in the picture!), e.g. "The picture above has a height of \the\tikzheight."

MWE

\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 Answers1

4

Does it help? It draws the picture and writes its size in its center. Printed values don't consider the scale factor. In this case, real height would be 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}

enter image description here

Update:

Leo Liu's answer adapted to this particular problem. It uses calc package instead of 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}

enter image description here

Ignasi
  • 136,588
  • I want to print the real height in the text (e.g. below the picture). Something like "The picture above has an height of 250pt." – Andreas Schneider May 04 '16 at 08:00
  • @Ignasi : I did not find "in node" in the PGF documentation, could you please tell the page or section number in the manual? Thanks! – AlexG May 04 '16 at 08:22
  • @Sr.Schneider Answer update, I hope it's better now. – Ignasi May 04 '16 at 08:30
  • 1
    @AlexG The expression is not in node but let ... in which corresponds to calc tikzlibrary. You can start with tutorial "Euclid’s Amber Version of the Elements" (section or chapter 4) – Ignasi May 04 '16 at 08:32