4

In my document, I have some pseudocode sections created using the program environment (tried several alternatives, but liked that one most, so I want to stick to that). Now I would simply like to indent the pseudocode sections on the left by some millimeters to make it more stand out from the body text, as depicted here:

IndentProgram

Is there some simple way to achieve that (globally, if possible)?

MWE for the given example:

\documentclass[ngerman]{scrbook}
\usepackage{program}
\usepackage{lipsum}
\begin{document}

\begin{figure}
    \begin{program}
        \BEGIN
        a=a+1;
        \END
    \end{program}
    \caption{Example code}
    \label{alg:exampleCode}
\end{figure}

 \lipsum[25]
 \end{document}

2 Answers 2

3

Patch program so that it thinks to be in a list environment indented by the amount you prefer.

\documentclass{book}
\usepackage{program}
\usepackage{etoolbox}

\usepackage{lipsum}

\pretocmd{\program}{\list{}{\leftmargin3em}\item\relax}{}{}% <---- change 3em to suit
\apptocmd{\endprogram}{\endlist}{}{}

\begin{document}

\begin{figure}
\begin{program}
  \BEGIN
  a=a+1;
  \END
\end{program}

\caption{Example code}
\label{alg:exampleCode}

\end{figure}

\lipsum[25]
\end{document}

enter image description here

You should contact the package author: the code is replete with \sf, \rm and \bf commands that have been obsolete for twenty years. With scrbook you get warnings that the commands may be removed from the class in a near future.

1

The problem is that the program environment cannot be placed into a \hbox and so it can never be "centered" except by hand. However, by placing it into a \vbox, it can be indented by a fixed amount (here 2in), if that solution is sufficient for your need.

The \makebox[0pt][l]{} is only to avoid an overfull box, lest the \vbox protrudes out the right margin.

This solution will not page break, but already being inside a figure float. that should not be an issue.

\documentclass[ngerman]{scrbook}
\usepackage{program}
\usepackage{lipsum}
\begin{document}

\begin{figure}
\setbox0=\vbox{%
    \begin{program}
        \BEGIN
        a=a+1;
        \END
    \end{program}
}\hspace{2in}\makebox[0pt][l]{\box0}
    \caption{Example code}
    \label{alg:exampleCode}
\end{figure}

 \lipsum[25]
 \end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .