1

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}

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

enter image description here

2

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

enter image description here

| improve this answer | |
1

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]{%
  \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}

enter image description here

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.
  (Google source2e.pdf if interested.)%
}%

\end{document}

enter image description here

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.
  (Google source2e.pdf if interested.)%
}%

\end{document}

enter image description here

Alternatively:

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

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.