2

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}
  • hello, What do you want to do at the end ? – flav May 4 '16 at 7:19
  • this answer works with your code after suppressing empty lines inside tikzpicture: tex.stackexchange.com/a/11949/1952 – Ignasi May 4 '16 at 7:21
  • Let's say I want to save the height \newdimen\tikzheight and print it like \showthe\tikzheight. – Sr. Schneider May 4 '16 at 7:24
2

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

  • 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." – Sr. Schneider May 4 '16 at 8: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 4 '16 at 8:22
  • @Sr.Schneider Answer update, I hope it's better now. – Ignasi May 4 '16 at 8: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 4 '16 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.