TransWikia.com

Using arrays and variables in TikZ

TeX - LaTeX Asked by cow-brat on July 7, 2021

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

defzamkl{{3,4,4.5,5}}
defzamkh{{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]}
defmyl{pgfmathresult}
pgfmathparse{zamkh[0]}
defmyh{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}
defzamkl{{3,4,4.5,5}}
defzamkh{{4,3.5,5,4.5}}

pgfmathparse{zamkl[0]}
defmyl{pgfmathresult}

pgfmathparse{zamkh[0]}
defmyh{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}

2 Answers

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}
defzamkl{{3,4,4.5,5}}
defzamkh{{4,3.5,5,4.5}}

pgfmathparse{zamkl[0]}
edefmyl{pgfmathresult}

pgfmathparse{zamkh[0]}
edefmyh{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}

Correct answer by Peter Grill on July 7, 2021

Exactly what happens I don't know, but using

pgfmathsetmacromyl{zamkl[0]}
pgfmathsetmacromyh{zamkh[0]}

instead of the defs works.

enter image description here

documentclass[border=5mm,tikz]{standalone}
begin{document}
begin{tikzpicture}
defzamkl{{3,4,4.5,5}}
defzamkh{{4,3.5,5,4.5}}


pgfmathsetmacromyl{zamkl[0]}
pgfmathsetmacromyh{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}

Answered by Torbjørn T. on July 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP