您不能在命令内部向命令传递任何可选参数吗?

您不能在命令内部向命令传递任何可选参数吗?

有没有办法不传递另一个命令中调用的命令的可选参数的值,而不是传递一个空参数?我已经编译了我的代码以更好地解释我的问题。

\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}

在此处输入图片描述

相关内容