LaTeX MXML Listings Preset


@englisch speaking visitors: skip the german parts and read just the code 🙂 the download link is at the bottom

Gestern war ich Stunden viel zu lange damit beschäftigt, zu versuchen meine MXML-Code-Beispiele, für meine Bachelor Arbeit, in mein LaTeX-Projekt einzubinden. Da ich auch einen gewissen ästhetischen Anspruch an meine Arbeit habe, sollte der Code auch mit Syntax-Highlighting dargestellt werden. Doch solche Aufgaben sind mit LaTeX irgendwie nicht wirklich einfach zu lösen … Aber ich habe es letztendlich doch (teilweise) geschafft 🙂
Da ich nichts dazu im Netz fand, will ich hier meine Lösung preisgeben.

Als erstes braucht man das listings-Packet und für Farben das color-Paket

\usepackage{listings}
\usepackage{color}

dann muss man ein paar Farben definieren

% Colors used by Flex Builder 3.
\definecolor{purple}{rgb}{0.65, 0.12, 0.82}
\definecolor{flexred}{rgb}{0.65, 0.01, 0.01}
\definecolor{flexgreen}{rgb}{0, 0.6, 0}
\definecolor{flexgray}{rgb}{0.25, 0.37, 0.75}
\definecolor{flexblue}{rgb}{0, 0.2, 1}
\definecolor{flexfunction}{rgb}{0.2, 0.6, 0.4}
\definecolor{flexvar}{rgb}{0.4, 0.6, 0.8}
\definecolor{black}{rgb}{0, 0, 0}

da das listings-Paket MXML nicht kennt, muss man eine neue Sprachdefinition anlegen

% Define new language for listings.
\lstdefinelanguage{MXML} {
   basicstyle=\ttfamily\scriptsize,
   sensitive=true,
   morecomment=[s][\color{flexred}\ttfamily]{<!--}{-->}, commentstyle=\color{flexred},
   morecomment=[s][\color{black}]{<?}{?>}, commentstyle=\color{black},
%  morecomment=[n][\color{black}]{\{}{\}}, commentstyle=\color{black},
   string=[b]", stringstyle=\color{flexred},
   morestring=[b]', stringstyle=\color{flexred},
   showstringspaces=false,
   numberstyle=\scriptsize,
   numberblanklines=true,
   showspaces=false,
   breaklines=true,
   showtabs=false,
   alsoletter={:},
   keywords = [1]
   {
      mx:WindowedApplication, mx:Application, mx:Button, mx:Text,
      mx:HBox, mx:VBox
   },
   keywordstyle={[1]\color{flexblue}},
%  otherkeywords={<,>,/>,</},
   keywords = [2]
   {
      mx:Script
   },
   keywordstyle={[2]\color{flexgreen}},
}

Da man in Verbindung mit MXML vielleicht auch ActionScript Code benutzt, braucht man auch dafür eine neue Sprachdefinition

% Define new language for listings.
\lstdefinelanguage{ActionScript} {
   basicstyle=\ttfamily\scriptsize,
   sensitive=true,
   %morecomment=[l][\color{flexgreen}\ttfamily]{//},
   %morecomment=[s][\color{flexgreen}\ttfamily]{/*}{*/},
   %morecomment=[s][\color{flexgray}\ttfamily]{/**}{*/},
   morestring=[b]",
   morestring=[d]/,
   morecomment=[l][\color{flexgreen}\ttfamily]{//},
   morecomment=[s][\color{flexgreen}\ttfamily]{/*}{*/},
   morecomment=[s][\color{flexgray}\ttfamily]{/**}{*/},
   morecomment=[n][\color{flexblue}\ttfamily]{<}{>},
   stringstyle=\color{flexred}\textbf,
   commentstyle=\color{flexgreen},
   showstringspaces=false,
   numberstyle=\scriptsize,
   numberblanklines=true,
   showspaces=false,
   breaklines=true,
   showtabs=false,
   emph =
   {[1]
      class, package, interface
   },
   emphstyle={[1]\color{purple}\textbf},
   emph =
   {[2]
      internal, public, protected, private,
      super, this, import, new, extends, implements,
      void, true, false, as
   },
   emphstyle={[2]\color{flexblue}\textbf},
   emph =
   {[3]
      function
   },
   emphstyle={[3]\color{flexfunction}\textbf},
   emph =
   {[4]
      var
   },
   emphstyle={[4]\color{flexvar}\textbf}
}

So, die Vorbereitungsarbeit wäre erledigt. Jetzt nur noch den Code folgender Maßen einbinden

\lstset{language=MXML}
\begin{lstlisting}
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	<mx:Button id="btn" x="30" y="25" width="100" label="Say hello!"
             click="{txt.text = 'Hello World!'}" />
	<mx:Text id="txt" x="30" y="55" width="100" text="" />
</mx:Application>
\end{lstlisting}

Das Ergebnis sieht dann so aus

MXML in LaTeX
MXML in LaTeX
Im Vergleich zu der Darstellung im FlexBuilder sind noch kleine Unterschiede zu erkennen, doch besser hab ich’s einfach nicht hinbekommen. Falls jemand Verbesserungsvorschläge hat, bitte ich um ein Kommentar 🙂

 

 

 

Zum Glück fand ich Clovis‘ Blog, der den ActionScript-Teil schon gemacht hatte – sonst würde ich mich wohl immer noch damit rumärgern 😉

English speakers: If you have suggestions to improve the MXML listings preset, feel free to comment (also if you’ve something else to say, comment! 😉 ). You can download everything in one file from the following link.

Download: ActionScript & MXML listings Preset

If you want to contribute, go to the priatepad at http://piratepad.net/latex-mxml-listings-preset
(after you’re done contributing, be so kind and drop a line here)


2 Antworten zu “LaTeX MXML Listings Preset”

  1. Schade, dass einige Links zu Bildern/Download noch auf das alte Blog zeigen…

    Ich habe eine minimale Erweiterung für den ActionScript drüben beim Original kommentiert:

    morestring=[b]“,
    morestring=[d]/,
    morecomment=[l][\color{flexgreen}\ttfamily]{//},
    morecomment=[s][\color{flexgreen}\ttfamily]{/*}{*/},
    morecomment=[s][\color{flexgray}\ttfamily]{/**}{*/},
    morecomment=[n][\color{flexblue}\ttfamily]{<}{>},

    Als Ersatz für die Zeilen 5-8 im ActionScript-Listing, dann klappts auch (fast, bis auf Text zwischen XML-Tags) mit XML in ActionScript und sogar (fast, bis auf die Flags am Ende) mit RegExpr in ActionScript.

    Gruß karfau

Kommentar verfassen