newcommand: 좌표를 단일 인수로 전달

newcommand: 좌표를 단일 인수로 전달

newcommand배열 인수를 전달한 다음 해당 개별 배열 요소에 액세스할 수 있습니까 ?

예를 들어, 배열의 세 번째 요소(첫 번째 인수)에 두 번째 인수의 값을 곱하려는 경우:

\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}

관련 정보