# Can you pass no optional argument to a command inside a command?

Is there any way to pass no value for the optional argument of a command called in another command, instead of passing an empty argument ? I've compiled my code to better explain my issue.

\documentclass{article}

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


• Your MWE shows \item being used with a curly brace, which is not its native syntax. – Steven B. Segletes Apr 22 '20 at 2:00
• True thanks. I've edited, however the result is the same. – James Well Apr 22 '20 at 2:03
• Are you aware of ctan.org/topic/exercise – cgnieder Apr 22 '20 at 6:03

Not sure I've fully comprehended the desired behavior, but maybe this. It involves testing for an optional argument and branching accordingly.

\documentclass{article}

\newcommand{\question}[2][\relax]{%
\begin{description}
\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}


With macros defined in terms of \newcommand that process an optional argument you cannot distinguish the case of the default-value of the optional argument being used due to no optional argument being provided from the case of the default-value being provided explicitly.

But you can define a macro which uses \kernel@ifnextchar for detecting the presence of an optional argument and forking accordingly.

\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]{%
}%
\newcommand{\subquestionAToptarg}[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.
}%

\end{document}


Alternatively:

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

\end{document}


Alternatively:

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

\end{document}


Alternatively:

\documentclass{article}

\makeatletter

\newcommand\exchange[2]{#2#1}%

\newcommand{\question}[2][]{%
\begin{description}%
\begingroup
\@tempdima=-\itemindent\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.