ifnum에서 새 명령의 숫자 사용

ifnum에서 새 명령의 숫자 사용

정의한 항목의 양에 따라 문서의 결과 텍스트/모양을 변경하기 위해 처음에 항목 목록(예: 과일)을 정의하는 Latex 문서를 만들려고 합니다.

따라서 나는 \newcommand{\length}항목(과일)의 수를 세고 그 수를 '반환'하는 a를 정의했습니다.

이 최소한의 (작동하지 않는) 예제는 내가 달성하려는 것을 보여줍니다.

\documentclass{article}
\makeatletter
\newcounter{numlength}%
\newcommand{\length}[1]{%
    \setcounter{numlength}{0}%
    \@for\element:=#1\do{\stepcounter{numlength}}%
    \value{numlength}% Variant 1
    % \arabic{numlength}% Variant 2
    % \thenumlength% Variant 3
}
\makeatother
\def\fruits{apple,banana,mango}
\begin{document}
\section{Fruits}
Number of fruits: \length{\fruits}
\\There \ifnum\length{\fruits}=1 is just a single fruit\else are multiple fruits\fi on my list:
\\\fruits
\end{document}

내 텍스트( )에 숫자를 인쇄하는 것은 Number of fruits: \length{\fruits}변형 2와 3(내가 이해하는 바)에서만 작동하지만, 어떤 변형을 사용하더라도 숫자를 사용하는 것은 \ifnum작동하지 않습니다.

명확히 하자면, 저는 항목(과일) 자체를 인쇄하는 좋은 방법에는 관심이 없습니다. 내 주요 관심사는 조건 \newcommand{\length}에서 '계산'한 숫자를 사용하는 것입니다 \ifnum.

그리고 가능하다면 어떤 패키지( )도 사용을 자제하고 싶습니다 \usepackage.

더 많은 정보가 필요하시면 알려주시고 미리 감사드립니다!

답변1

@DavidCarlisle이 그의 의견에서 지적했듯이 TeX에서는 TeX가 숫자를 기대하는 매크로를 사용하려면 확장 가능해야 하므로 할당을 사용할 수 없습니다.

문제를 해결할 수 있는 두 가지 방법이 있습니다.

  1. 첫 번째 단계에서 과제를 수행하고 결과를 \ifnum.

  2. 확장 기능으로 과일 개수를 세어보세요.

다음은 L3의 함수/매크로를 사용하여 후자를 수행합니다.

\documentclass{article}

\ExplSyntaxOn
\cs_generate_variant:Nn \clist_count:n { V }
\cs_new_eq:NN \length \clist_count:V
\ExplSyntaxOff

\newcommand*\fruits{apple,banana,mango}

\begin{document}
\section{Fruits}
Number of fruits: \length{\fruits}
\\There \ifnum\length{\fruits}=1 is just a single fruit\else are multiple fruits\fi on my list:
\\\fruits
\end{document}

여기에 이미지 설명을 입력하세요


TeXnical 정보를 무시하고 위의 내용을 사용하는 것이 좋습니다.

내 의견과 달리 이 답변의 코드는 변형을 사용합니다 . L3의 기능 중 하나로 설정되지 않았기 V때문에 이는 의미상 더 깨끗해야 합니다. 와 달리 올바른 결과에 필요한 삭제 단계를 수행하기 때문입니다 .\fruitclistVN


첫 번째 접근 방식에서는 다음과 같습니다.

첫 번째 접근 방식을 사용하려면 실제 코드를 크게 변경할 필요가 없습니다. 직접 출력을 제거하고 대신 두 번째 단계에서는 카운터 값을 사용하면 됩니다 numlength.

\documentclass{article}
\makeatletter
\newcounter{numlength}%
\newcommand{\length}[1]{%
    \setcounter{numlength}{0}%
    \@for\element:=#1\do{\stepcounter{numlength}}%
}
\makeatother
\def\fruits{apple,banana,mango}
\begin{document}
\section{Fruits}
Number of fruits: \length{\fruits}\arabic{numlength}
\\There \length\fruits\ifnum\value{numlength}=1 is just a single fruit\else are multiple fruits\fi on my list:
\\\fruits
\end{document}

(위와 같이 출력)

답변2

다른 사람들이 이미 설명했듯이 먼저 길이를 매크로에 저장한 다음 \ifnum. 하지만 이 방법은 번거롭고 확장 자체가 불가능합니다.

나는 가 필요하지 않은 방법을 제안합니다 \def. 그러면 기존 명령을 망칠 위험이 없습니다(그리고 새로운 명령을 "발명"할 수도 있습니다).

목록에 추가하고 요소 수에 따라 더 복잡한 분기를 수행할 수도 있습니다. 목록은 비어 있지 않아야 합니다.

\branchonlist완전히 확장 가능하다는 점에 유의하세요 .

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\definelist}{mm}
 {
  \clist_clear_new:c { l_lennart_list_#1_clist }
  \clist_set:cn { l_lennart_list_#1_clist } { #2 }
 }

\NewDocumentCommand{\appendtolist}{mm}
 {
  \clist_put_right:cn { l_lennart_list_#1_clist } { #2 }
 }

\NewExpandableDocumentCommand{\listlength}{m}
 {
  \clist_count:c { l_lennart_list_#1_clist }
 }

\NewExpandableDocumentCommand{\branchonlist}{mmm}
 {% #1 = list name, #2 = cases, #3 = no match
  \int_case:nnF { \clist_count:c { l_lennart_list_#1_clist } } { #2 } { #3 }
 }

\ExplSyntaxOff

\definelist{fruits}{apple,mango,banana}
\definelist{animals}{gnu,gnat}
\definelist{letters}{a}

\begin{document}

Number of fruits: \listlength{fruits}

\branchonlist{fruits}{
  {1}{There is just a single fruit}
}{There are multiple fruits}

\bigskip

Number of animals: \listlength{animals}

\branchonlist{animals}{
  {1}{There is just a single animal}
  {2}{There are two animals}
}{There are multiple animals}

\bigskip

Number of letters: \listlength{letters}

\branchonlist{letters}{
  {1}{There is just a single letter}
  {2}{There are two letters}
  {3}{There are three letters}
}{There are multiple letters}

\bigskip

\appendtolist{letters}{b}

Number of letters: \listlength{letters}

\branchonlist{letters}{
  {1}{There is just a single letter}
  {2}{There are two letters}
  {3}{There are three letters}
}{There are multiple letters}

\bigskip

\appendtolist{letters}{c}

Number of letters: \listlength{letters}

\branchonlist{letters}{
  {1}{There is just a single letter}
  {2}{There are two letters}
  {3}{There are three letters}
}{There are multiple letters}

\bigskip

\appendtolist{letters}{d}

Number of letters: \listlength{letters}

\branchonlist{letters}{
  {1}{There is just a single letter}
  {2}{There are two letters}
  {3}{There are three letters}
}{There are multiple letters}

\end{document}

여기에 이미지 설명을 입력하세요

관련 정보