
실제로 전달되는 것보다 더 많은 인수를 후루룩 마시는 \foo
또 다른 제어 시퀀스를 제어 시퀀스에 포함해도 괜찮습니까 ?\slurp
\foo
예를 들어 다음과 같이 해도 괜찮습니까?
\documentclass{article}
\newcommand\foo [1]{#1 \slurp}
\newcommand\slurp[3]{#1 #2 #3}
\begin{document}
\foo{a}{b}{c}{d}
\end{document}
이것 대신?
\documentclass{article}
\newcommand\foo [4]{#1 \slurp{#2}{#3}{#4}}
\newcommand\slurp[3]{#1 #2 #3}
\begin{document}
\foo{a}{b}{c}{d}
\end{document}
답변1
괜찮나요? 네 확실합니다! 실제로 이러한 매크로 정의의 용도는 다양합니다. 특히 별표 표시된 명령 변형에 대한 기본 정의가 있습니다. 예를 들어,article
\section
다음과 같이 정의합니다
\newcommand\section{\@startsection {section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%
{\normalfont\Large\bfseries}}
소요 시간 확인영\section[<toc>]{<title>}
일반적으로 ?! 로 지정/사용하더라도 인수입니다 . \@startsection
6개의 인수를 취한 다음 사용자가 별표를 추가했는지 여부를 테스트하기 때문입니다 . 에서latex.ltx
:
\def\@startsection#1#2#3#4#5#6{%
\if@noskipsec \leavevmode \fi
\par
\@tempskipa #4\relax
\@afterindenttrue
\ifdim \@tempskipa <\z@
\@tempskipa -\@tempskipa \@afterindentfalse
\fi
\if@nobreak
\everypar{}%
\else
\addpenalty\@secpenalty\addvspace\@tempskipa
\fi
\@ifstar
{\@ssect{#3}{#4}{#5}{#6}}%
{\@dblarg{\@sect{#1}{#2}{#3}{#4}{#5}{#6}}}}
따라서 우리가 일반적으로 지정하는 인수는 \section
두 단계 아래의 매크로에 의해서만 먹혀집니다.
이것이 왜 좋은 관행인지 보여주는 또 다른 좋은 예는 카테고리 코드의 변경과 관련이 있습니다. 사용을 위해 인수가 소비되면 해당 범주 코드를 변경할 수 없습니다. 따라서 도우미 매크로는 일반적으로 카테고리 코드를 수정하는 데 사용됩니다.~ 전에어떤 주장이라도 삼켜버리세요.
그 밖에도 수많은 예가 있다.LaTeX 커널, 기본 글꼴 매크로부터 ToC 처리, 심지어 다음을 통한 새 명령 정의까지 \newcommand
:
\def\newcommand{\@star@or@long\new@command}
다시 말하지만, 인수를 사용하지 않지만 토치를 다른 매크로에 전달하기 전에 일부 작업을 수행하는 또 다른 매크로입니다. 일반적으로 이 원칙은 커널과 패키지 전반에 걸쳐 잘 사용됩니다.
답변2
Werner의 답변에서 설명했듯이 이는 일반적인 관행입니다. *-변형이 있는 모든 매크로는 다음과 같이 정의됩니다.
\newcommand{\foo}{\@ifstar{\@sfoo}{\@foo}}
\newcommand{\@sfoo}[1]{Foo with * applied to #1}
\newcommand{\@sfoo}[1]{Foo without * applied to #1}
또는 그 변형. 마찬가지로, 하나 이상의 선택적 인수가 있는 매크로는 선택적 인수 \makebox
가 없는지, 하나 또는 두 개의 선택적 인수가 있는지 결정하기 위해 긴 경로를 취해야 합니다.
\newcommand{\bar}{\@ifnextchar[{\@bar@i}{\@bar}}
\def\@bar@i[#1]{\@ifnextchar[{\@bar@ii{#1}}{\@bar@iii{#1}}
\def\@bar@ii#1[#2]#3{Bar has two optional arguments, #1 and #2, and #3}
\def\@bar@iii#1#2{Bar has one optional argument, #1, and #2}
\def\@bar#1{Bar has no optional argument and #1}
상황 xparse
은 매우 다릅니다. *-변형과 선택적 인수는 상당히 일반적인 방식으로 지정할 수 있으므로 로드하는 것이 좋습니다.모두실제 인수:
\usepackage{xparse}
\NewDocumentCommand{\foo}{sm}{%
\IfBooleanTF{#1}
{Foo with * applied to #2}
{Foo without * applied to #2}%
}
\NewDocumentCommand{\bar}{oom}{%
\IfNoValueTF{#1}
{Bar has no optional argument and #3}
{\IfNoValueTF{#2}
{Bar has one optional argument, #1, and #3}
{Bar has two optional arguments, #1 and #2, and #3}%
}%
}%
}
이것이 LaTeX3의 "미래"입니다.