
별표가 있는/별표가 없는 버전의 명령에 대해 읽은 모든 문서에서는 다음과 같은 수행 방법을 권장합니다(예:TeX FAQ 목록):
\newcommand{\mycommand}{\@ifstar\mycommandStar\mycommandNoStar}
\newcommand{\mycommandStar}{%
<few lines of code only for starred mycommand>
<many lines of code common to starred/non starred mycommand>
}
\newcommand{\mycommandNoStar}{%
<few lines of code only for non starred mycommand>
<many lines of code common to starred/non starred mycommand>
}
환경의 경우 구성표는 유사합니다(예:이 답변):
\newenvironment{myenvironment}{%
<few lines of code only for non starred myenvironment>
<many lines of code common to starred/non starred myenvironment>
}{%
<few lines of code only for non starred myenvironment>
<many lines of code common to starred/non starred myenvironment>
}
\newenvironment{myenvironment*}{%
<few lines of code only for starred myenvironment>
<many lines of code common to starred/non starred myenvironment>
}{%
<few lines of code only for starred myenvironment>
<many lines of code common to starred/non starred myenvironment>
}
그러나 대부분의 경우 별표가 있는 버전과 별표가 없는 버전 사이에는 약간의 차이만 있으며 이 방법을 사용하려면 두 버전 간의 공통 코드의 모든 변경 사항을 복사해야 합니다. 이는 유지 관리가 어렵습니다(특히 공통 코드가 다음과 같은 경우). 긴).
더 효율적인 방법이 있나요?
답변1
이는 주로 명령이 수행해야 하는 작업에 따라 다릅니다. 처음에 일부 다른 코드를 실행해야 한다는 이유만으로 \mycommand*
다른 경우에는 다음 접근 방식이 작동합니다.\mycommand
\newcommand{\mycommand}{\@ifstar{\@tempswatrue\@mycommand}{\@tempswafalse\@mycommand}}
\newcommand{\@mycommand}{%
\if@tempswa
<few lines of code only for starred mycommand>
\else
<few lines of code only for non starred mycommand>
\fi
<many lines of code common to starred/non starred mycommand>
}
이것 으로 xparse
더 쉬워집니다:
\usepackage{xparse}
\NewDocumentCommand{\mycommand}{s}
{\IfBooleanTF{#1}
{<few lines of code only for starred mycommand>}
{<few lines of code only for non starred mycommand>}%
<many lines of code common to starred/non starred mycommand>%
}
환경 양식의 경우 사용 가능한 항목이 없기 때문에 추가적인 복잡성이 있습니다 \@ifstar
.
\newenvironment{myenvironment}{%
<few lines of code only for non starred myenvironment>
\@myenvironmentstart
}{%
<few lines of code only for non starred myenvironment>
\@myenvironmentfinish
}
\newenvironment{myenvironment*}{%
<few lines of code only for starred myenvironment>
\@myenvironmentstart
}{%
<few lines of code only for starred myenvironment>
\@myenvironmentfinish
}
\newcommand{\@myenvironmentstart}{%
<many lines of code common to starred/non starred myenvironment>
}
\newcommand{\@myenvironmentfinish}{%
<many lines of code common to starred/non starred myenvironment>
}
에서는 실제 단순화가 불가능합니다 xparse
.
답변2
xparse
LaTeX3 번들의 일부인 패키지는 이와 관련하여 매우 편리합니다 . 예를 들어:
\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand{\foo}{s}{This is the \IfBooleanTF{#1}{starred }{}foo command.}
\begin{document}
\foo{}
\foo*{}
\end{document}