
또는 인수로 \yesifone
가져와 이 두 가지 경우에 따라 다르게 동작하는 명령이 있습니다 . 명령의 출력을 전달하고 싶습니다 . 그러나 출력을 생성하지 않는 다른 명령에 의존합니다 (구체적인 경우에는 변수를 설정합니다). 이 작업을 수행하려는 나의 현재 시도는 실패합니다. 아마도 단지 숫자가 아닌 비교에 를 포함하기 때문일 것입니다 . 예를 들어 호출을 무시하고 출력 텍스트만 확인하여 이 작업을 수행하려면 어떻게 해야 합니까 ?1
0
\one
\one
\blah
ifnum
\blah{}
1
\blah{}
즉, 다음을 출력하고 싶습니다 yes
.
\documentclass{minimal}
\begin{document}
\newcommand{\blah}{}
\newcommand{\one}{
\blah{}
1
}
\newcommand{\yesifone}[1]{%
\ifnum1=#1\relax{yes}\else{no}\fi
}
\yesifone{\one} % error: Missing number, treated as zero.
\end{document}
답변1
귀하의 코드를 사용하면 \yesifone{\one}
토큰이 생성됩니다.
\yesifone{1\one}2
이는 으로 확장됩니다 .
\ifnum112=12\one\relax{1y11e11s11}2\else{1n11o11}2\fi
\ifnum의 두 번째 TeX-⟨ 에 속하는 토큰을 수집할 당시숫자⟩-수량, 즉 뒤에 있는 숫자 , 토큰이 확장되어 s.th가 됩니다. 좋다=12\one
\ifnum112=12␣10\blah{1}2␣10112␣10\relax{1y11e11s11}2\else{1n11o11}2\fi.
바로 뒤에 있는 공간 토큰은 제거되고 확장되며 대체 텍스트가 비어 있으므로 사라집니다. 따라서 당신은 s.th를 가지고 있습니다. 좋다:␣10=12\blah
\ifnum112=12{1}2␣10112␣10\relax{1y11e11s11}2\else{1n11o11}2\fi.
\ifnum그래서 의 두 번째 TeX-⟨ 에 속하는 토큰을 수집할 당시숫자⟩-수량, 즉 뒤에 있는 숫자 , TeX는 빈 중괄호 그룹, 즉 명시적 문자 토큰 및 을 찾습니다 . 이는 확실히 유효한 TeX-⟨를 형성하는 토큰 시퀀스의 시작을 형성하지 않습니다.=12{1}2숫자⟩-수량.
TeX가 눈과 소화관을 가진 짐승이라는 Knuth의 비유를 생각해 보십시오.
- 확장 가능 토큰의 확장은 확장이 억제되지 않는 한(예: 매개변수 텍스트 또는 -할당의 대체 텍스트를 구성하는 토큰의 경우) 일종의 역류 프로세스의 식도에서 발생합니다
\def
. (LaTeX\newcomand
등\NewDocumentCommand
은 호출을 위한 정교한 래퍼입니다\def
.) - 할당은 위장에서 이루어집니다.
따라서 식도/확장형 토큰의 확장이 충분하고 식도 뒤의 소화 기관이 조판에만 관여하는 작업과 비조판 작업을 수행하기 위한 위장을 포함하는 "변수"에 값을 할당하는 것과 같은 작업을 분리하세요.
\documentclass{minimal}
% Introduce/initialize things used as variable whose value is
% to be set via assignments that take place in the stomach:
\newcommand\VariableRelatedToBlah{}
% Define macros for tasks that involve digestive organs behind the gullet for
% for non-typesetting-tasks, e.g.,_setting_ values of variables via assignments:
\newcommand\SetValueOfVariableRelatedToBlah[1]{%
\def\VariableRelatedToBlah{#1}%
}
% Define macros for tasks that involve only the gullet, e.g.,
% _retrieving_ values of variables, or additionally to the gullet
% involve digestive organs behind the gullet only for typesetting:
\newcommand\RetrieveValueOfVariableRelatedToBlah{%
\VariableRelatedToBlah
}
\newcommand\firstofone[1]{#1}%
\newcommand{\yesifone}[1]{%
\ifnum1=\expandafter\firstofone\expandafter{\number#1} yes\else no\fi
}
\begin{document}
% Now you can keep work that involves the stomach for non-typesetting
% separated from work where the gullet is sufficient/where tokens
% delivered by the gullet can directly be used for typesetting:
\SetValueOfVariableRelatedToBlah{0}%
\yesifone{\RetrieveValueOfVariableRelatedToBlah}
\SetValueOfVariableRelatedToBlah{1}%
\yesifone{\RetrieveValueOfVariableRelatedToBlah}
\end{document}