我正在尝试制作一个简单的图形,当我更改输入参数时,该图形会发生变化。我使用输入参数创建了数组:
\def\zamkl{{3,4,4.5,5}}
\def\zamkh{{4,3.5,5,4.5}}
然后,我尝试创建变量myl
和myh
,我打算稍后使用它们:
\pgfmathparse{\zamkl[0]}
\def\myl{\pgfmathresult}
\pgfmathparse{\zamkh[0]}
\def\myh{\pgfmathresult}
但结果很奇怪,图形有大小8x8
,虽然它应该是6x8
:
整个代码如下:
\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}
答案1
您需要使用\edef
而不是\def
。当您仅使用 时\def
,该值将设置为宏\pgfmathresult
,并且不是这价值在\pgfmathresult
时\def
。后续pgfmathparse
的值会发生变化,因此这是和被评估后所取\pgfmathresult=4
的值。\myl
\myh
代码:
\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}
答案2
我不知道到底发生了什么,但是使用
\pgfmathsetmacro\myl{\zamkl[0]}
\pgfmathsetmacro\myh{\zamkh[0]}
而不是\def
s 作品。
\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}