27

I am attempting to construct a new environment which will allow me to format arbitrary list types (itemize, enumerate, description etc.) with the new environment. As such I am passing it as a parameter to the new environment but it complains when I attempt the following:

\newenvironment{mylist}[1]
{
\begin{#1}
#Environment definition here
}
{\end{#1}}

I get the following error:

! Illegal parameter number in definition of \endmylist.

                 1

l.15 {\end{#1}}

How can I pass my parameters into the end block?

Stephen L
  • 373

2 Answers2

27

One possibility is to use the xparse package:

\usepackage{xparse}
\NewDocumentEnvironment{mylist}{m}{%
  \begin{#1}%
  % other code
}{%
  \end{#1}%
}
Philipp
  • 17,641
  • 8
    Thank you, very useful package. As a side note for everyone who, like me, doesn't know xparse the {m} parameter is specified just like table columns. The m stands for "mandatory parameter". – Riccardo T. Oct 29 '11 at 14:00
  • 2
    As of October 2021, loading xparse is not needed, as the code has been incorporated in the kernel – egreg Feb 17 '23 at 20:24
1

The environ package is an alternative way to achieve this, since it negotiates the environment content like a macro (called \BODY) and lets you place arguments around it as needed. Here's an example:

enter image description here

\documentclass{article}

\usepackage{environ,lipsum}

\NewEnviron{myenv}[1]{% \textbullet~Some code #1 \textbullet\par% Use argument before \BODY ~ \begin{myenv} \BODY \par \textbullet~Some code #1 \textbullet% Use argument after \BODY ~ \end{myenv} }

\begin{document}

\lipsum[1][1]

\begin{myenv}{argument} \lipsum[1][2-5] \end{myenv}

\lipsum[1][6]

\end{document}

Werner
  • 603,163