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 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?

share|improve this question
1  
For your second approach, you can center the whole listings box by \newenvironment{query} %% APPROACH 2 {\begin{center}\begin{minipage}{4cm}\centering\begin{queryl}} {\end{queryl}\end{minipage}\end{center}} –  hpesoj626 Nov 21 '12 at 2:03
add comment

1 Answer 1

up vote 4 down vote accepted

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}
share|improve this answer
    
Completely missed that in the documentation. (Section 4.10 for posterity) texdoc.net/texmf-dist/doc/latex/listings/listings.pdf Thanks! –  badroit Nov 21 '12 at 2:02
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.