pgfmath-functionとしてのリストの長さ

Oct 29 2020

このスレッドを読みましたが、\def\mylist{1,2,3,5,7}後の計算で値が必要になるため、のようなリストの長さを指定するpgfmath関数が必要です。

以下は正しく動作しません。

出力が表示されます:次のよう112357
になります:5

私は何をしなければなりませんか?

\documentclass[a4paper]{article}
\usepackage{tikz}

\begin{document}
\makeatletter
\pgfmathdeclarefunction{Len}{1}{%
\begingroup
\def\templist{#1}
\foreach[count=\mycount] \i in \templist {   \xdef\Len{\mycount}  }%
\Len%
\endgroup
}
\makeatother

\def\mylist{1,2,3,5,7}
\pgfmathparse{Len(\mylist)}\pgfmathresult
\end{document}

回答

3 egreg Oct 28 2020 at 23:57

PGF配列には、追加の中括弧のセットが必要であることに注意してください。

\def\myarray{{1,2,3}}
\pgfmathparse{dim{\myarray}}

に3つ保存され\pgfmathresultます。

n番目のアイテムには次の方法でアクセスできます

\pgfmathparse{\myarray[n]}

どの店舗のn番目のアイテム。インデックス作成は0から始まることに注意してください。\pgfmathresult

完全な例:

\documentclass{article}
\usepackage{pgfmath}

\newcommand{\myarray}{{640,231,100,91,1003}}

\begin{document}

\pgfmathparse{dim{\myarray}}\pgfmathresult: should be 5

\pgfmathparse{\myarray[4]}\pgfmathresult: should be 1003

\pgfmathparse{dim({640,231,100,91,1003})}\pgfmathresult: should be 5

\pgfmathparse{dim{{640,231,100,91,1003}}}\pgfmathresult: should be 5

\end{document}

あなたはそれを参照してくださいdim(...)またはdim{...}同等ですが、アレイの周りにカッコをとにかく必要とされています。