Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a lot of VBA (Visual Basic for Applications, the programming language in Microsoft Office) code which I have to add to my thesis in the appendix.

I wonder how I can most comfortably list those lines of code (which have to be structured in a certain way, as there are many, many lines) include those in my pdflatex document.

Has anybody here experience with typesetting large amounts of VBA code?


Option Explicit

Sub Signal(strSignalfolge As String, Optional lngTakt As Long = 100)
'Prozedur erzeugt eine Serie von Warntönen.
'Die optionale Variable lngTakt gibt den Takt in Millisekunden vor (Standard: 100 ms)
'Das Muster kann über die String-Variable strSignalfolge beeinflusst werden:
' Stern (*)     -> 1 Warnton
' Ziffern 1..9  -> 1..9 Takte Pause
' Leerzeichen   -> 1 Sekunde Pause
' Minus (-)     -> 1.5 Sekunden Pause
    Dim i As Integer
    Dim b As String
    For i = 1 To Len(strSignalfolge)
        b = Mid(strSignalfolge, i, 1)
        Select Case b
            Case "*": beep
            Case 1 To 9: DELAY CInt(b) * lngTakt
            Case " ": DELAY 1000
            Case "-": DELAY 1500
        End Select
        DELAY lngTakt
    Next i

End Sub

Public Function strParse(Data As String, Trenn As String, Nr As Integer)
'Funktion trennt die Zeichenkette <Data>
    On Error Resume Next
    Dim MainData() As String, SplitData() As String
    MainData = Split(Data, Trenn)
    SplitData = Split(MainData(Nr - 1), Trenn)
    strParse = SplitData(0)
End Function


Public Sub ProtokollZeile(strData As String)
'Gibt eine Protokollzeile im Direktfenster aus: Datum, Uhrzeit und <strData>
    Debug.Print Now & " " & strData
End Sub

Sub NetSend(strmsg As String, Optional strEmpf As String = "imf3beck")
'Prozedur sendet die Kurznachricht <strMsg> über das Intranet an den Rechner <strEmpf>
    Dim a
    a = Shell("cmd.exe /c net send " & strEmpf & " " & strmsg, vbMinimizedFocus)
    MsgBox "(net send message)" & vbCr & strmsg
End Sub

Sub NetSendMessungBeendet(Optional strBem As String = "")
'Sendet über das Netzwerk eine Statusnachricht, dass die Messung beendet wurde und gibt diese
'Nachricht auch lokal als Warndialog aus
Dim strMsgText As String
strMsgText = (Format(Now, "hh:mm:ss") & " Messung beendet" & strBem)
NetSend (strMsgText)
End Sub

Public Function ZellBereichAdresse(strZellber As String) As String
Application.Volatile
ZellBereichAdresse = CStr(Range(strZellber).Address)
End Function
share|improve this question

2 Answers 2

up vote 14 down vote accepted

listings packages will help you.

Using style=A

enter image description here

Using style=B

enter image description here

TeX Input File

\documentclass[dvipsnames,cmyk]{article}
\usepackage{listings,xcolor}

\lstset
{
    breaklines=true,
    tabsize=3,
    showstringspaces=false
}


\lstdefinestyle{Common}
{
    extendedchars=\true,
    language={[Visual]Basic},
    frame=single,
    %===========================================================
    framesep=3pt,%expand outward.
    framerule=0.4pt,%expand outward.
    xleftmargin=3.4pt,%make the frame fits in the text area. 
    xrightmargin=3.4pt,%make the frame fits in the text area.
    %=========================================================== 
    rulecolor=\color{Red}
}

\lstdefinestyle{A}
{
    style=Common,
    backgroundcolor=\color{Yellow!10},
    basicstyle=\scriptsize\color{Black}\ttfamily,
    keywordstyle=\color{Orange},
    identifierstyle=\color{Cyan},
    stringstyle=\color{Red},
    commentstyle=\color{Green}
}

\lstdefinestyle{B}
{
    style=Common,
    backgroundcolor=\color{Black},
    basicstyle=\scriptsize\color{White}\ttfamily,
    keywordstyle=\color{Orange},
    identifierstyle=\color{Cyan},
    stringstyle=\color{Red},
    commentstyle=\color{Green}
}

\begin{document}
\begin{lstlisting}[style=A]%please try style=B
Option Explicit

Sub Signal(strSignalfolge As String, Optional lngTakt As Long = 100)
'Prozedur erzeugt eine Serie von Warntönen.
'Die optionale Variable lngTakt gibt den Takt in Millisekunden vor (Standard: 100 ms)
'Das Muster kann über die String-Variable strSignalfolge beeinflusst werden:
' Stern (*)     -> 1 Warnton
' Ziffern 1..9  -> 1..9 Takte Pause
' Leerzeichen   -> 1 Sekunde Pause
' Minus (-)     -> 1.5 Sekunden Pause
    Dim i As Integer
    Dim b As String
    For i = 1 To Len(strSignalfolge)
        b = Mid(strSignalfolge, i, 1)
        Select Case b
            Case "*": beep
            Case 1 To 9: DELAY CInt(b) * lngTakt
            Case " ": DELAY 1000
            Case "-": DELAY 1500
        End Select
        DELAY lngTakt
    Next i

