GeneralMaking a new environment based on the listing environment

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
csmgroup
Posts: 25
Joined: Sat Jul 17, 2010 6:58 pm

Making a new environment based on the listing environment

Post by csmgroup »

I need to make a new environment based on the listing environment.

I'd like to have this code.

Code: Select all

\definecolor{darkgray}{rgb}{0.97,0.97,0.97}
\lstset{backgroundcolor=\color{darkgray}, 
           columns=fullflexible, 
           basicstyle=\ttfamily\footnotesize, 
           numbers=left, 
           numberstyle=\tiny, 
           stepnumber=2, 
           numbersep=5pt}
\begin{lstlisting}[frame=tb]
var capital = Map("US" -> "Washington", "France" -> "Paris") 
capital += ("Japan" -> "Tokyo")
\end{lstlisting}
as this

Code: Select all

\begin{mycode}
var capital = Map("US" -> "Washington", "France" -> "Paris") 
capital += ("Japan" -> "Tokyo")
\end{mycode}
I came up with the newenvironment as follows, but it doesn't work.

Code: Select all

\documentclass{article}

\newenvironment{mycode}%
{
\lstset{columns=fullflexible, 
           basicstyle=\ttfamily\footnotesize, 
           numbers=left, 
           numberstyle=\tiny, 
           stepnumber=2, 
           numbersep=5pt}
\begin{lstlisting}[frame=tb]
}
{\end{lstlisting}}

\begin{document}
\begin{mycode}
Hello, world.
\end{mycode}
\end{document}

This is the error code.

Code: Select all

ERROR: Undefined control sequence.

--- TeX said ---
\mycode -> \lstset 
                   {columns=fullflexible, basicstyle=\ttfamily \footnotesize...
l.16 \begin{mycode}
What's wrong with this?
Last edited by csmgroup on Sun Aug 01, 2010 8:31 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Making a new environment based on the listing environment

Post by Juanjo »

In your minimal example you forgot to load listings. Anyway, verbatim-like environments cannot be defined in the usual way. Check subsection 4.16 in the manual of the listings package.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
csmgroup
Posts: 25
Joined: Sat Jul 17, 2010 6:58 pm

Re: Making a new environment based on the listing environmen

Post by csmgroup »

Thanks for the answer.
Post Reply