다음에 호출할 때와 처음으로 다르게 동작하도록 하는 명령이 필요합니다. 지금은 을 사용합니다 \newcommand\foo{First\gdef\foo{Next}}
. 즉, 처음으로 자체를 재정의합니다.
게다가 어느 시점에서는 그 의미를 "재설정"해야 하므로 를 정의합니다 \newcommand\resetfoo{\gdef\foo{First\gdef\foo{Next}}}
.
그런데 이게 좀 어려운 것 같아요. 이 작업을 수행하는 일반적인 방법이 있을 수 있지만 저는 그것에 대해 알지 못합니다(또한 이에 대해 거의 아는 바가 없습니다).확장). 이런 종류의 문제를 해결하는 올바른 방법은 무엇입니까?
질문
질문에서 알 수 있듯이 좀 더 일반적인 방법이 필요합니다. 정의를 변경하는 일부 기능입니다.N"확장"(단어를 올바른 위치에 사용하고 있는지 잘 모르겠습니다). 이 문제를 어떻게 해결하시겠습니까? 나에게도 필요한 것이 필요하다 \resetfoo
.
어떤 식으로든 아직 완료되지 않은 경우, 내 생각은 명령 \changedefinitionafter\foo{3}{OneTwoThree}{Next}
이나 그와 유사한 것을 갖는 것입니다. expl3
솔루션도 환영합니다.
다음은 보다 일반적인 MWE입니다.
\documentclass{scrartcl}
\newcommand\foo{First\gdef\foo{Next}}
\newcommand\resetfoo{\gdef\foo{First\gdef\foo{Next}}}
\begin{document}
\foo~\foo~\foo
\resetfoo~\foo~\foo
\end{document}
또한 질문 제목에 대한 태그와 제안도 환영합니다.
답변1
다음 예를 살펴보십시오.
\documentclass{article}
\newcounter{testcount}
\newcommand{\modifyme}{%
\addtocounter{testcount}{1}
\ifnum\thetestcount<3%
Hello
\fi
\ifnum\thetestcount>2%
World
\fi
}
\newcommand{\resetme}{\setcounter{testcount}{0}}
\begin{document}
\modifyme
\modifyme
\modifyme
\modifyme
\resetme
\modifyme
\end{document}
답변2
나는 카운터를 사용하는 것을 좋아합니다! 여기서는 에 지정된 숫자 이후의 확장만 \changedefinitionafter
수정됩니다.
\documentclass{article}
\pagestyle{empty}% for cropping
\makeatletter
\newcount\count@foo
\newcount\nth@foo
\newcommand\changedefinitionafter[4]{
% #1: name of macro
% #2: exceptional occurence
% #3: normal expansion
% #4: exceptional expansion
\global\count@foo=0
\global\nth@foo=#2
\gdef#1{%
\advance\count@foo by 1
\ifnum\count@foo=\nth@foo
#4%
\else
#3%
\fi
}
\edef\resetname{reset\expandafter\@gobble\string#1}
\expandafter\gdef\csname \resetname \endcsname{\global\count@foo=0 }
}
\makeatother
\begin{document}
\obeylines
\changedefinitionafter\foo{3}{OneTwoThree}{Next}
\foo
\foo
\foo
\foo
\resetfoo
\foo
\foo
\foo
\foo
\end{document}
답변3
카운터는 없지만 이 유형의 각 명령에 대해 세 개의 매크로가 있습니다.
\documentclass{article}
\makeatletter
\newcommand{\newchangingcommand}[4]{%
% #1 = macro name
% #2 = steps
% #3 = value until step #1
% #4 = value from step #1
\@namedef{\string#1@counter}{0}%
\@namedef{\string#1@limit}{#2}%
\def#1{%
% step the counter
\global\@nameedef{\string#1@counter}{\number\numexpr\@nameuse{\string#1@counter}+1\relax}%
\ifnum\@nameuse{\string#1@counter}=\@nameuse{\string#1@limit}\relax
\gdef#1{#4}#4%
\else
#3%
\fi
}%
}
\providecommand\@nameedef[1]{\expandafter\edef\csname#1\endcsname}
\makeatother
\newchangingcommand{\foo}{3}{Two}{Next}
\newchangingcommand{\foob}{2}{One}{Next}
\begin{document}
\foo--\foob\par
\foo--\foob\par
\foo--\foob\par
\foo--\foob\par
\end{document}
인수 이동에 이러한 명령을 사용하면 여러 가지 이유로 실패할 수 있다는 점에 유의하세요.