26

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)?

Werner
  • 603,163
SoftTimur
  • 19,767
  • 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 07: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 06 '13 at 14:41