Change \begin{macrocode} to \begin{lstlistings}

Change \begin{macrocode} to \begin{lstlistings}

Post by Christophe » Tue, 26 Apr 2005 16:36:11


Hello,

I'm trying to change the default behaviour of the doc package to use
listings pretty printing features, that is

% \begin{macrocode}
\def\toto{toto}
% \end{macrocode}

would produce the code as usual but in the documentation the code would
be pretty-printed as with the listings package.

In doc.sty, I understand that one of the key macros is

|gdef|xmacro@code#1% \end{macrocode}[#1|end[macrocode]]

So the fellowing code won't work

\let\macrocode\lstlisting
\let\endmacrocode\endlstlisting

since the lstlisting environment will ignore the "% \end{macrocode}"
convention and will display the "% ".

I've been through the listings code and it is really too difficult to
understand for me (the \lstnewenvironment seems to construct the
listings environment and takes care of closing verbatim parts, but it
seems uneasy to modify it in order to take car of "%
\end{macrocode}").

Would somebody here have a "quick and dirt" solution to my problem (I do
*not* want the commands inside macrocode to be indexed).

Thanks a lot
 
 
 

Change \begin{macrocode} to \begin{lstlistings}

Post by brotherel » Wed, 27 Apr 2005 02:44:59

"Christophe Jorssen" < XXXX@XXXXX.COM > writes:


If you don't strictly require this to run within (La)TeX, the
following awk script will do a first approximation of making all the
things in comments text and all the other ones lstlistings:

(not tested; some strings might need to be adapted -- I wrote this to
handle Emacs LISP. You'll need an appropriate listings setup and
\renewenvironment{macrocode}{}{}, the call is gawk -f littex.awk foo.dtx,
if you save the script to littex.awk.)

#!/usr/bin/gawk
BEGIN {
LIT_RE="^%[[:space:]]*";
FALSE=0;
TRUE=1;

in_lit_comment=TRUE;
}

## convert tabs to spaces
1 {
gsub("\t", " ");
}


($0 ~ LIT_RE) && (!in_lit_comment){
print endCODE();
in_lit_comment=TRUE;
}

($0 !~ LIT_RE) && (in_lit_comment){
print beginCODE();
in_lit_comment=FALSE;
}

($0 ~ LIT_RE) && (in_lit_comment){
print fmtDOC();
}

($0 !~ LIT_RE) && (!in_lit_comment){
print fmtCODE();
}

END {
if (!in_lit_comment){
print endCODE();
}
}

function endCODE(){
return "\\end{lstlisting}";
}

function beginCODE(){
return "\\begin{lstlisting}{name=" FILENAME "}";
}


function fmtDOC(){
return gensub(LIT_RE, " ", 1);
}

function fmtCODE(){
return $0;
}

HTH
Ulrich
--
€x or \forall x\in \mathbb{N}?
http://www.yqcomputer.com/ (Current release: 0.5.0 20050306)

 
 
 

Change \begin{macrocode} to \begin{lstlistings}

Post by Christophe » Wed, 27 Apr 2005 18:18:53


Thank you Ulrich. I know nothing about awk (I assume it runs only under
*nix) but I'm going to look what I can find about it on the web.

By the way, the original code of macrocode is really tricky as the
lstlisting one... It seems it is never easy to have verbatim text!

Best regards
 
 
 

Change \begin{macrocode} to \begin{lstlistings}

Post by Morten Hho » Wed, 27 Apr 2005 18:21:17

On Tue, 26 Apr 2005 11:18:53 +0200, Christophe Jorssen



On the contrary. It's quite easy to get verbatim text - it's stopping it
that's hard ;-)
--
Morten Hholm
My armpits may be smelly but my is address not.
UK-TUG FAQ: <URL: http://www.yqcomputer.com/ ;