[Tex/LaTex] Set horizontal width on custom listings environment

listings

I want to create a custom listings environment and want to set its width as something like 90% of textwidth, centred.

Per an earlier question for algorithms, the first thing I tried was to embed the listing in a minipage environment. However, custom listing environments must be done through the \lstnewenvironment command. I found a way to still make standard environment work, but it requires to use the escape command every time before ending the environment.

The second thing I tried was this latter command, but as far as I can see, it doesn't offer a feature to set the width of the listing or centre it.

\documentclass{article}
\usepackage{listings}
\usepackage{lipsum}


\lstnewenvironment{queryl} %% APPROACH 1
 {\lstset{frame=shadowbox,escapechar=`}}
 {}

\newenvironment{query} %% APPROACH 2
 {\begin{minipage}{4cm}\centering\begin{queryl}}
 {\end{queryl}\end{minipage}}


\begin{document}
%% APPROACH 1
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis.
\begin{queryl}
begin
{ do nothing }
end ;
\end{queryl}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis.

%% APPROACH 2
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis.

\begin{query}
begin
{ do nothing }
end ;`%<- needs escape-to-LaTeX character to work
\end{query}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis.
\end{document}

enter image description here

One can note that for the first environment, the frame extends beyond the width of the text. Also, puzzlingly the minipage environment doesn't seem to want to centre and swallows space (though I guess I could perhaps at least get that to work with tinkering).

In any case, does anyone have any ideas on how to set a width for a custom listings environment and centre it without requiring use of the escape character each time to end the environment?

Best Answer

You can specify the width with \lstset{linewidth=<length>}.

To incorporate this into your custom environment:

\lstnewenvironment{queryl}
   {\lstset{frame=shadowbox,escapechar=`,linewidth=6cm}}
   {}

However, I would actually suggest you define the queryl environment to be able to accept an optional first parameter in case you need to adjust any parameters locally:

\lstnewenvironment{queryl}[1][] 
   {\lstset{frame=shadowbox,escapechar=`,linewidth=8cm, #1}}
   {}

Now when you use this as

\begin{queryl}
 ...
\end{queryl}

you get the first image below, but with

\begin{queryl}[linewidth=10cm]
 ...
\end{queryl}

you would obtain the second version:

enter image description here

Code:

\documentclass{article}
\usepackage{listings}

\lstnewenvironment{queryl}[1][] 
 {\lstset{frame=shadowbox,escapechar=`,linewidth=8cm, #1}}
 {}

\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis.
\begin{queryl}
begin
{ listing with default line width }
end ;
\end{queryl}

\begin{queryl}[linewidth=10cm]
begin
{ listing with width adjust locally }
end ;
\end{queryl}
\end{document}