[Tex/LaTex] How to place a program and a text side by side

codeline-numberingpositioning

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?

Best Answer

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

\documentclass{article}
\usepackage[margin=25mm]{geometry}
\usepackage{algpseudocode}
\usepackage{multicol}
\begin{document}

\begin{multicols}{2}% 2-column layout
  \begin{minipage}{0.45\textwidth}
    \begin{algorithmic}[1]% Taken from the algorithmicx package documentation
      \Procedure{Euclid}{$a,b$}
      \State $r\gets a\bmod b$
      \While{$r\not=0$}
        \State $a\gets b$
        \State $b\gets r$
        \State $r\gets a\bmod b$
      \EndWhile\label{euclidendwhile}
      \State \textbf{return} $b$
      \EndProcedure
    \end{algorithmic}
  \end{minipage}
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.
\end{multicols}

\end{document}

produces

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.