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
Subject: Re: Publish to latex with syntax highlighting
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:
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.
-->
<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.
<!-- 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>
<!-- 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>
<!-- 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
Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for
all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content.
Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available
via MATLAB Central. Read the complete Disclaimer prior to use.