Date: Wed, 02 May 2007 10:32:38 +0200
From: Michael Wild <themiwi.REMOVE.THIS@student.ethz.ch>
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.2pre) Gecko/20070116 Thunderbird/2.0b2 Mnenhy/0.7.5.0
MIME-Version: 1.0
Newsgroups: comp.soft-sys.matlab
Subject: Re: Publish to latex with syntax highlighting
References: <ef558a9.-1@webcrossing.raydaftYaTP> <see-A91AED.17290301052007@news.frontiernet.net>
In-Reply-To: <see-A91AED.17290301052007@news.frontiernet.net>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: milliways.ifd.mavt.ethz.ch
Message-ID: <46384ca8@news1.ethz.ch>
X-Trace: news1.ethz.ch 1178094760 82.130.106.80 (2 May 2007 10:32:40 +0100)
Organization: Swiss Federal Institute of Technology (ETHZ)
Lines: 318
Path: news.mathworks.com!newsfeed-00.mathworks.com!oleane.net!oleane!news.in2p3.fr!in2p3.fr!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!news-zh.switch.ch!switch.ch!news1.ethz.ch!not-for-mail
Xref: news.mathworks.com comp.soft-sys.matlab:406378


Doug Schwarz wrote:
> In article <ef558a9.-1@webcrossing.raydaftYaTP>,
>  Kelly <kakearney@nospamgmail.com> wrote:
> 
>> When I publish a document to html, the blocks of code in the final
>> document use the colored syntax highlighting corresponding to the
>> Matlab editor, and are also surrounded by a light gray box. However,
>> when I publish to Latex, the code remains in black and is not offset
>> from the rest of the text, making it a little more difficult to
>> distinguish descriptive text from the code in the final document.
>>
>> Is there a way to add syntax highlighting to the final Latex
>> document? There is a small section in the mxdom2latex.xsl file that
>> seems to have this aim (lines 162 - 180). However, I don't knw much
>> about Latex coding beyond the very basics, so I may be
>> misunderstanding it.
>>
>> For the record, I'm running R2006b beta for Intel Macs on a MacOS X
>> system.
>>
>> Thanks,
>> Kelly
> 
> You might want to try the listings package.  Here's an example:
> 
> -------------------- matlab_listings.tex ----------------------------
> \documentclass{article}
> \usepackage[margin=0.75in]{geometry}
> \usepackage{courier}
> \usepackage{color}
> \usepackage{listings}
> 
> \definecolor{dkgreen}{rgb}{0,0.6,0}
> \definecolor{gray}{rgb}{0.5,0.5,0.5}
> 
> \begin{document}
> 
> \lstset{language=Matlab,
>    keywords={break,case,catch,continue,else,elseif,end,for,function,
>       global,if,otherwise,persistent,return,switch,try,while},
>    basicstyle=\ttfamily,
>    keywordstyle=\color{blue},
>    commentstyle=\color{red},
>    stringstyle=\color{dkgreen},
>    numbers=left,
>    numberstyle=\tiny\color{gray},
>    stepnumber=1,
>    numbersep=10pt,
>    backgroundcolor=\color{white},
>    tabsize=4,
>    showspaces=false,
>    showstringspaces=false}
> 
> \begin{lstlisting}
> function y = demo(x) % This is a comment.
>    str = 'hello there';
>    y = x + 1;
> end
> \end{lstlisting}
> 
> \end{document}
> -----------------------------------------------------------------------
> 

