2

If I am using this simple new command...

\newcommand{\Lem}[3]{
\begin{lemma}
{#1}
\begin{itemize}
\item{#2}
\item{#3}
\end{itemize}
\end{lemma}
}

how can I use it if for example I only have one item in the list part. Thanks

2
  • 2
    note item does not take an argument so \item #2 not \item{#2} also take care to use % at ends of lines to avoid adding extra white space. Commented Jan 21 at 15:15
  • 2
    Please always include a complete MWE. Don't make other users do more work. Start with \documentclass, include all relevant \usepackage commands, and end with \end{document}.
    – Sandy G
    Commented Jan 21 at 15:16

4 Answers 4

6

You need some syntax to separate the items, you could use {..}{...} or (if there are no commas in the items {... , ... , ...} but I would use the standard latex syntax \item ... \item ... then you just need

\newcommand{\Lem}[2]{%<<<
\begin{lemma}%<--
{#1}%
\begin{itemize}
#2
\end{itemize}%<
\end{lemma}%<
}

\Lem{foo}{\item this  \item that}


Although generally I'd avoid hiding environments in commands, environments are a core part of latex syntax and you can get better context syntax help, and more functionality, eg use of \verb or minted if you use an environment.

\newenvironment{\Lem}[]{%<<<
\begin{lemma}%<--
{#1}%
\begin{itemize}%
}
{%
\end{itemize}%<
\end{lemma}%<
}

\begin{Lem}{foo}
\item this
\item that
\end{Lem}

5

The command \Lem is defined with \NewDocumentCommand. The first argument is mandatory. The second till ninth argument are optional. It uses \IfValueT to test if an argument is empty.

enter image description here

\documentclass[border=6pt,varwidth]{standalone}
\usepackage{amsthm}
\newtheorem{lemma}{Lemma}
\NewDocumentCommand{\Lem}{moooooooo}{\begin{lemma}
#1
\IfValueT{#2}{\begin{itemize}
\IfValueT{#2}{\item #2}
\IfValueT{#3}{\item #3}
\IfValueT{#4}{\item #4}
\IfValueT{#5}{\item #5}
\IfValueT{#6}{\item #6}
\IfValueT{#7}{\item #7}
\IfValueT{#8}{\item #8}
\IfValueT{#9}{\item #9}
\end{itemize}}\end{lemma}}
\begin{document}
\Lem{Example 1} end
\Lem{Example 2}[item 1] end
\Lem{Example 3}[item 1][item 2][item 3][item 4] end
\end{document}
3

You can place the items in a loop, for example using \foreach from the pgffor package (included in tikz if you're using that). Note that if an item includes commas you must enclose it in braces.

enter image description here

\documentclass{article}

\usepackage{pgffor}

\newtheorem{lemma}{Lemma}

\newcommand{\Lem}[2]{
    \begin{lemma}{#1}
        \begin{itemize}
            \foreach \i in {#2}
                {\item \i}
        \end{itemize}
    \end{lemma}
}

\begin{document}

\Lem{Here is a lemma.}{Item.}
\Lem{Here is another lemma.}{Item 1., Item 2., {Items 3, 4, 5.}}

\end{document}  
1

A version closer to the original code, that checks if #3 is empty. In that case nothing is done, in the \else case an \item is added for the argument. This requires that the argument is always provided (\Lem{Here is a lemma.}{Item.}{} for example), unlike the anwer from matexmatics which uses actual optional arguments.

\documentclass{article}

\newtheorem{lemma}{Lemma}

\newcommand{\Lem}[3]{
    \begin{lemma}{#1}
        \begin{itemize}
            \item #2
            \if\relax\detokenize{#3}\else
               \item #3
            \fi
        \end{itemize}
    \end{lemma}
}

\begin{document}

\Lem{Here is a lemma.}{Item.}{}
\Lem{Here is another lemma.}{Item 1.}{Item 2.}

\end{document} 

You must log in to answer this question.

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