Here's the PROBLEM:
You want a nice looking "Code List" numbered environment, with numbering of the chapter and then the code list, a bit like theorem numbering in AMSLaTeX. You look up CTAN and find a number of nice fancy verbatim style environments, like morevrb, alltt, fancyvrb, and listings. It looks like the listings package is really cool. It is language-aware, so you can have it format C/C++, Perl, Fortran or whatever, using the language keywords and comment syntax to apply special styles, just like your favourite programmers editor would.
The problem is that the caption and numbering used by the listings package is too much like Table numbering. You really want it to be more distinctive. What to do? You could hack a solution perhaps.
SOLUTION 1:
Since I was writing a manual that uses C++, Perl and bash scripts I initially tried the listings package as follows:
Code: Select all
%%
%% My Code List environment (for listing verbatim C++ code)
%%
\usepackage{listings}
\lstloadlanguages{[GNU]C++,Perl,sh}
\lstnewenvironment{listing}[2][]{ \vspace{1ex}\noindent
\lstset{ basicstyle=\ttfamily,
frame=tb,
columns=fullflexible,
framexrightmargin=-.2\textwidth,
breaklines=true,
prebreak=/,
caption=#1,
label=#2
} }{ }
The problems I had with this solution was that (a) I decided I did not much like the syntax highlighting after all (no big problem really, I could just disable it as above with my "basicstyle=\ttfamily," option), and (b) more seriously, I just did not like the look of the captioning. What I really wanted was a distinct looking caption, more like a theorem or definition environment.
SOLUTION 2.:
I decided to just use the AMSLaTeX theorem environments. I defined my custom Code List environment in the peamble just after my theorem, conjecture and definition definitions as follows,
Code: Select all
%
% Theorem-like environments for AMS style packages
%
\newtheorem{theorem}{Theorem}
\newtheorem{conjecture}{Conjecture}
\theoremstyle{definition}
\newtheorem{definition}{Definition}[chapter]
\newtheorem{codelist}{Code Listing}[chapter]
Code: Select all
\begin{codelisting}
\label{c_hellobash}
\begin{verbatim}
#!/bin/bash
# Simple example bash script
echo "Hello $@"
exit
\end{verbatim}
\end{codelisting}
SOLUTION 3:
(The nice thing about LaTeX is that sometimes TIMTOWODI

What I decided upon was to combine the above use of the AMSLaTeX definition environment with the listings package as my auxiliary verbatim style portion. (Did you know that listings, and I think also alltt and fancyvrb allow word wrapping?) So I used the above "\newtheorem{codelist}{Code Listing}[chapter]" command in the preamble, as well as the above "\lstnewenvironment{listing}[2][]{ \vspace{1ex}\noindent ...." stuff, only to retain the AMSLaTeX captioning style I just deleted the use of the optional arguments for the ""\lstnewenvironment{listing}", thus,
Code: Select all
\newcommand{\singledollar}{$}
\theoremstyle{definition}
\newtheorem{codelist}{Code Listing}[chapter]
\usepackage{listings}
\lstloadlanguages{[GNU]C++,Perl,sh}
\lstnewenvironment{listing}[][]{ \vspace{1ex}\noindent
\lstset{ basicstyle=\ttfamily,
frame=tb,
columns=fullflexible,
framexrightmargin=-.2\textwidth,
breaklines=true,
prebreak=/ } }{ }
Ideally I'd like to play around a bit more with the language-aware syntax highlighting inside the listings environment. I could use coloring for example. "breaklines=true," and "prebreak=/ " by the way, specify wrapping of long lines and insertion of the "/" character at the end of the broken line, a bit like some automatic code documentation parsers and textbook or code manual typesetters.
Here's the final usage (see Code List~\ref{c_runmyapp}:
Code: Select all
\begin{codelist}[A script for starting an application that uses obscure shared libraries]
\label{c_runmyapp}
\begin{listing}
#!/bin/bash
#
# Bash script to run "myapp" safely with cmd line argument
#
export LD_LIBRARY_PATH=$HOME/lib/myapp
. $HOME/bin/myapp $@
exit
\end{listing}\phantom{$\singledollar} % This just resets my Kile or Emacs editor syntax highlighting
\end{codelist}
Well, I use Kile, which gives me LaTeX/TeX aware syntax highlighting. But the odd number of "$" characters in my non-verbatim "listing" environment stuffs up the syntax highlighting, since Kile is not that smart that it knows about the rules for this obscure listings package, i.e., Kile (and probably your emacs, or vi,or Kate, or Alpha, or other LaTeX editor) will get tripped up because it doesn't know that a \begin{listing}...\end{listing} environment should have verbatim style highlighting. I guess if I knew how to configure Kile correctly i could fix this by making Kile aware of how to highlight the new "listing" environment. But I haven't looked into that yet, so the \phantom" command is just a kludge to get an extra invisible "$" character so that at least outside the "listing" environment I recover nice Kile editor highlighting for my sanity. If I can't configure Kile I know I can configure emacs, it's just a pain to read up on how to do it.
That's all for today. If I can figure out how to tailor Kile syntax highlighting I'll post it to the forum's "LaTeX Editors->Kile" board, unless someone beats me to it (please do!).