Discussion:
Listings: lstenvironments and captions
(too old to reply)
m***@hio.no
2007-07-03 07:38:46 UTC
Permalink
Hello

The problem pointed at below is actually a LaTeX issue, not a Listings
one.
Still, I meet it within a Listings lstenvironment context, and choose
to describe it this way, as maybe a workaround can be found through
Listings.

I am building some lstenvironments, for which I would like the
captions to be augmented and formatted automatically, by a macro,
\eksempel.
\eksempel is defined with \newcommand{\eksempel}{....} (See below).

Here is an example environment: (see line with %!!!! below)
--------------------------------------------------------------------------
\lstnewenvironment{barephp}[2]{%
\lstset{
label=#1,
language=php,
caption=\eksempel{#2}, %!!!!
float=h,
}
} {}
----------------------------------------------------------------------------

Here is the definition of \eksempel (\eksempel works fine on its
own):
---------------------------------------------------------------
% define a counter first
\newcounter{eksemp}[chapter]
\renewcommand{\theeksemp}{\thechapter.\Alph{eksemp}}

\newcommand{\eksempel}[1]{
\refstepcounter{eksemp}
\emph{\textbf{Eksempel \theeksemp:} #1}
}
----------------------------------------------------------------

\eksempel works fine when called on its own. \eksempel{some text}.
So does
\begin{barephp}{some label}{some text}
when
caption=#2.

But when
caption=\eksempel{#2},
as in %!!!!! above, I get the error message:
--------------------------------------------------------------------
! Missing \endcsname inserted.
<to be read again>
\csname\endcsname
l.172 \begin{barephp}{some label}{some text}
----------------------------------------------------------------------

Is there any workaround
here?

Thanks
...

Michael
Axel Sommerfeldt
2007-07-03 15:21:58 UTC
Permalink
Is there any workaround here?
You must take care of the fact that an argument to \caption (or caption= in
listings package) is fragile, could be evaluated more-than-once, will be
put into a \hbox for width calculation etc.

Maybe the easiest solution would be taking the/my caption package for that.
(It coorparates with the listings package as well.)

HTH,
Axel
Axel Sommerfeldt
2007-07-03 15:24:12 UTC
Permalink
Post by Axel Sommerfeldt
You must take care of the fact that an argument to \caption (or
caption= in listings package) is fragile
Sorry, must (of course) read "should not be fragile" instead of "is
fragile" ;-)

Axel
Dan
2007-07-03 17:18:41 UTC
Permalink
Post by m***@hio.no
Here is an example environment: (see line with %!!!! below)
--------------------------------------------------------------------------
\lstnewenvironment{barephp}[2]{%
\lstset{
label=#1,
language=php,
caption=\eksempel{#2}, %!!!!
float=h,
}
} {}
Try
caption=\protect\exsemple{#2},

OR
Post by m***@hio.no
\newcommand{\eksempel}[1]{
\DeclareRobustCommand{\eksemple}[1]{


Dan

Loading...