My first question is how you write a program in LaTeX, with and without a line number for each line.

The second question is, because often my program is not too large, I want to save some space for text on its right hand side. So we could imagine two rectangle parts side by side: a program and some text. Could anyone tell me how to realize that?

Thank you very much!

Edit1: Now I have another need... I would like to have l1, l2, l3... to specify each line, instead of 1, 2, 3.... Does anyone know how to do that?

  • By "text" are you referring commenting your program (or code)? Or do you want a more formal/lengthy discussion in paragraph style? – Werner Aug 6 '11 at 0:29
  • Its not clear to me exactly what you want. Are you trying to program LaTeX, or show C code for example? Please post a complete MWE (including \documentclass) of what you have tried already to clarify things. – Peter Grill Aug 6 '11 at 0:59
  • @Werner, I do not want to comment my code, i just need a more formal/lengthy discussion in paragraph style... – SoftTimur Aug 6 '11 at 1:24
  • @Peter Grill, I am not trying to program LaTeX – SoftTimur Aug 6 '11 at 1:25

I assume that your program/code does not have to float around in your text, although it is also possible to construct something to that effect that floats in your document.

You can use the multicol package to generate a 2-column environment, placing your program/code on the left in a minipage environment. This sample code


\begin{multicols}{2}% 2-column layout
    \begin{algorithmic}[1]% Taken from the algorithmicx package documentation
      \State $r\gets a\bmod b$
        \State $a\gets b$
        \State $b\gets r$
        \State $r\gets a\bmod b$
      \State \textbf{return} $b$
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum venenatis facilisis arcu, a malesuada elit vulputate quis. Duis et interdum eros. Maecenas ac sem at elit rutrum vehicula eu et elit. In convallis justo in arcu condimentum a adipiscing tortor auctor. Nam adipiscing ante sed ante vehicula tincidunt. Morbi volutpat purus tortor, a venenatis turpis. Vestibulum tristique augue ut dui cursus a aliquam velit volutpat.



Program code on left|decription on right

Here you have to be careful that the description length does not exceed the program code. The program code above was produced using algpseudocode from the algorithmicx package. The look of the algorithm can be changed in many ways, as suggested in the package documentation. For instance, here is a different line numbering style (producing l1, l2,...,l9):

\algrenewcommand{\alglinenumber}[1]{\footnotesize\texttt{l}#1:}% l1, l2, ...

Algorithmicx showing a different line numbering style (<10 lines)

As you can see, this works well for pseudocode that has less than 10 lines. To accommodate for more lines, you could use:

\algrenewcommand{\alglinenumber}[1]{\footnotesize\texttt{l}\ifnum#1<10 \phantom{0}\fi #1:}% l 1, l 2, ... l 9, l10, l11, ...

Algorithmicx showing a different line numbering style (<99 lines)

Alternatively, placing the two respective components (program/code and description) in a minipage each would allow you to deviate from the length restriction, just like in @xport's answer. The listings package is used to import and format the code.

None of these approaches allow the program/code and description components to flow across pages, since the minipage environment does not break across pages.

| improve this answer | |
  • Thanks for your comment. Just another 1 question: what if i need to put l1: at the place of 1:, l2: at the place of 2:...? – SoftTimur Aug 7 '11 at 1:45

Let's assume your program is written in Visual Basic, called it VisualBasicCode.vb as follows.

' VisualBasicCode.vb
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
ZellBereichAdresse = CStr(Range(strZellber).Address)
End Function

The TeX input file:


    aboveskip=-\fboxsep% it is needed to remove vertical white space

    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.





enter image description here

| improve this answer | |
  • 1
    Please try style=B. – xport Aug 6 '11 at 2:58
  • Thanks very much for your comment, which is not same as @Werner's. I have 1 questions: Should I put VisualBasicCode.vb in the same folder as the tex file? – SoftTimur Aug 7 '11 at 1:44
  • visualbasiccode.vb should be placed in sub folder codes for better file management so you need to change \lstinputlistings[style=A]{visualbasiccode.vb} to \lstinputlistings[style=A]{codes/visualbasiccode.vb}. – xport Aug 7 '11 at 1:49
  • @SoftTimur: Not necessarily. If you put it in the same folder as the .tex, then @xport's code will work as-is. If you want the file to be stored somewhere else, you should reference it using a relative path. Something like \lstinputlisting[style=B]{code/vb/VisualBasicCode.vb}. – Werner Aug 7 '11 at 2:11

To answer your first question, you may want to use algorithm2e.sty, which is a floating algorithm environment with algorithmic keywords. This can be found on CTAN at the following link:


To number your algorithm use the \LinesNumbered command. Because it is a floating algorithm environment, it may be possible to wrap text around it like you do for figures. Not 100% sure on this as I have not tried it. This, however, may or may not answer your second question.

| improve this answer | |

An other possibilty might be to create a floating container for your code with the floatrow package and use the listings package to format your code See for example this. It should be possible to make this float behave like a wrapfigure, and thus be displayed next to your text.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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