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.