i created once a customized style sheet for my own use which made use of 
the listings package. however, it is nowhere near finished or perfect. 
however, editing is pretty straight forward if you have read the most 
basic tutorial on xsl. to use it just copy that stuff into some file 
ending on .xsl and use it for publishing. below goes the code (i hope it 
doesn't get truncated):

<?xml version="1.0" encoding="utf-8"?>

<!--
This is an XSL stylesheet which converts mscript XML files into XSLT.
Use the XSLT command to perform the conversion.

Ned Gulley and Matthew Simoneau, September 2003
Copyright 1984-2006 The MathWorks, Inc.
$Revision: 1.1.6.4 $  $Date: 2006/06/27 23:02:42 $

Adapted by Michael Wild for more latex-like output.
-->

<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#160;"> ]>
<xsl:stylesheet
   version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:escape="http://www.mathworks.com/namespace/latex/escape"
 
xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">
   <xsl:output method="text" indent="no"/>

<xsl:template match="mscript">
% This LaTeX was auto-generated from an M-file by MATLAB.
% To make changes, update the M-file and republish this document.

\documentclass[11pt]{article}
\usepackage{geometry}
\geometry{a4paper}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{epstopdf}
\DeclareGraphicsRule{.tif}{png}{.png}{`convert #1 `dirname #1`/`basename 
#1 .tif`.png}

\usepackage{hyperref}
\usepackage[usenames,dvipsnames]{color}
\definecolor{lightgray}{gray}{0.5}
\definecolor{darkgray}{gray}{0.3}

\usepackage{pdfcolmk}

\usepackage{listings}
\lstset{
   language=Matlab,
   basicstyle=\small\ttfamily,
   keywordstyle=,
   showstringspaces=false,
   commentstyle=\color{darkgray}}


     <!-- Determine if the there should be an introduction section. -->
     <xsl:variable name="hasIntro" select="count(cell[@style = 
'overview'])"/>
     <xsl:if test = "$hasIntro">
\title{<xsl:apply-templates select="cell[1]/steptitle"/>\\
{\large <xsl:apply-templates select="cell[1]/text"/>}}
</xsl:if>

\begin{document}
   \maketitle

     <xsl:variable name="body-cells" select="cell[not(@style = 
'overview')]"/>

     <!-- Include contents if there are titles for any subsections. -->
     <xsl:if test="count(cell/steptitle[not(@style = 'document')])">
   \tableofcontents
     </xsl:if>

     <!-- Loop over each cell -->
     <xsl:for-each select="$body-cells">
         <!-- Title of cell -->
         <xsl:if test="steptitle">
           <xsl:variable name="headinglevel">
             <xsl:choose>
               <xsl:when test="steptitle[@style = 
'document']">section</xsl:when>
               <xsl:otherwise>section</xsl:otherwise>
             </xsl:choose>
           </xsl:variable>

\<xsl:value-of select="$headinglevel"/>{<xsl:apply-templates 
select="steptitle"/>}

</xsl:if>

         <!-- Contents of each cell -->
         <xsl:apply-templates select="text"/>
         <xsl:apply-templates select="mcode"/>
         <xsl:apply-templates select="mcodeoutput"/>
         <xsl:apply-templates select="img"/>
% end of cell

     </xsl:for-each>


<xsl:if test="copyright">
\begin{par} \footnotesize \color{lightgray} \begin{flushright}
\emph{<xsl:apply-templates select="copyright"/>}
\end{flushright} \color{black} \normalsize \end{par}
</xsl:if>


\end{document}

</xsl:template>



<!-- HTML Tags in text sections -->
<xsl:template match="p">

<xsl:apply-templates/><xsl:text></xsl:text>
</xsl:template>

<xsl:template match="ul">\begin{itemize}
<xsl:apply-templates/>\end{itemize}
</xsl:template>
<xsl:template match="li">   \item <xsl:apply-templates/><xsl:text>
</xsl:text></xsl:template>
<xsl:template match="pre">
   <xsl:choose>
     <xsl:when test="@class='error'">
\begin{verbatim}<xsl:value-of select="."/>\end{verbatim}
     </xsl:when>
     <xsl:otherwise>
\begin{verbatim}<xsl:value-of select="."/>\end{verbatim}
     </xsl:otherwise>
   </xsl:choose>
</xsl:template>
<xsl:template match="b">\textbf{<xsl:apply-templates/>}</xsl:template>
<xsl:template match="tt">\texttt{<xsl:apply-templates/>}</xsl:template>
<xsl:template match="i">\textit{<xsl:apply-templates/>}</xsl:template>
<xsl:template match="a">\begin{verbatim}<xsl:value-of 
select="."/>\end{verbatim}</xsl:template>

