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?

1

2 Answers 2

28

One possibility is to use the xparse package:

\usepackage{xparse}
\NewDocumentEnvironment{mylist}{m}{%
  \begin{#1}%
  % other code
}{%
  \end{#1}%
}
2
  • 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". Commented Oct 29, 2011 at 14:00
  • 2
    As of October 2021, loading xparse is not needed, as the code has been incorporated in the kernel
    – egreg
    Commented Feb 17, 2023 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}
1
  • 1
    I’d say that environ is obsolete
    – egreg
    Commented Feb 17, 2023 at 20:20

You must log in to answer this question.

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