Discussion:
lstlisting environment inside another environment
(too old to reply)
Didier Verna
2008-04-29 16:03:39 UTC
Permalink
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, ***@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 ***@xemacs.org
Ulrike Fischer
2008-04-29 16:21:52 UTC
Permalink
Post by 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
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
2008-04-29 16:34:52 UTC
Permalink
Post by Ulrike Fischer
Post by 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
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
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/

Didier Verna, ***@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 ***@xemacs.org
Didier Verna
2008-04-29 16:38:44 UTC
Permalink
Post by Didier Verna
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
\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, ***@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 ***@xemacs.org
Joseph Wright
2008-04-29 16:50:11 UTC
Permalink
Post by Didier Verna
Post by Didier Verna
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
\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
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
2008-04-29 16:50:46 UTC
Permalink
Post by Didier Verna
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
2008-04-29 17:00:47 UTC
Permalink
Post by Herbert Voss
\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/

Didier Verna, ***@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 ***@xemacs.org
Didier Verna
2008-04-29 17:27:23 UTC
Permalink
Post by Didier Verna
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\***@out
\newenvironment{ccode}{%
\begingroup
\@bsphack
\immediate\openout\***@out\jobname.lst
\let\do\@makeother\dospecials\catcode`\^^M\active
\def\***@processline{%
\immediate\write\***@out{\the\***@line}}%
\***@start}{%
\immediate\closeout\***@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}
--
5th European Lisp Workshop at ECOOP 2008, July 7: http://elw.bknr.net/2008/

Didier Verna, ***@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 ***@xemacs.org
Loading...