<xsl:template match="text()">
   <!-- Escape special characters in text -->
   <xsl:call-template name="replace">
     <xsl:with-param name="string" select="."/>
   </xsl:call-template>
</xsl:template>

<xsl:template match="equation">
<xsl:value-of select="."/>
</xsl:template>


<!-- Code input and output -->

<xsl:template match="mcode">\begin{lstlisting}
<xsl:value-of select="."/>
\end{lstlisting}
</xsl:template>

<xsl:template match="mcodeoutput">
   {\color{lightgray} \small\begin{verbatim}<xsl:value-of 
select="."/>\end{verbatim} }
</xsl:template>


<!-- Figure and model snapshots -->

<xsl:template match="img">
\includegraphics [width=4in]{<xsl:value-of select="@src"/>}
</xsl:template>

<!-- Colors for syntax-highlighted input code -->

<xsl:template 
match="mwsh:code">\begin{verbatim}<xsl:apply-templates/>\end{verbatim}
</xsl:template>
<xsl:template match="mwsh:keywords">
   <span class="keyword"><xsl:value-of select="."/></span>
</xsl:template>
<xsl:template match="mwsh:strings">
   <span class="string"><xsl:value-of select="."/></span>
</xsl:template>
<xsl:template match="mwsh:comments">
   <span class="comment"><xsl:value-of select="."/></span>
</xsl:template>
<xsl:template match="mwsh:unterminated_strings">
   <span class="untermstring"><xsl:value-of select="."/></span>
</xsl:template>
<xsl:template match="mwsh:system_commands">
   <span class="syscmd"><xsl:value-of select="."/></span>
</xsl:template>


<!-- Used to escape special characters in the LaTeX output. -->

<escape:replacements>
   <!-- special TeX characters -->
   <replace><from>$</from><to>\$</to></replace>
   <replace><from>&amp;</from><to>\&amp;</to></replace>
   <replace><from>%</from><to>\%</to></replace>
   <replace><from>#</from><to>\#</to></replace>
   <replace><from>_</from><to>\_</to></replace>
   <replace><from>{</from><to>\{</to></replace>
   <replace><from>}</from><to>\}</to></replace>
   <!-- mainly in code -->
   <replace><from>~</from><to>\ensuremath{\tilde{\;}}</to></replace>
   <replace><from>^</from><to>\^{}</to></replace>
   <replace><from>\</from><to>\ensuremath{\backslash}</to></replace>
   <!-- mainly in math -->
   <replace><from>|</from><to>\ensuremath{|}</to></replace>
   <replace><from>&lt;</from><to>\ensuremath{&lt;}</to></replace>
   <replace><from>&gt;</from><to>\ensuremath{&gt;}</to></replace>
</escape:replacements>

<xsl:variable name="replacements" 
select="document('')/xsl:stylesheet/escape:replacements/replace"/>

<xsl:template name="replace">
   <xsl:param name="string"/>
   <xsl:param name="next" select="1"/>

   <xsl:variable name="count" select="count($replacements)"/>
   <xsl:variable name="first" select="$replacements[$next]"/>
   <xsl:choose>
     <xsl:when test="$next > $count">
       <xsl:value-of select="$string"/>
     </xsl:when>
     <xsl:when test="contains($string, $first/from)">
       <xsl:call-template name="replace">
         <xsl:with-param name="string"
                         select="substring-before($string, $first/from)"/>
         <xsl:with-param name="next" select="$next+1" />
       </xsl:call-template>
       <xsl:copy-of select="$first/to" />
       <xsl:call-template name="replace">
         <xsl:with-param name="string"
                         select="substring-after($string, $first/from)"/>
         <xsl:with-param name="next" select="$next"/>
       </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
       <xsl:call-template name="replace">
         <xsl:with-param name="string" select="$string"/>
         <xsl:with-param name="next" select="$next+1"/>
       </xsl:call-template>
     </xsl:otherwise>
   </xsl:choose>
</xsl:template>

</xsl:stylesheet>