wiki:Latex/Examples/Listings

Listings

Listings can be well typeset with the listings package. However, it requires some setup before listings look very well. See the example below. These examples use the lstcustom.sty language and style definitions. Just copy that style file into the same directory as your latex file.

The result is given here: lstcustom.pdf Download

\documentclass{scrartcl}
\usepackage{url}

\usepackage{listings}
\usepackage{lstcustom}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
 
% beramono or luximono give very nice ttfamily fonts
\usepackage[scaled=0.8]{beramono}
 
\begin{document}  

\renewcommand{\lstfontfamily}{\ttfamily}
%\renewcommand{\lstfontfamily}{\sffamily}
         
\section{Listings in Latex}
           
Here is some normal text for comparison. The looks of your listings are most
influenced by the type of font you use, i.e. teletype (\verb|\ttfamily|) or 
serife free (\verb|\sffamily|). Still within one of those two classed there are
very big differences according to the concrete fonts you use for the two
classes. The default font for tt have quite classic looks. Hence I
actually prefer tt fonts with a nicer font, e.g. \emph{beramono},
\emph{luximono} or \emph{palatino}. See \url{http://www.tug.dk/FontCatalogue/}
on how to use other fonts.
 
\subsection{Black \& White Listings}
 
\begin{lstlisting}[language=pseudo, style=black]
  algorithm calcMyNumbers(n)
        foreach number till n do
                calc something important
                if result is good then be happy else continue
\end{lstlisting} 
      
\subsection{Eclipse like Listings}

\begin{lstlisting}[language=c, style=eclipse]
int main(int argc, char* argv){
        // comment 
        int a = 5 + 3;
        for(;a<100;a++){
                a = a-- /* another comment */    
        } 
}        
\end{lstlisting}                              
   
\begin{lstlisting}[language=java, style=eclipse]  
public class MyClass{    
        int    myVar1     = 27; 
        float  thisIsCool = 3.234f; 
        String myString   = "Hello World!";           
          
        /** This is a Javadoc Comment */   
        /* This is a normal comment */  
        public MyClass(String param){
                myString = param; // comment  
        }
}        
\end{lstlisting}
  
\begin{lstlisting}[language=oaw, style=eclipse]
import meta1 
import meta2  
extension test::myextension  
        
/* comment */
create meta2::Type1 this transform(meta1::Type2 in):
        let a = 5: // comment
        this.setName(in.name + a) 
;
\end{lstlisting} 

\begin{lstlisting}[language=xml, style=eclipse]
<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0"
    xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="synccharts"
    nsURI="http://kieler.cs.cau.de/synccharts/0.2" nsPrefix="synccharts">
  <eClassifiers xsi:type="ecore:EClass" name="Action">
    <eStructuralFeatures xsi:type="ecore:EReference" name="effects" upperBound="-1"
        eType="#//Effect" containment="true"/>
    <eStructuralFeatures xsi:type="ecore:EReference" name="trigger" eType="#//Expression"
        containment="true"/>
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="delay" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"
        defaultValueLiteral="1"/>
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="isImmediate" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EBoolean"/>
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="label" eType="#//Parsable"/>
  </eClassifiers>
</ecore> 
\end{lstlisting} 

Eclipse Xpand uses french guillemots ('«', '»'). The listings
package unfortunately does not support multibyte characters of UTF-8.
So you have to (1) encode the whole tex file in latin1 (inputenc) or
(2) write the code in separate files (lstinputlisting) which have latin1
encoding while your tex file may have UTF encoding or (3)
use an escape character in the listing to escape special symbols or 
(4) use \emph{literate} in language definition to map the symbols to 
other ones (this is used here). Either way I did not get guillemots
to be recognized in any of the enviornments (keywords, delimiters, \ldots). 

(3) is shown here:
\begin{lstlisting}[escapechar={@}]
Hall@ö@ @ä@ @ü@ @ß@!
\end{lstlisting} 

(4) is shown here, the literate is in the language definition of oaw.  
\begin{lstlisting}[language=oaw, style=eclipse, caption={Listing in tex File}] 
/* Copyright by me   
 */  
«IMPORT 'http://www.mydomain.com/myNamespaceURI'»
«EXTENSION xpt::diagram::ViewmapAttributesUtils»
   
«DEFINE createNodeShape(node : gmfgen::GenNode) FOR gmfgen::FigureViewmap-»
  «LET (if figureQualifiedClassName = null 
     then 'org.eclipse.draw2d.RectangleFigure' 
     else figureQualifiedClassName
     endif)  
     AS fqn-»  
     «EXPAND xpt::Common::generatedMemberComment»
     «REM Another Comment»
     «REM» Comment «ENDREM»
    protected java.lang.String createSomething() {
     
    }    
  «ENDLET-»
«ENDDEFINE»   
\end{lstlisting}    
         
\begin{lstinputlisting}[language=oaw, style=eclipse, caption={Imported Listing
from external file}, inputencoding=latin1]{lstxpand.xpt}
\end{lstinputlisting}

\end{document}      

Attachments