新しいコマンド: 座標を単一の引数として渡す

新しいコマンド: 座標を単一の引数として渡す

配列引数を渡してnewcommand、個々の配列要素にアクセスすることは可能ですか?

たとえば、配列の 3 番目の要素 (最初の引数) を 2 番目の引数の値で乗算したい場合は、次のようになります。

\newcommand{\hello}[2]{(#1.#3)*#2}

(注 #1.#3 はおそらく間違っています。この値にアクセスする方法を尋ねています。)

本体コードでコマンドを使用します:

I want \hello{1,2,5}{2} gumdrops.

編集: 最小限の動作例?

\documentclass{standalone}
\newcommand{\hello}[2]{(#1)*(#2)}
\begin{document}
I want \hello{1}{2} gumdrops.
\end{document}

実際には掛け算ではありません( で数値を掛け算する方法がわからないようですnewcommand)が、それは私にとって重要ではなく、いずれにしても引数の値にアクセスできるようにしたいのです。最初の引数を除いて、配列を渡してその特定の要素にアクセスできるようにしたいと思います。個別に(つまり、ループしない)、これは MWE ではまったく表現されません。

答え1

次のものがあなたに合うかもしれません:

ここに画像の説明を入力してください

\documentclass{article}
\usepackage{etoolbox,xparse}

\ExplSyntaxOn
  \cs_new_eq:NN \CALC \fp_eval:n
\ExplSyntaxOff

\newcounter{argcnt}
\makeatletter
\newcommand{\newarray}[2]{% \newarray{<array>}{<csv list>}
  \setcounter{argcnt}{0}% Restart argument count
  \renewcommand{\do}[1]{% With each element do...
    \stepcounter{argcnt}% Next arg
    \expandafter\gdef\csname #1@\theargcnt\endcsname{##1}% Store arg in \<array>@<num>
  }%
  \docsvlist{#2}% Store array
  \expandafter\xdef\csname #1@0\endcsname{\theargcnt}% Store array element count
}
\newcommand{\getelement}[2]{% \getelement{<array>}{<num>}
  \@ifundefined{#1@0}% <array> not defined
    {\@latex@error{Array #1 undefined}\@ehc}{}
  \expandafter\ifnum\csname #1@0\endcsname<#2 % <num> out of range
    \@latex@error{Array #1 only has \csname #1@0\endcsname\space elements}\@ehc
  \fi
  \csname #1@#2\endcsname% Print element
}
\newcommand{\calc}[1]{\expandafter\CALC\expandafter{#1}}% calculations
\makeatother

\begin{document}
\newarray{arrayA}{a,b,c}%
\newarray{arrayB}{1,2,3}%

\getelement{arrayA}{1}

$2 \times \getelement{arrayB}{2} + \getelement{arrayB}{3} = \calc{2*\getelement{arrayB}{2}+\getelement{arrayB}{3}}$
\end{document}

\newarray{<array>}{<csv list>}<array>カンマ区切りの値リストに含まれる要素を持つ配列を作成します<csv list>\getelement{<array>}{<num>}配列のインデックスのように動作し、\calc計算を実行するために使用できます。数値要素。

このアプローチにより、単なる数字だけでなく、事実上あらゆるものを配列に格納できます (および/またはそれらを混在させることができます)。

答え2

これはあなたが言いたいことと似ていますか? おそらく、これで始めることができるでしょう。

\documentclass{article}

\newcommand{\twothings}[2]{%
    \gdef\thingone{#1}
    \gdef\thingtwo{#2}
}

\newcommand{\pickone}[1]{%
    \ifnum#1 = 1
        \thingone
    \else
        \ifnum#1 = 2
            \thingtwo
        \else
            \relax
        \fi
    \fi%
}

\begin{document}

\twothings{First}{Second}

The second thing is \pickone{2}.

\end{document}

関連情報