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

lstlisting environment inside another environment

358 views
Skip to first unread message

Didier Verna

unread,
Apr 29, 2008, 12:03:39 PM4/29/08
to

Hello,

does anybody have a hack already to be able to put a listing environment
inside another, user-defined one ? The following example shows the
problem:

\documentclass{article}

\usepackage{listings}

\newenvironment{myenv}{%
\begin{lstlisting}[language=C]}{%
\end{lstlisting}}

\begin{document}

\begin{myenv}
int foo;
\end{myenv}

\end{document}

didier(pts/0)% latex minimal.tex 18:00 04/29/08
This is pdfTeXk, Version 3.141592-1.40.3 (Web2C 7.5.6)
%&-line parsing enabled.
entering extended mode
(./minimal.tex
LaTeX2e <2005/12/01>
Babel <v3.8h> and hyphenation patterns for english, usenglishmax, dumylang, noh
yphenation, arabic, farsi, croatian, ukrainian, russian, bulgarian, czech, slov
ak, danish, dutch, finnish, basque, french, german, ngerman, ibycus, greek, mon
ogreek, ancientgreek, hungarian, italian, latin, mongolian, norsk, icelandic, i
nterlingua, turkish, coptic, romanian, welsh, serbian, slovenian, estonian, esp
eranto, uppersorbian, indonesian, polish, portuguese, spanish, catalan, galicia
n, swedish, ukenglish, pinyin, loaded.
(/usr/share/texmf-texlive/tex/latex/base/article.cls
Document Class: article 2005/09/16 v1.4f Standard LaTeX document class
(/usr/share/texmf-texlive/tex/latex/base/size10.clo))
(/usr/share/texmf-texlive/tex/latex/listings/listings.sty
(/usr/share/texmf-texlive/tex/latex/graphics/keyval.sty)
(/usr/share/texmf-texlive/tex/latex/listings/lstpatch.sty)
(/usr/share/texmf-texlive/tex/latex/listings/lstmisc.sty)
(/usr/share/texmf-texlive/tex/latex/listings/listings.cfg)) (./minimal.aux)
(/usr/share/texmf-texlive/tex/latex/listings/lstlang1.sty)
(/usr/share/texmf-texlive/tex/latex/listings/lstmisc.sty)
(/usr/share/texmf-texlive/tex/latex/base/omscmr.fd))
*X

*! Interruption.
<*>

? X
No pages of output.
Transcript written on minimal.log.
zsh: exit 1 latex minimal.tex


--
5th European Lisp Workshop at ECOOP 2008, July 7: http://elw.bknr.net/2008/

Didier Verna, did...@lrde.epita.fr, http://www.lrde.epita.fr/~didier

EPITA / LRDE, 14-16 rue Voltaire Tel.+33 (0)1 44 08 01 85
94276 Le Kremlin-Bicętre, France Fax.+33 (0)1 53 14 59 22 did...@xemacs.org

Ulrike Fischer

unread,
Apr 29, 2008, 12:21:52 PM4/29/08
to
Am Tue, 29 Apr 2008 18:03:39 +0200 schrieb Didier Verna:

> Hello,
>
> does anybody have a hack already to be able to put a listing environment
> inside another, user-defined one ? The following example shows the
> problem:

See documentation of listings:

4.16 Environments
If you want to define your own pretty-printing environments, try the
following command. The syntax comes from LATEX’s \newenvironment.
\lstnewenvironment

--
Ulrike Fischer

Didier Verna

unread,
Apr 29, 2008, 12:34:52 PM4/29/08
to
Ulrike Fischer <ne...@nililand.de> wrote:

Doesn't work either. OK, my minimal example was perhaps too minimal. I
need a listings environment inside another, not the other way around. It
is actually to put a listing inside a beamer block. Something like that:

\begin{block}<overlay>{title}
\begin{lstlisting}[all-sorts-of-options]
code...
\end{lstlisting}
\end{block}

I want a user-defined environment to do that, but it seems that listings
environments (whether explicit or created via \lstnewenvironment) don't
like to be put inside other ones.

--
5th European Lisp Workshop at ECOOP 2008, July 7: http://elw.bknr.net/2008/

EPITA / LRDE, 14-16 rue Voltaire Tel.+33 (0)1 44 08 01 85

94276 Le Kremlin-Bicêtre, France Fax.+33 (0)1 53 14 59 22 did...@xemacs.org

Didier Verna

unread,
Apr 29, 2008, 12:38:44 PM4/29/08
to
I wrote:

> Doesn't work either. OK, my minimal example was perhaps too minimal. I
> need a listings environment inside another, not the other way around. It
> is actually to put a listing inside a beamer block. Something like that:
>
> \begin{block}<overlay>{title}
> \begin{lstlisting}[all-sorts-of-options]
> code...
> \end{lstlisting}
> \end{block}
>
> I want a user-defined environment to do that, but it seems that listings
> environments (whether explicit or created via \lstnewenvironment) don't
> like to be put inside other ones.

