Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying to make a simple graphic, that will change, when I change input parameters. I made arrays with input parameters:

\def\zamkl{{3,4,4.5,5}}
\def\zamkh{{4,3.5,5,4.5}}

then, I'm trying to make variables myl and myh, which I intend to use later:

\pgfmathparse{\zamkl[0]}
\def\myl{\pgfmathresult}
\pgfmathparse{\zamkh[0]}
\def\myh{\pgfmathresult}

but the result is very strange, the figure has size 8x8, though it should be 6x8:

enter image description here

The whole code is:

\begin{tikzpicture}
\def\zamkl{{3,4,4.5,5}}
\def\zamkh{{4,3.5,5,4.5}}

\pgfmathparse{\zamkl[0]}
\def\myl{\pgfmathresult}

\pgfmathparse{\zamkh[0]}
\def\myh{\pgfmathresult}

\draw[help lines] (0,0) grid (6,8);

\coordinate (A) at (0,0);
\coordinate (B) at (\myl,0); \node at (B) [below] {$T$};
\coordinate (C) at (2*\myl,0);
\coordinate (D) at (2*\myl,\myh); \node at (D) [left] {$S$};
\coordinate (E) at (2*\myl,2*\myh);
\coordinate (F) at (\myl,2*\myh); \node at (F) [below] {$R$};
\coordinate (G) at (0,2*\myh);
\coordinate (H) at (0,\myh); \node at (H) [right] {$W$};

\draw [very thick] (A) -- (C) -- (E) --(G) -- (A);

\fill (B) circle (4pt);
\fill (D) circle (4pt);
\fill (F) circle (4pt);

\end{tikzpicture}
share|improve this question
    
Welcome to TeX.SE. –  Peter Grill Apr 6 at 19:36

2 Answers 2

up vote 6 down vote accepted

You need to use an \edef instead of a \def. When you use just a \def, the value is set to the macro \pgfmathresult, and not the value of \pgfmathresult at the time of the \def. The subsequent pgfmathparse changes the value of `\pgfmathresult=4 so this is the value that both \myl and \myh take on once they are evaluated.

enter image description here

Code:

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
\def\zamkl{{3,4,4.5,5}}
\def\zamkh{{4,3.5,5,4.5}}

\pgfmathparse{\zamkl[0]}
\edef\myl{\pgfmathresult}

\pgfmathparse{\zamkh[0]}
\edef\myh{\pgfmathresult}

\draw[help lines] (0,0) grid (6,8);

\coordinate (A) at (0,0);
\coordinate (B) at (\myl,0); \node at (B) [below] {$T$};
\coordinate (C) at (2*\myl,0);
\coordinate (D) at (2*\myl,\myh); \node at (D) [left] {$S$};
\coordinate (E) at (2*\myl,2*\myh);
\coordinate (F) at (\myl,2*\myh); \node at (F) [below] {$R$};
\coordinate (G) at (0,2*\myh);
\coordinate (H) at (0,\myh); \node at (H) [right] {$W$};

\draw [very thick] (A) -- (C) -- (E) --(G) -- (A);

\fill (B) circle (4pt);
\fill (D) circle (4pt);
\fill (F) circle (4pt);

\end{tikzpicture}
\end{document}
share|improve this answer

Exactly what happens I don't know, but using

\pgfmathsetmacro\myl{\zamkl[0]}
\pgfmathsetmacro\myh{\zamkh[0]}

instead of the \defs works.

enter image description here

\documentclass[border=5mm,tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\def\zamkl{{3,4,4.5,5}}
\def\zamkh{{4,3.5,5,4.5}}


\pgfmathsetmacro\myl{\zamkl[0]}
\pgfmathsetmacro\myh{\zamkh[0]}

\draw[help lines] (0,0) grid (6,8);

\coordinate (A) at (0,0);
\coordinate (B) at (\myl,0); \node at (B) [below] {$T$};
\coordinate (C) at (2*\myl,0);
\coordinate (D) at (2*\myl,\myh); \node at (D) [left] {$S$};
\coordinate (E) at (2*\myl,2*\myh);
\coordinate (F) at (\myl,2*\myh); \node at (F) [below] {$R$};
\coordinate (G) at (0,2*\myh);
\coordinate (H) at (0,\myh); \node at (H) [right] {$W$};

\draw [very thick] (A) -- (C) -- (E) --(G) -- (A);

\fill (B) circle (4pt);
\fill (D) circle (4pt);
\fill (F) circle (4pt);

\end{tikzpicture}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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