Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am trying to combine various packages and commands with PdfLaTeX (through MikTex on MS Windows), including newenvironment, quote, mcode and lstlisting, to create an environment to define multiple inlined text boxes that include commentary plus snippets of marked-up code. Simple experiments with quote and newenvironment work as shown in the documentation (e.g. Oetiker et al's not so short guide to latex), but addition of mcode/lstlisting disrupts compilation. The reported error message is that \end{document} is encountered prior to \end{quote}. I've tinkered with the order of the individual elements but to no avail.

The following does NOT work:

\documentclass[12pt]{article} 

\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage[pdftex]{hyperref}
\usepackage{amssymb}
\usepackage{geometry}
\usepackage{graphicx}
\usepackage[numbered,autolinebreaks,useliterate]{mcode}

\newenvironment{testcode}[2]%
{\small\begin{quote}title: #1
\begin{lstlisting}
#2\end{lstlisting}
}{\end{quote}}

\begin{document}

\begin{testcode}{some code}
{myvar= 1;}\end{testcode}

\end{document}

I left the list of all packages in the original LaTeX doc because I am unsure about package dependencies.

The following substitution of the later part of the code DOES compile and typesets very much as I would like, but then I cannot pass variables to newenvironment, removing some of the flexibility I was looking for:

\newenvironment{testcode}
{\small\begin{quote}}{\end{quote}}

\begin{document}

\begin{testcode}{
   title: some code
   \begin{lstlisting}
      myvar= 1;
   \end{lstlisting}}
\end{testcode}

\end{document}

I also attempted substituting newenvironment with lstnewenvironment, somewhat according to this solution, but did not get rid of the error. Appreciate some help with this.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

You have to use carefully LaTeX commands in the definition of listings environments. Here I show you how to define testcode with \lstnewenvironment and also a different way.

\documentclass[12pt]{article} 

\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{amssymb}
\usepackage{geometry}
\usepackage{graphicx}

% this is for “secondtest”
\usepackage{caption}
\captionsetup[lstlisting]{
  position=top,
  margin=3pc,
  justification=raggedright,
  singlelinecheck=false,
  font=small,
}
\usepackage[numbered,autolinebreaks,useliterate]{mcode}
\usepackage{hyperref}
\usepackage{lipsum} % Just for this example

\lstnewenvironment{testcode}[1]
  {\small\quote Title: #1}
  {\endquote}
\lstset{basicstyle=\ttfamily} % just to better see the listings

\lstnewenvironment{secondtest}[2][]
 {\small
  \lstset{
    aboveskip=\topsep,
    belowskip=\topsep,
    belowcaptionskip=\topsep,
    title=Title: #2,
    xleftmargin=3pc,
    #1}
 }
 {}

\begin{document}

\lipsum[2]

\begin{testcode}{some code}
myvar= 1;
\end{testcode}

\lipsum[2]

\begin{secondtest}{some code}
myvar= 1;
\end{secondtest}

\lipsum[2]

\end{document}

enter image description here

You can also add other listings options to secondtest in the following format:

\begin{secondtest}[<listings options>]{the title}
<the code>
\end{secondtest}
share|improve this answer
    
Thanks for clearing that up... the first answer is enough, but I'll look further into the alternative. –  Try Hard Oct 3 '13 at 19:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.