如何使用PGF数学表达式?

如何使用PGF数学表达式?

我有一个包含值的数组。我希望对其进行一些计算并找出最大值。

查看代码:

\documentclass{scrartcl}
\usepackage{tikz}
\begin{document}
  %value array with 80 values
  \def\Rtable{{100, 100.391, 100.781, 101.172, 101.562, 101.953, 102.343, 102.733, 103.123, 103.513, 103.903, 104.292, 104.682, 105.071, 105.46, 105.849, 106.238, 106.627, 107.016, 107.405, 107.794, 108.182, 108.57, 108.959, 109.347, 109.735, 110.123, 110.51, 110.898, 111.286, 111.673, 112.06, 112.447, 112.835, 113.221, 113.608, 113.995, 114.382, 114.768, 115.155, 115.541, 115.927, 116.313, 116.699, 117.085, 117.47, 117.856, 118.241, 118.627, 119.012, 119.397, 119.782, 120.167, 120.552, 120.936, 121.321, 121.705, 122.09, 122.474, 122.858, 123.242, 123.626, 124.009, 124.393, 124.777, 125.16, 125.543, 125.926, 126.309, 126.692, 127.075, 127.458, 127.84, 128.223, 128.605, 128.987, 129.37, 129.752, 130.133, 130.515, 130.897}}
  %initializes the result value
  \def\RiValue{0}
  Test the result value: \RiValue.\\[\baselineskip]
  %calculation
  \foreach \i in {0, ..., 80}
  {
    \pgfmathparse{abs(\Rtable[\i] - 100 - 3.0897 / 8 * \i)}%this line works (see link below)
    \pgfmathsetmacro{\RiValue}{max(\RiValue, \pgfmathresult)}
    %the max routine seems not doing the right thing, because \RiValue stores every value of the abs routine
    %also \RiValue and \pgfmathresult have both the same value in each iteration
    \i: \RiValue\ and \pgfmathresult\newline
  }\\[\baselineskip]
  %\RiValue seems not changed at all
  The result value at the end: \RiValue.
\end{document}

关联对于我的另一个问题(我希望使用这个计算)。

我做错什么了?

答案1

不清楚您希望发生什么。\RiValue不过,它只是在本地重新定义,因此在每个循环开始时它都为零。由于生成的值都不是负数,因此计算值始终是最大值。

您期望这样的事情发生吗?

可能的需要

\documentclass[twocolumn]{article}
\usepackage{tikz}
\begin{document}
  %value array with 80 values
  \def\Rtable{{100, 100.391, 100.781, 101.172, 101.562, 101.953, 102.343, 102.733, 103.123, 103.513, 103.903, 104.292, 104.682, 105.071, 105.46, 105.849, 106.238, 106.627, 107.016, 107.405, 107.794, 108.182, 108.57, 108.959, 109.347, 109.735, 110.123, 110.51, 110.898, 111.286, 111.673, 112.06, 112.447, 112.835, 113.221, 113.608, 113.995, 114.382, 114.768, 115.155, 115.541, 115.927, 116.313, 116.699, 117.085, 117.47, 117.856, 118.241, 118.627, 119.012, 119.397, 119.782, 120.167, 120.552, 120.936, 121.321, 121.705, 122.09, 122.474, 122.858, 123.242, 123.626, 124.009, 124.393, 124.777, 125.16, 125.543, 125.926, 126.309, 126.692, 127.075, 127.458, 127.84, 128.223, 128.605, 128.987, 129.37, 129.752, 130.133, 130.515, 130.897}}
  %initializes the result value
  \def\RiValue{0}
  Test the result value: \RiValue.\par
  %calculation
  \foreach \i in {0, ..., 80}
  {
    \pgfmathsetmacro\myresult{abs(\Rtable[\i] - 100 - 3.0897 / 8 * \i)}%this line works (see link below)
    \pgfmathsetmacro{\RiValue}{max(\RiValue, \myresult)}
    %the max routine seems not doing the right thing, because \RiValue stores every value of the abs routine
    %also \RiValue and \pgfmathresult have both the same value in each iteration
    \i: \RiValue\ and \myresult\par
    \global\let\RiValue\RiValue
  }
  %\RiValue seems not changed at all
  The result value at the end: \RiValue.
\end{document}

相关内容