# How can I wrap this code in a function?

I have a text that describes spell powers in a game. The following pattern is repeated for each spell:

\documentclass{article}
\begin{document}

% Template for spell powers.
\underline{0 ниво} \\   % Underlined, unindented.
\begin{itemize}
\item{example}        % Indented bullet points.
\item{example 2}      % Random number of bullet points.
\end{itemize}
\underline{1 ниво} \\
%\begin{itemize}        % Error when no items!
%\end{itemize}
\underline{2 ниво} \\
\underline{3 ниво} \\
\underline{4 ниво} \\
\begin{itemize}
\item{example 3}      % Bullet points under any underlined word.
\end{itemize}
\underline{5 ниво} \\
\underline{6 ниво} \\   % Last underlined item.

\end{document}


I would like to have the pattern declared somewhere and then used from the whole file. I expect to need less typing that way, and be able to make changes to the template in only one place. How can this be achieved?

Note about the bullet point text that (1) there can be between 0 and many bullet points under an underlined word and (2) the bullet-point text is from 20 to 200 characters

EDIT:

Expected parameters:

#1 - small heading - just bolded. 10-100 chars
#2 - #8 - an itemization between 0 and many items with 20 to 200 chars - one for each underlined word

• It is not very clear what you want to have. What are your parameters? Ok, the itemize below the first underline, check. But the rest stays as it? What is the empty \textbf{} for? Do you want auto-numbering? Please delete everything you do not need, complete your code to a full but minimal document and use #1, #2... for every parameter you need. Afterwards you can tell us e.g.: "#1 should be an itemization between 0 and many items with 20 to 200 chars". Mar 3 '15 at 7:39
• @LaRiFaRi, any better now? Mar 3 '15 at 8:26
• @Vorac: You mention #2-#9 (8 elements) to be an itemization, yet your example code has what seems like only 7 elements (0 ниво-6 ниво). Do you need control over the style of X ниво as well, or would something like \something{<title>}{<list1>}{<list2>}{<list3>}...{<list8>} (note that I only go up to 8 here since you're only allowed 9 parameters total by default) be sufficient; <listX> can be left empty. Mar 3 '15 at 15:26

This solution is based on one of egreg's answers: https://tex.stackexchange.com/a/136133/46716 and uses xparse from the LaTeX3 Project.

It defines a command \spell which takes as optional arguments the examples separated by a semi-colon.

\documentclass{article}
\usepackage{xparse}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}

\ExplSyntaxOn
\newcounter{spells}
\NewDocumentCommand{\spell}{o}{
\par\noindent\underline{\thespells\ ниво}
\stepcounter{spells}
\IfNoValueTF{#1}{
\par\vspace{\topsep}
}{
\list_examples:n{#1}
}
}

\cs_new:Npn \list_examples:n #1{
\seq_set_split:Nnn \splitted_seq{;}{#1}
\begin{itemize}
\seq_map_inline:Nn \splitted_seq{
\item ##1
}
\end{itemize}
}
\ExplSyntaxOff

\begin{document}

\spell[example; example 2]
\spell
\spell
\spell
\spell[example 3]
\spell
\spell

\end{document}


EDIT: to your request of having the entire pattern in 1 macro:

\documentclass{article}
\usepackage{xparse}
\usepackage{ifmtarg}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}

\ExplSyntaxOn
\makeatletter
\newcommand{\spell}[8]{
\par\noindent\textbf{#1}
\par\noindent\underline{0\ ниво}
\@ifmtarg{#2}{\par\vspace{\topsep}}{\list_examples:n{#2}}
\par\noindent\underline{1\ ниво}
\@ifmtarg{#3}{\par\vspace{\topsep}}{\list_examples:n{#3}}
\par\noindent\underline{2\ ниво}
\@ifmtarg{#4}{\par\vspace{\topsep}}{\list_examples:n{#4}}
\par\noindent\underline{3\ ниво}
\@ifmtarg{#5}{\par\vspace{\topsep}}{\list_examples:n{#5}}
\par\noindent\underline{4\ ниво}
\@ifmtarg{#6}{\par\vspace{\topsep}}{\list_examples:n{#6}}
\par\noindent\underline{5\ ниво}
\@ifmtarg{#7}{\par\vspace{\topsep}}{\list_examples:n{#7}}
\par\noindent\underline{6\ ниво}
\@ifmtarg{#8}{\par\vspace{\topsep}}{\list_examples:n{#8}}
}
\makeatother

\cs_new:Npn \list_examples:n #1{
\seq_set_split:Nnn \splitted_seq{;}{#1}
\begin{itemize}
\seq_map_inline:Nn \splitted_seq{
\item ##1
}
\end{itemize}
}
\ExplSyntaxOff

\begin{document}

\spell{Small Heading}{example; example 2}{}{}{example 3}{}{}{}

\end{document}


This gives you a macro \spell with 8 arguments. The first is the heading, and the other 7 are the examples. Multiple examples can be separated with a semi-colon. So

\spell{Small Heading}{example; example 2}{}{}{example 3}{}{}{}


produces:

• Thanks for the answer. Actually my requirement is to be able to replicate the whole structure, much as @Werner explains above. Mar 4 '15 at 9:23
• @Vorac Will your underlined words always be 0-6? Or could it also be 0-8? Mar 4 '15 at 9:28
• Yes, the underlined words are always the same. One more small request - please put some comments in the key points of the code. I am new to Latex, but would like to understand the code, before using it. Mar 4 '15 at 9:34
• @Vorac I edited my answer. Your entire pattern is now in one single macro. Is that what your looking for? Mar 4 '15 at 10:37
• Wow, this is extremely powerful. I am trying to imagine having 200 "spells" written in one format, and needing to consistently re-format them a new format in a text file or a LibreOffice Writer. Apr 6 '15 at 8:59