![新しいコマンド: 座標を単一の引数として渡す](https://rvso.com/image/305778/%E6%96%B0%E3%81%97%E3%81%84%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89%3A%20%E5%BA%A7%E6%A8%99%E3%82%92%E5%8D%98%E4%B8%80%E3%81%AE%E5%BC%95%E6%95%B0%E3%81%A8%E3%81%97%E3%81%A6%E6%B8%A1%E3%81%99.png)
配列引数を渡して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}