
나는 마크로 성분이 포함된 나만의 요리책 스타일을 만들려고 노력 중입니다. 가끔 재료를 분리해서 헤드라인을 넣어야 할 때가 있어서 옵셔널 인수로 시도해봤습니다. 최소한의 예는 다음과 같습니다.
\documentclass{scrartcl}
\newcommand{\ingredient}[3][]{
#1 #2 & #3 \\
}
\begin{document}
\begin{tabular}{r|l}
\ingredient[\multicolumn{2}{l}{Head} \\]{test1}{test2}
\ingredient[test3]{test4}{test5}
\ingredient{test6}{test7}
\end{tabular}
\end{document}
이 코드에 어떤 문제가 있나요? 다음과 같은 오류 메시지가 나타납니다.
! Misplaced \omit.
\multispan ->\omit
\@multispan
l.7 ...[\multicolumn{2}{l}{Head} \\]{test1}{test2}
답변1
표 자료는정말줄 "앞"에 무엇이 허용되는지에 대해 까다롭습니다. 특히, 완전히 확장 가능해야 합니다.
따라서 심지어과제선택적 인수를 확인하기 위해 수행해야 하는 작업 \ingredient
은 줄을 시작하기에 충분하며 \multicolumn
더 이상 표시되지 않습니다.
이 문제를 해결하는 가장 쉬운 방법은 첫 번째 인수를 \ingredient
필수로 만드는 것입니다.
\documentclass{scrartcl}
\newcommand{\ingredient}[3]{
#1 #2 & #3 \\
}
\begin{document}
\begin{tabular}{r|l}
\ingredient{\multicolumn{2}{l}{Head} \\}{test1}{test2}
\ingredient{test3}{test4}{test5}
\ingredient{}{test6}{test7}
\end{tabular}
\end{document}
답변2
너~할 수 있다원래 구문을 유지하고 \DeclareExpandableDocumentCommand
다음에서 사용하는 경우 선택적 매개변수가 있는 단일 매크로를 갖습니다.xparse
패키지.
암호:
\documentclass{scrartcl}
\usepackage{xparse}
\DeclareExpandableDocumentCommand{\ingredient}{O{} m m}{%
#1 #2 & #3 \\%
}%
\begin{document}
\begin{tabular}{r|l}
\ingredient[\multicolumn{2}{l}{Head} \\]{test1}{test2}
\ingredient[test3]{test4}{test5}
\ingredient{test6}{test7}
\end{tabular}
\end{document}
답변3
noalign
기능을 구현하기 위해 "창의적으로" 사용할 수 있습니다 . 다음 코드에서는 다음과 유사하게 작동하는 함수를 정의 합니다 \NewDocumentCommandOptionalInTabular
.\NewDocumentCommand
~할 수 있다원하는 대로 선택적 인수(또는 별표)를 사용하세요.
\documentclass{scrartcl}
% ======== copy paste this part ========
\ExplSyntaxOn
\cs_new_protected:Npn \NewDocumentCommandOptionalInTabular #1 #2 #3 {
\NewDocumentCommandOptionalInTabular_aux:xnnn {\exp_not:c{\cs_to_str:N #1-aux}} #1 {#2} {#3}
}
\cs_new_protected:Npn \NewDocumentCommandOptionalInTabular_aux:nnnn #1 #2 #3 #4 {
\cs_new:Npn #2 { \noalign \bgroup #1 }
\NewDocumentCommand #1 {#3} { \egroup #4 }
}
\cs_generate_variant:Nn \NewDocumentCommandOptionalInTabular_aux:nnnn {x}
\ExplSyntaxOff
% ======== end ========
% then you can just use \NewDocumentCommandOptionalInTabular to replace \NewDocumentCommand
\NewDocumentCommandOptionalInTabular \ingredient {O{} m m}{
#1 #2 & #3 \\
}
\begin{document}
\begin{tabular}{r|l}
\ingredient[\multicolumn{2}{l}{Head} \\]{test1}{test2}
\ingredient[test3]{test4}{test5}
\ingredient{test6}{test7}
\end{tabular}
\end{document}
에서 영감을 받다에그레그의 답변.