And BTW, I can use \lstinputlisting instead of an environment (it
works in that case), but this is too boring for just a few lines of code.

--
5th European Lisp Workshop at ECOOP 2008, July 7: http://elw.bknr.net/2008/

EPITA / LRDE, 14-16 rue Voltaire Tel.+33 (0)1 44 08 01 85

94276 Le Kremlin-Bicętre, France Fax.+33 (0)1 53 14 59 22 did...@xemacs.org

Joseph Wright

unread,
Apr 29, 2008, 12:50:11 PM4/29/08
to
On Apr 29, 5:38 pm, Didier Verna <did...@lrde.epita.fr> wrote:
> I wrote:
> > Doesn't work either. OK, my minimal example was perhaps too minimal. I
> > need a listings environment inside another, not the other way around. It
> > is actually to put a listing inside a beamer block. Something like that:
>
> > \begin{block}<overlay>{title}
> > \begin{lstlisting}[all-sorts-of-options]
> > code...
> > \end{lstlisting}
> > \end{block}
>
> > I want a user-defined environment to do that, but it seems that listings
> > environments (whether explicit or created via \lstnewenvironment) don't
> > like to be put inside other ones.
>
> And BTW, I can use \lstinputlisting instead of an environment (it
> works in that case), but this is too boring for just a few lines of code.
>
> --
> 5th European Lisp Workshop at ECOOP 2008, July 7:http://elw.bknr.net/2008/
>
> Didier Verna, did...@lrde.epita.fr,http://www.lrde.epita.fr/~didier

>
> EPITA / LRDE, 14-16 rue Voltaire Tel.+33 (0)1 44 08 01 85
> 94276 Le Kremlin-Bicêtre, France Fax.+33 (0)1 53 14 59 22 did...@xemacs.org

This is all to do with catcodes, I think. I don't think it's possible
to nest lstlisting environments at all.

Joseph Wright

Herbert Voss

unread,
Apr 29, 2008, 12:50:46 PM4/29/08
to
Didier Verna schrieb:

> I want a user-defined environment to do that, but it seems that listings
> environments (whether explicit or created via \lstnewenvironment) don't
> like to be put inside other ones.
>

\documentclass{beamer}%
\usepackage[T1]{fontenc}
\usepackage{xcolor,listings}
\pagestyle{empty}
\begin{document}

\defverbatim[colored]\lst{%
\begin{lstlisting}[basicstyle=\small\ttfamily]
foo
bar
baz
\end{lstlisting}
}
\begin{frame}[fragile]
\begin{block}<1->{title}
\lst
\end{block}
\end{frame}

\end{document}


Herbert

Didier Verna

unread,
Apr 29, 2008, 1:00:47 PM4/29/08
to
Herbert Voss <herb...@googlemail.com> wrote:

> \documentclass{beamer}%
> \usepackage[T1]{fontenc}
> \usepackage{xcolor,listings}
> \pagestyle{empty}
> \begin{document}
>
> \defverbatim[colored]\lst{%
> \begin{lstlisting}[basicstyle=\small\ttfamily]
> foo
> bar
> baz
> \end{lstlisting}
> }
> \begin{frame}[fragile]
> \begin{block}<1->{title}
> \lst
> \end{block}
> \end{frame}
>
> \end{document}

Hmmm. Nice way of saying RTFBM ! :-)

Thanks !

--
5th European Lisp Workshop at ECOOP 2008, July 7: http://elw.bknr.net/2008/

EPITA / LRDE, 14-16 rue Voltaire Tel.+33 (0)1 44 08 01 85

94276 Le Kremlin-Bicętre, France Fax.+33 (0)1 53 14 59 22 did...@xemacs.org

Didier Verna

unread,
Apr 29, 2008, 1:27:23 PM4/29/08
to
I wrote:

> Hmmm. Nice way of saying RTFBM ! :-)

But in fact, that doesn't make it very easy to /rilly/ write inline
code in your document. In the meantime, I figured a way to do that by
hacking something on top of the verbatim package...

The idea is to output the environment's contents into a temporary file,
and use \lstinputlisting afterwards.


There you go !


\documentclass{beamer}

\usepackage{listings}

\usepackage{verbatim}
\makeatletter
\newwrite\lstvrb@out
\newenvironment{ccode}{%
\begingroup
\@bsphack
\immediate\openout\lstvrb@out\jobname.lst
\let\do\@makeother\dospecials\catcode`\^^M\active
\def\verbatim@processline{%
\immediate\write\lstvrb@out{\the\verbatim@line}}%
\verbatim@start}{%
\immediate\closeout\lstvrb@out
\@esphack
\endgroup
\begin{block}{C Code}
\lstinputlisting[language=C]{\jobname.lst}
\end{block}}
\makeatother

\begin{document}

\begin{frame}[fragile]
\begin{ccode}
int foo (int bar)
{
return 3;
}
\end{ccode}
\end{frame}

\end{document}

0 new messages