![您不能在命令内部向命令传递任何可选参数吗?](https://linux22.com/image/412224/%E6%82%A8%E4%B8%8D%E8%83%BD%E5%9C%A8%E5%91%BD%E4%BB%A4%E5%86%85%E9%83%A8%E5%90%91%E5%91%BD%E4%BB%A4%E4%BC%A0%E9%80%92%E4%BB%BB%E4%BD%95%E5%8F%AF%E9%80%89%E5%8F%82%E6%95%B0%E5%90%97%EF%BC%9F.png)
有没有办法不传递另一个命令中调用的命令的可选参数的值,而不是传递一个空参数?我已经编译了我的代码以更好地解释我的问题。
\documentclass{article}
\newcommand{\question}[2][\quad]{
\begin{description}
\item[#1] #2
\end{description}
}
\newcommand{\subquestion}[2][]{
\question{\question[#1]{#2}}
}
\begin{document}
\question[1.]{
This is question one.
}
\subquestion[a)]{
It has a subquestion.
}
\question{
This is a non-numbered question, which is obtained by omitting the optional argument in the question command. It has the same indent as a numbered question because the default value of the optional argument is quad.
}
\subquestion{
This non-numbered subquestion doesn't have the same indent as a numbered subquestion, because the absent optional argument of subquestion passes an empty argument as an the optional argument of the question command, instead of passing no argument at all. If I set the default value of the optional argument of subquestion to quad, the problem is resolved, but I would like this arborescing structure to be editable without having to find all the concerned commands.
}
\end{document}
答案1
我不确定我是否完全理解了所需的行为,但也许是这样的。它涉及测试可选参数并相应地进行分支。
\documentclass{article}
\newcommand{\question}[2][\relax]{%
\begin{description}
\ifx\relax#1\item[\quad] #2\else \item[#1] #2\fi
\end{description}%
}
\newcommand{\subquestion}[2][\relax]{%
\ifx\relax#1\question{\question{#2}}\else
\question{\question[#1]{#2}}\fi
}
\begin{document}
\question[1.]{
This is question one.
}
\subquestion[a)]{
It has a subquestion.
}
\question{
This is a non-numbered question, which is obtained by omitting the optional argument in the question command. It has the same indent as a numbered question because the default value of the optional argument is quad.
}
\subquestion{
This non-numbered subquestion doesn't have the same indent as a numbered subquestion, because the absent optional argument of subquestion passes an empty argument as an the optional argument of the question command, instead of passing no argument at all. If I set the default value of the optional argument of subquestion to quad, the problem is resolved, but I would like this arborescing structure to be editable without having to find all the concerned commands.
}
\end{document}
答案2
使用根据\newcommand
该过程定义的宏可选参数,您无法区分由于没有提供可选参数而使用的可选参数的默认值的情况和明确提供默认值的情况。
但是您可以定义一个宏,用于\kernel@ifnextchar
检测可选参数的存在并相应地分叉。
\documentclass{article}
\makeatletter
\@ifdefinable\question{%
\DeclareRobustCommand\question{%
\kernel@ifnextchar{[}{\questionAToptarg}{\questionATNOoptarg}%
}%
}%
\newcommand{\questionATNOoptarg}[1]{%
\begin{description}%
\item\hspace{-\labelsep}#1\end{description}%
}%
\newcommand{\questionAToptarg}[2][]{%
\begin{description}%
\item[#1]#2\end{description}%
}%
\@ifdefinable\subquestion{%
\DeclareRobustCommand\subquestion{%
\kernel@ifnextchar{[}{\subquestionAToptarg}{\subquestionATNOoptarg}%
}%
}%
% Nesting \question yields nesting description-environments which in
% turn yields inserting vertical \topsep/\partopsep-glue (calculated
% to \@topsepadd) twice. One of these insertions needs to be
% annihilated.
\newcommand{\subquestionATNOoptarg}[1]{%
\question{\vspace{-\@topsepadd}\vspace{-\baselineskip}\question{#1}}%
}%
\newcommand{\subquestionAToptarg}[2][]{%
\question{\vspace{-\@topsepadd}\vspace{-\baselineskip}\question[#1]{#2}}%
}%
\makeatother
\begin{document}
\question[1.]{This is question one. This is question one.
This is question one. This is question one.
This is question one. This is question one.
This is question one. This is question one.}%
\subquestion[a)]{It has a subquestion. It has a subquestion.
It has a subquestion. It has a subquestion.
It has a subquestion. It has a subquestion.
It has a subquestion. It has a subquestion.}%
\question{%
This is a non-numbered question, which is obtained by omitting the optional
argument in the question command. It has the same indent as a numbered
question.%
}%
\subquestion{%
This non-numbered subquestion does have the same indent as a numbered
subquestion. The problem has nothing to do with \texttt{\string\quad}.
This arborescing structure is based on the \LaTeX-kernel's
\texttt{\string\list}-command which is described in source2e.pdf.
(Google source2e.pdf if interested.)%
}%
\end{document}
或者:
\documentclass{article}
\makeatletter
\newcommand{\question}[2][\hspace{-\labelsep}]{%
\begin{description}%
\item[#1]#2\end{description}%
}%
\newcommand{\subquestion}[2][\hspace{-\labelsep}]{%
\question[\hspace{-\itemindent}\hspace{-\labelsep}]{\question[#1]{#2}}%
}%
\makeatother
\begin{document}
\question[1.]{This is question one. This is question one.
This is question one. This is question one.
This is question one. This is question one.
This is question one. This is question one.}%
\subquestion[a)]{It has a subquestion. It has a subquestion.
It has a subquestion. It has a subquestion.
It has a subquestion. It has a subquestion.
It has a subquestion. It has a subquestion.}%
\question{%
This is a non-numbered question, which is obtained by omitting the optional
argument in the question command. It has the same indent as a numbered
question.%
}%
\subquestion{%
This non-numbered subquestion does have the same indent as a numbered
subquestion. The problem has nothing to do with \texttt{\string\quad}.
This arborescing structure is based on the \LaTeX-kernel's
\texttt{\string\list}-command which is described in source2e.pdf.
(Google source2e.pdf if interested.)%
}%
\end{document}
或者:
\documentclass{article}
\makeatletter
\newcommand{\question}[2][\hspace{-\itemindent}\hspace{-\labelsep}]{%
\begin{description}%
\item[#1]#2\end{description}%
}%
\newcommand{\subquestion}[2][\hspace{-\itemindent}\hspace{-\labelsep}]{%
\question{\question[#1]{#2}}%
}%
\makeatother
\begin{document}
\question[1.]{This is question one. This is question one.
This is question one. This is question one.
This is question one. This is question one.
This is question one. This is question one.}%
\subquestion[a)]{It has a subquestion. It has a subquestion.
It has a subquestion. It has a subquestion.
It has a subquestion. It has a subquestion.
It has a subquestion. It has a subquestion.}%
\question{%
This is a non-numbered question, which is obtained by omitting the optional
argument in the question command. It has the same indent as a numbered
question.%
}%
\subquestion{%
This non-numbered subquestion does have the same indent as a numbered
subquestion. The problem has nothing to do with \texttt{\string\quad}.
This arborescing structure is based on the \LaTeX-kernel's
\texttt{\string\list}-command which is described in source2e.pdf.
(Google source2e.pdf if interested.)%
}%
\end{document}
或者:
\documentclass{article}
\makeatletter
\newcommand\exchange[2]{#2#1}%
\newcommand{\question}[2][]{%
\begin{description}%
\begingroup
\@tempdima=-\itemindent\relax
\advance\@tempdima-\labelsep\relax
\settowidth\@tempdimb{#1}%
\ifdim\@tempdima>\@tempdimb
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{%
\expandafter\exchange\expandafter{\the\@tempdima}{\endgroup\item[\hbox to}{#1\hss}]#2\end{description}%
}{%
\endgroup
\item[#1]#2\end{description}%
}%
}%
\newcommand{\subquestion}[2][]{%
\question{\question[#1]{#2}}%
}%
\makeatother
\begin{document}
\question[1.]{This is question one. This is question one.
This is question one. This is question one.
This is question one. This is question one.
This is question one. This is question one.}%
\question[111.]{This is not question one. This is not question one.
This is not question one. This is not question one.
This is not question one. This is not question one.
This is not question one.}%
\question[111111.]{This is not question one. This is not question one.
This is not question one. This is not question one.
This is not question one. This is not question one.
This is not question one.}%
\subquestion[a)]{It has a subquestion. It has a subquestion.
It has a subquestion. It has a subquestion.
It has a subquestion. It has a subquestion.
It has a subquestion. It has a subquestion.}%
\subquestion[aa)]{It has a subquestion. It has a subquestion.
It has a subquestion. It has a subquestion.
It has a subquestion. It has a subquestion.
It has a subquestion. It has a subquestion.}%
\subquestion[aaaaaaaaaaaaaaa)]{It has a subquestion. It has a subquestion.
It has a subquestion. It has a subquestion.
It has a subquestion. It has a subquestion.
It has a subquestion. It has a subquestion.}%
\question{%
This is a non-numbered question, which is obtained by omitting the optional
argument in the question command. It has the same indent as a numbered
question.%
}%
\subquestion{%
This non-numbered subquestion does have the same indent as a numbered
subquestion. The problem has nothing to do with \texttt{\string\quad}.
This arborescing structure is based on the \LaTeX-kernel's
\texttt{\string\list}-command which is described in source2e.pdf.
(Google source2e.pdf if interested.)%
}%
\end{document}