Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem with new environment: illegal parameter

506 views
Skip to first unread message

Pieter Rautenbach

unread,
Sep 8, 2004, 6:57:42 AM9/8/04
to
Hallo everyone

I have two problems concerning the declaration of a new environment.
The first snippet given here has a problem with the #1 and #2 as far
as I could figure it out. I looked for help on the web -- one
reference said something about using ## , but I didn't understand it
well enough.

\newenvironment{minipagefig}[2]
{
\begin{figure}
\centering
\begin{minipage}{0.9\textwidth}
}
{
\end{minipage}
\caption{\textit{#1} \label{fig:#2}}
\end{figure}
}

! Illegal parameter number in definition of \endminipagefig.
<to be read again>
1

This snippet, similar to the first, has a problem with the braces of
\fbox. If I take away the lines containing \fbox{ and its partner },
it works fine. The way I understand it, LaTeX should not have a
problem parsing it.

\newenvironment{minipagefig}[2]
{
\begin{figure}
\centering
\fbox{
\begin{minipage}{0.9\textwidth}
}
{
\end{minipage}
}%\fbox
\end{figure}
}

! Extra }, or forgotten \endgroup.
\fbox ...ox {\color@begingroup \kern \fboxsep {#1}
\kern \fboxsep
\color@endg...

Any help will be appreciated. I this isn't clear enough, I will try
and restate my problem.

Pieter Rautenbach

Hendri Adriaens

unread,
Sep 8, 2004, 7:24:51 AM9/8/04
to
> \newenvironment{minipagefig}[2]
> {
> \begin{figure}
> \centering
> \begin{minipage}{0.9\textwidth}
> }
> {
> \end{minipage}
> \caption{\textit{#1} \label{fig:#2}}
> \end{figure}
> }

Parameters cannot be used in the \end{environment}
part of the definition.

\makeatletter
\newenvironment{minipagefig}[2]
{
\def\@tempa{#1}%
\def\@tempb{#2}%


\begin{figure}
\centering
\begin{minipage}{0.9\textwidth}
}
{
\end{minipage}

\caption{\textit{\@tempa} \label{fig:\@tempb}}
\end{figure}
}

> \newenvironment{minipagefig}[2]
> {
> \begin{figure}
> \centering
> \fbox{
> \begin{minipage}{0.9\textwidth}
> }

This last brace closes the fbox, the env. def
is not closed properly!

Do (for instance):
\newsavebox\mybox
\renewenvironment{minipagefig}[2]
{
\begin{figure}
\centering
\savebox\mybox\bgroup


\begin{minipage}{0.9\textwidth}
}
{
\end{minipage}

\egroup
\fbox{\usebox{\mybox}}
\end{figure}
}

-Hendri.


David Carlisle

unread,
Sep 8, 2004, 7:35:41 AM9/8/04
to
{
\end{minipage}
\caption{\textit{#1} \label{fig:#2}}
\end{figure}
}

! Illegal parameter number in definition of \endminipagefig.
<to be read again>

you can only use the arguments in the begin part of an environment
definition. If you want to use them at the end save them in a macro;

that is use
\def\tempone{#1}
\def\temptwo{#2}
in the begin-part and then use \tempone in the end part.

The way I understand it, LaTeX should not have a
problem parsing it.

\newenvironment{minipagefig}[2]
{
\begin{figure}
\centering
\fbox{
\begin{minipage}{0.9\textwidth}
}

latex, as you found will have a problem: brace groups have to match up
there is no way latex (or anything else) can know that the final } in
the code above is intended to match the { before begin{figure} rather
than the { after fbox.

You need to use lrbox, the environment form of sbox which was introduced
for just this reason. Or use a boxed minipage environment from 9eg) the
boxedminipage package.

David

Martin Vaeth

unread,
Sep 8, 2004, 7:37:38 AM9/8/04
to
Pieter Rautenbach <p...@sun.ac.za> schrieb:

>
> I have two problems concerning the declaration of a new environment.
> The first snippet given here has a problem with the #1 and #2 as far
> as I could figure it out.

One can use parameters only in the "begin" part of the environment.

> This snippet, similar to the first, has a problem with the braces of
> \fbox. If I take away the lines containing \fbox{ and its partner },
> it works fine. The way I understand it, LaTeX should not have a
> problem parsing it.

No, TeX can only parse balanced braces. It is in general not possible
to use the environment as a parameter of a macro
(although some tricks have been invented to avoid this restriction
somewhat, but if possible you should avoid these tricks).
In your particular situation you might instead use an lrbox environment
to collect the environment in a box register and use your fbox
command with that register at the end of your environment.

Ulrich M. Schwarz

unread,
Sep 8, 2004, 7:32:08 AM9/8/04
to
p...@sun.ac.za (Pieter Rautenbach) writes:

> Hallo everyone
>
> I have two problems concerning the declaration of a new environment.
> The first snippet given here has a problem with the #1 and #2 as far
> as I could figure it out. I looked for help on the web -- one
> reference said something about using ## , but I didn't understand it
> well enough.
>
> \newenvironment{minipagefig}[2]
> {
> \begin{figure}
> \centering
> \begin{minipage}{0.9\textwidth}
> }
> {
> \end{minipage}
> \caption{\textit{#1} \label{fig:#2}}
> \end{figure}
> }

The parameters at the beginning of the environment are only visible
there, not at the end. (Internally, an environment is much like two
commands \minipagefig{the}{args}{you}{give} and \endminipagefig{}.)
So, you'll have to store the parameters yourself if you still need
them:

\newcommand\mycaption{}
\newcommand\mylabel{}

\newenvironment{minipagefig}[2]{%
\renewcommand{\mycaption}{#1}%
\renewcommand{\mylabel}{#2}%
\begin{figure}%
\centering
\begin{minipage}{0.9\textwidth}%
}{%
\end{minipage}%
\caption{\textit{\mycaption}\label{fig:\mylabel}}%
\end{figure}%
}

(Note that I've removed a spurious space between caption and label and
added some % to prevent some other spaces. My motto is: in macros,
better have too many % than too few -- missing space is easier to see
in the output.)

HTH
Ulrich
--
Getting lost in the \footnotes and \temporals? Talcum makes LaTeX more fun.
Now even less bugs, and, of course, still with special Beamer support.
http://talcum.sarovar.org/ (Current release: 0.4.1, 20040904)

0 new messages