End Sub

Public Function strParse(Data As String, Trenn As String, Nr As Integer)
'Funktion trennt die Zeichenkette <Data>
    On Error Resume Next
    Dim MainData() As String, SplitData() As String
    MainData = Split(Data, Trenn)
    SplitData = Split(MainData(Nr - 1), Trenn)
    strParse = SplitData(0)
End Function


Public Sub ProtokollZeile(strData As String)
'Gibt eine Protokollzeile im Direktfenster aus: Datum, Uhrzeit und <strData>
    Debug.Print Now & " " & strData
End Sub

Sub NetSend(strmsg As String, Optional strEmpf As String = "imf3beck")
'Prozedur sendet die Kurznachricht <strMsg> über das Intranet an den Rechner <strEmpf>
    Dim a
    a = Shell("cmd.exe /c net send " & strEmpf & " " & strmsg, vbMinimizedFocus)
    MsgBox "(net send message)" & vbCr & strmsg
End Sub

Sub NetSendMessungBeendet(Optional strBem As String = "")
'Sendet über das Netzwerk eine Statusnachricht, dass die Messung beendet wurde und gibt diese
'Nachricht auch lokal als Warndialog aus
Dim strMsgText As String
strMsgText = (Format(Now, "hh:mm:ss") & " Messung beendet" & strBem)
NetSend (strMsgText)
End Sub

Public Function ZellBereichAdresse(strZellber As String) As String
Application.Volatile
ZellBereichAdresse = CStr(Range(strZellber).Address)
End Function
\end{lstlisting}
\end{document}

Notes:

  1. It is better to import the VBA source code from within your TeX input file using \lstinputlisting[<options>]{filename.vb}. This way will make your TeX input file clean.
  2. Try my style B by changing style=A to style=B.
share|improve this answer
    
Thank you very much - this is pretty close to what I need. The only problem: I want to print my thesis in black & white, so do you have a suggestion for a black&white-style? Maybe different font styles like sans serif or roman could be used to distinguish between the different types of keywords, but as I am not a programmer, I'm not sure, if there might be conventions how to format code to make it better readable. –  Martin Jul 12 '11 at 18:48
    
p. s. Thanks for your notes. Concerning note 1: this is a good advice which I surely will follow - especially as I have many VBA modules to typeset. –  Martin Jul 12 '11 at 18:51
    
p. s. a minor correction: is it possible, that there is a typo and that it has to be extendedchars=true without the backslash ?! –  Martin Jul 12 '11 at 20:37
    
@Martin: There was a historical reason I used \true instead of true. –  xport Jul 13 '11 at 7:08
    
does it work with "\true"? I had problems with special characters like äöüß and changing the definition to true seemed to solve this issue. –  Martin Jul 13 '11 at 7:51

Your options include:

I would go with minted. Mind that it uses pygments so its dependencies include python + pygments. You must also call your TeX engine with -shell-escape option. E.g.

xelatex -shell-escape source_file.tex

Edit

First of all I am a Python guy! :P

More seriously now, I prefer minted because I find it easier to set up. You don't have to spend time with listings' options in order to get colored output. Usually the defaults for each language are fine. Also you have great flexibility if you want to typeset an obscure language. Pygmentize has lexers for almost everything.

I must admit that I haven't used it for lots and lots of code snippets, so I don't know if the compile time gets unacceptable.

I think that the best choice would be to try them both and choose whichever suits you better!

Here is a MWE

\documentclass[12pt, final]{article}

\usepackage{minted}

\begin{document}

\begin{minted}{python}
class FancyColoredStuff(object):
    def __init__(self, language):
        self.language = language

    def highlight(self):
        print "This is colorful code in %s" % self.language
\end{minted}

\end{document}
share|improve this answer
1  
Note that there is indeed a listing and a listings package. –  Martin Scharrer Jul 12 '11 at 16:18
    
Thanx for the correction. It was a typo. I meant listings –  pmav99 Jul 12 '11 at 18:03
    
@pmav99: thanks for mentioning the alternatives! Can you explain why you would prefer minted to listings? –  Martin Jul 12 '11 at 18:49
    
@Martin, I updated my answer –  pmav99 Jul 12 '11 at 19:19
    
@pmav99: thanks for your explanation. I'll try it out, however I had already put some effort in trying out listings, so I think I'll try with listings. –  Martin Jul 12 '11 at 19:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.