4

I'm trying to define a command with \NewDocumentCommand that create a subsection with a given name (mandatory) and optionally some descriptive paragraph. Since I don't want these paragraph to be printed out when they're empty, I've tried taking advantage of the \IfBlankF functionality by excluding the paragraph altogether if their respective content is empty.

The command I've defined is as follows:

\documentclass{article}

\begin{document}
    \NewDocumentCommand{\newrule}{ m O{} O{} O{} O{} }{ 
        \subsubsection{#1}
        \IfBlankF{#2}{
            \paragraph{Addendum}
            #2
        }
        \IfBlankF{#3}{
            \paragraph{Rationale}
            #3
        }
        \IfBlankF{#4}{
            \paragraph{Examples}
            #4
        }
        \IfBlankF{#5}{
            \paragraph{Exceptions}
            #5
        }
    }

    \section{Section}
    \subsection{Subsection}
    \newrule{Rule 1}{blah blah}{}{}{}
 
\end{document}

This works almost as intended, except that whenever the arguments are non empty, the paragraph name leading up to them are not present. The same happens with any other kind of content replacing the \paragraph{...} part.

However, if I invert the behavior by e.g. writing

\IfBlankF{#2}{
    \paragraph{Addendum} abcdef
    #2
}

both the paragraph names and subsequent content works. What am I doing wrong?

7
  • Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for the users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. Commented Jul 8 at 12:49
  • @samcarter_is_at_topanswers.xyz edited, thx
    – bru3s
    Commented Jul 8 at 12:57
  • You defined optional arguments, so you should write \newrule{Rule 1}[blah blah][][][] or \newrule{Rule 1}[blah blah] Commented Jul 8 at 12:59
  • 1
    It is usually a bad design to have consecutive optional arguments as you can not omit them in arbitrary combinations. Commented Jul 8 at 13:02
  • @samcarter_is_at_topanswers.xyz I see, that was it. Thx, I also see David's point, and should probably go for another approach.
    – bru3s
    Commented Jul 8 at 13:18

1 Answer 1

3

You specified four optional arguments that should go in brackets [].

Anyway, I don't think it's a good interface.

\documentclass{article}

\NewDocumentCommand{\newrule}{ m O{} O{} O{} O{} }{%
    \subsubsection{#1}
    \IfBlankF{#2}{%
        \paragraph{Addendum}
        #2%
    }
    \IfBlankF{#3}{%
        \paragraph{Rationale}
        #3%
    }
    \IfBlankF{#4}{%
        \paragraph{Examples}
        #4%
    }
    \IfBlankF{#5}{%
        \paragraph{Exceptions}
        #5%
    }%
}

\begin{document}

\section{Section}

\subsection{Subsection}

\newrule{Rule 1}[blah blah]
 
\newrule{Rule 2}[][blah blah]
 
\newrule{Rule 3}[][][blah blah]
 
\newrule{Rule 4}[][][][blah blah]
 
\end{document}

output

I'd use a key-value interface:

\documentclass{article}

\ExplSyntaxOn

\keys_define:nn { brusamento/rule }
 {
  addendum .tl_set:N = \l_brusamento_rule_addendum_tl,
  rationale .tl_set:N = \l_brusamento_rule_rationale_tl,
  examples .tl_set:N = \l_brusamento_rule_examples_tl,
  exceptions .tl_set:N = \l_brusamento_rule_exceptions_tl,
 }

\NewDocumentCommand{\newrule}{ m O{} }
 {
  \subsubsection{#1}
  \group_begin: % keep the keys local
  \keys_set:nn { brusamento/rule } { #2 }
  \tl_if_blank:VF \l_brusamento_rule_addendum_tl
   {
    \paragraph{Addendum}
    \tl_use:N \l_brusamento_rule_addendum_tl
   }
  \tl_if_blank:VF \l_brusamento_rule_rationale_tl
   {
    \paragraph{Rationale}
    \tl_use:N \l_brusamento_rule_rationale_tl
   }
  \tl_if_blank:VF \l_brusamento_rule_examples_tl
   {
    \paragraph{Examples}
    \tl_use:N \l_brusamento_rule_examples_tl
    }
  \tl_if_blank:VF \l_brusamento_rule_exceptions_tl
   {
    \paragraph{Exceptions}
    \tl_use:N \l_brusamento_rule_exceptions_tl
   }
  \group_end:
}

\ExplSyntaxOff

\begin{document}

\section{Section}

\subsection{Subsection}

\newrule{Rule 1}[addendum=blah blah]
 
\newrule{Rule 2}[rationale=blah blah]
 
\newrule{Rule 3}[examples=blah blah]
 
\newrule{Rule 4}[exceptions=blah blah]

\end{document}
1
  • Thanks for the write-up of the alternative approach, much appreciated. I'll definitely be using that from now on.
    – bru3s
    Commented Jul 8 at 14:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .