# Custom environment with minipages causes “Illegal parameter number in definition of \end” [duplicate]

Using MiKTeX with PDFLaTeX on Windows

I want to create an environment that contains two minipages; the first one has a description item, and the second has a small text (or picture). This piece of code works as I want:

{
\noindent
\begin{minipage}{0.5\textwidth}
\begin{description}
\item[Label] Description
\end{description}
\end{minipage}\hfill
\begin{minipage}{0.3\textwidth}
Second minipage
\end{minipage}
}


I wanted to create a macro or environment to make it easy for me to introduce this kind of content. So I create the following environment with two arguments:

\newenvironment{myenvironment}[2]% 1:label, 2:second minipage text
{%
\noindent
\begin{minipage}{0.5\textwidth}
\begin{description}
\item[#1] % Description
}%
{%
\end{description}
\end{minipage}\hfill
\begin{minipage}{0.3\textwidth}
#2
\end{minipage}
}


But when I try to use it with

\begin{myenvironment}{Label}{Second minipage text}
Description text
\end{myenvironment}


I get Illegal parameter number in definition of \endmyenvironment. pointing to the line that finishes the newenvironment declaration. After some hours struggling with this I tried using double backslashes (##) and got two new different errors: You can't use macro parameter character #' in restricted horizontal mode and You can't use macro parameter character #' in internal vertical mode. This time the errors were pointing to the lines when I wanted to use the environment, but had no complains on the definition.

What am I doing wrong?

• This issue is known - the arguments/parameters are not visible within the \end part of the environment definition. xparse makes it available, or you should use environ. Alternatively, define the arguments in macros that you use within the \end segment. – Werner Jul 11 '17 at 22:46
• Thanks a lot @Werner. Had just found out and posted my own answer beforse seeing your comments. I will check those packages! – aguadopd Jul 11 '17 at 23:05

The ## had nothing to do with my problem. It seems that you cannot use arguments inside of the closing or ending code of a custom environment. There are workarounds for this, but in my opinion this should be a must have feature. Using a possible workaround ---an auxiliary variable---, I arrived to this working environment:

\newenvironment{myenvironment}[2]%
{%
\def\myenvargumentII{#2}
\noindent
\begin{minipage}{0.5\textwidth}
\begin{description}
\item[#1] % Description
}%
{%
\end{description}
\end{minipage}\hfill
\begin{minipage}{0.3\textwidth}
\myenvargumentII
\end{minipage}
}


This solution arrived late. In the meantime I had managed to solve my problem with a \newcommanda with 3 arguments:

\newcommand{\myminipages}[3]% 1:label, 2:secondminipage, 3:Description
{%
\noindent
\begin{minipage}{0.5\textwidth}
\begin{description}
\item[#1] #3
\end{description}
\end{minipage}\hfill
\begin{minipage}{0.3\textwidth}
#2
\end{minipage}
\medskip % vertical space
}


Which can be used with:

\myminipages{Label}{Second minipage text}{Description text}


Hope it helps.

[a] It seems that there is no particular advantage of the newcommand approach over the newenvironment approach, at least for this use. More here and here.