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 defined my own language whose syntax is very close to the one of Pascal. I would like to type the following program for instance. Many people use the packages algorithmicx or algorithm2e to print programs, but it seems that we have to use their specific keywords of its syntax like procedure, end while.

I just want to indent spaces when needed, and bold some words when I want (sorry that I could not bold letters in the following figure). Do I have to use texttt for each line? Also, with verbatim, I could not bold words...

Also, as the program is not large, I would need to put it on the left side of the page, and leave space for some explanasion text on its right hand side. So I guss I need to get them all together, and use multicols.

Could anyone help?

program test                                      some text here
var
  i : integer;                                    ... 
  b : boolean;
begin
   i := 1;                                        ...
   b := true;
   while i < 15 do
      i := i  + 1;
      b := not b                                  some text here
   od
end

Edit1: Following @Werner's tabbing solution, I have made the code a minipage:

\begin{minipage}[t]{0.48\linewidth}%
\begin{tabbing}
Werner's solution
\end{tabbing}
\end{minipage}\hfill
\begin{minipage}[t]{0.48\linewidth}
a paragraph
\end{minipage}

Actually as the code shows, I want the texts to be another separated minipage on the right hand side of the code. I have then 2 questions:

  1. is it reasonable to make 0.48\linewidth instead of 0.5\linewidth? 0.48\linewidth is a conventional choice?

  2. is it possible to make a frame around the code, and use \caption and \label to make it a reference (I prefer figures)?

share|improve this question
    
1) The choice of 0.48\linewidth is to allow for some padding between the two minipages. \hfill completes the padding so as to use the entire \linewidth. 2) See my solution for framing the code - just put an \fbox{...} around it. You can put the entire thing in a \begin{figure}...\end{figure} and use \caption and \label just like you normally would for a "regular" figure. –  Werner Aug 11 '11 at 7:27
    
I am sure you have seen this, but I'll add a pointer to a very similar questions, for other people to see: Looking for style files for writing a programming language book –  Lover of Structure Feb 6 '13 at 14:41
add comment

2 Answers 2

up vote 24 down vote accepted

algorithmicx

You can define your own keywords when using the algorithmicx package. Section 3.1.10 Changing command names of the package documentation discusses the use of \algnewcommand/\algrenewcommand that you can use to create a new command/change existing formatting. The following two images are taken directly from the package documentation:

Algorithmicx \algnewcommand

Algorithmicx \algrenewcommand


algorithm2e

You can also define your own keywords using the algorithm2e package. Section 10 To define your own language keywords of the package documentation discusses this in detail. Specifically, you use commands like

  • \SetKwInput and \SetKwOutput for algorithm input / output definition
  • \SetKw for keyword definitions
  • \SetKwBlock for block definitions
  • \SetKwFunction for function / procedure definitions
  • \SetKwComment for comment definitions
  • \SetKwIf for if-statement definitions
  • \SetKwSwitch for multiple condition selection definitions
  • \SetKwFor for for loop definitions
  • \SetKwRepeat for repeat / while definitions

listings

Then there's the listings package that allows just as much modification. Keyword definitions are specified via styles and/or languages. Read the highly detailed package documentation for more information on this.


tabbing

The above use some rich programming directives from package that probably require a lot of reading. In the special case where you "just want to write a couple of lines of code and format stuff your own way", you could use the tabbing environment. This is either provided by LaTeX (as default) or by the tabbing package. The tabbing package documentation gives some motivation for using it, but that's up to you.

A typical tabbing environment would resemble

\begin{tabbing}
  <space> \= <space> \= <space> \= <space> \= \kill
  no indent \> \> more text \\
  \> one indent \> \\
  ....
  ....
\end{tabbing}

where the tab stops a indicated by \= and jumped to by using \>. It is best to put empty spacing in the first row (via \hspace{<space>}, \quad, \qquad or \phantom{<stuff>}), since a first-line \kill is provided (as opposed to \\[-\baselineskip]) to prevent the first line from being typeset. This ensures that the actual tabbing typesetting starts in the first row.

The advantage here is that you can format your code the way you want, without any special package "interrupting" your style or other preferences. Given your example, here is what I would do (without using \multicol):

\documentclass{article}
\usepackage{calc}% For length calculations
\begin{document}

\newlength{\mylen}%
\setlength{\mylen}{\linewidth-2\fboxsep-2\fboxrule}%

\fbox{\begin{minipage}{\mylen}
  \begin{tabbing}
    \quad \= \quad \= \quad \= \hspace{5cm} \= \kill
    \textbf{program} test \> \> \> \> \texttt{some text here} \\
    \textbf{var} \\
    \> $i$ : \textbf{integer}; \> \> \> \texttt{...} \\
    \> $b$ : \textbf{boolean}; \\
    \textbf{begin} \\
    \> $i:=1$; \> \> \> \texttt{...} \\
    \> $b:=\texttt{true}$; \\
    \> \textbf{while} $i<15$ \textbf{do} \\
    \> \> $i:=i+1$; \\
    \> \> $b:=\textbf{not}\ b$; \> \> \texttt{some text here} \\
    \> \textbf{od} \\
    \textbf{end}
  \end{tabbing}
\end{minipage}}

\end{document}

LaTeX tabbing example


verbatim

The verbatim environment is a paragraph-making environment that gets LaTeX to print exactly what you type in. It turns LaTeX into a typewriter with carriage returns and blanks having the same effect that they would on a typewriter. The output looks exactly as it looks in the input file. Typical typesetting in verbatim would resemble

\begin{verbatim}
  <stuff> 
\end{verbatim}

or

\begin{verbatim*}
  <stuff>
\end{verbatim*}

where the difference between verbatim and verbatim* is that the latter prints spaces as "visual" spaces, i.e., a short, squat "u". Inline verbatim is also possible using

\verb char literal_text char

or

\verb*char literal_text char

with a similar meaning as before for the unstarred and starred versions. There may be no space between \verb or \verb* and char (space is shown here only for clarity). Here char denotes the \verb delimiters and should match at the start and end. char can be anything except a space (or a * for the \verb form).

share|improve this answer
    
Isn't there a simpler solution? My programs are generally short and I do not have too many... –  SoftTimur Aug 10 '11 at 22:03
    
Well... i have no other choice... thank you... –  SoftTimur Aug 10 '11 at 22:47
    
Do you know how to add a frame around this code, and may I use \caption and \label to make it a reference? –  SoftTimur Aug 10 '11 at 23:19
    
@SoftTimur: I've updated my answer to show how to frame a code segment using tabbing. If you want to use \caption and \label, you can put the entire tabbing environment in a float of your own choice. Create your own using the float package. –  Werner Aug 11 '11 at 3:53
1  
I suggest to use the listings package because it's very easy and fast to use –  Spike Aug 20 '11 at 6:59
show 1 more comment

The minted package uses the Pygments Python module as a backend to produce highlighted (and even colored) code in LaTeX.

You can get your own language supported by writing a new Pygment module. See this blog post for example.

To write comments to the right of the code, you could probably use a minipage.

share|improve this answer
add comment

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.