[Tex/LaTex] \newcommand with optional multi-line argument and implicit itemize environment

itemizelistsmacrosoptional arguments

While drafting a text, I want to subdivide and group text pieces and keywords to easily reorder them throughout the document.

Examples for such a group of text pieces would be:


Example 1): Two arguments (2nd is optional and multi-line)

Having in the LaTeX file something like…

\mycommand{Summary of this paragraph (always provided)} {
    optional textline 1
    optional textline 2
    ...
}

…should result in the pdf as…

Summary of this paragraph (always provided)

  • optional textline 1
  • optional textline 2

Example 2): One argument

Having in the LaTeX file something like…

\mycommand{Summary of this paragraph (always provided)}

…should result (without optional arguments) in the pdf as…

Summary of this paragraph (always provided)


I tried already some things with \newcommand but I'm not a real expert.

The needed features would be

  • \mycommand gets at least one argument and prints this to the PDF (e.g. in bold letters)
  • \mycommand takes a variable number of following lines (in parenthesis) as second argument and adds for each new line an \item to an \itemize list (without writing the \item in the .tex-file)
  • Changing the \mycommand to display only text would then finally generate a running text without the need of removing the \item commands.

Any help appreciated.

Best Answer

You can profit of xparse and LaTeX3 functions.

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\outline}{ m o }
 {
  \par\noindent\textbf{#1}
  \IfNoValueTF { #2 }
   {
    \par\vspace{\topsep} % no list of items
   }
   {
    \egreg_outline_items:n { #2 } % there are items
   }
 }

\seq_new:N \l_egreg_outline_items_seq
\cs_new_protected:Npn \egreg_outline_items:n #1
 {
  % split the \\ separated list of items
  \seq_set_split:Nnn \l_egreg_outline_items_seq { \\ } { #1 }
  % make an itemize with them
  \begin{itemize}
  \seq_map_inline:Nn \l_egreg_outline_items_seq
   {
    \item ##1
   }
  \end{itemize}
 }
\ExplSyntaxOff

\begin{document}

\section{First}

\outline{Some text for the outline}

Here is something in the section.

\section{Second}

\outline{Here I have more}[
  something \\
  something else \\
  again
]

Here is something in the section.

\end{document}

enter image description here

Related Question