7

I want to create a pseudocode environment that looks like this:

source cl.ly/3O2W203g3j1I3T080x37

Basically, the font doesn't change - it's just the same as that of the surrounding text. It allows mathmode. And there is a box around it that fits the content vertically and horizontally (with some pre-defined margin), and then horizontally centers the entire box on the page.

How can I accomplish this?

3 Answers 3

6

You can use the tabbing environment, together with a personal environment:

\documentclass[a4paper]{article}

\usepackage{amsmath}

\newenvironment{boxedcode}
 {\setlength{\fboxsep}{1em}%
  \begin{lrbox}{\boxedcodebox}
  \begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}
  \begin{tabbing}}
 {\end{tabbing}
  \end{minipage}
  \end{lrbox}%
  \fbox{\usebox{\boxedcodebox}}}
\newsavebox{\boxedcodebox}

\begin{document}
\begin{figure}
\centering
\begin{boxedcode}
Inputs: $s_{j}{k}$, \= $j=1,\dots,M-i$, $k=1,\dots,d$ as in \eqref{whatever}\\
                    \> and $h_1,\dots,h_M$ ($h_l=t_l-t_{l-1}$)\\[\baselineskip]
$A_\mathrm{prev}(k)\leftarrow 0$, $k=1,\dots,d$\\
for \= $j=1,\dots,M-i$\\
    \> $B_\mathrm{next}\leftarrow 0$\\
    \> for \= $k=1,\dots,d$\\
    \>     \> $A_\mathrm{next}(k)\leftarrow A_\mathrm{prev}(k)+s_j(k)*h_{i+j}$\\
    \>     \> $B_\mathrm{next}\leftarrow B_\mathrm{next}+A_\mathrm{next}(k)*A_\mathrm{next}(k)$\\
    \>     \> $A_\mathrm{prev}(k)\leftarrow A_\mathrm{next}(k)$\\
    \> end\\
    \> $m_j\leftarrow (B_\mathrm{next}-B_\mathrm{prev})/(2h_{i+j})$\\
    \> $B_\mathrm{prev}\leftarrow B_\mathrm{next}$\\
end\\
return $m_1,\dots,m_{M-i}$
\end{boxedcode}
\caption{Calculation of discrete drift parameters}\label{algo:drift}
\end{figure}
\end{document}

enter image description here

As Martin Scharrer points out, the code for the environment can be simplified by loading the adjustbox package:

\usepackage{adjustbox}
\newenvironment{boxedcode}
 {\setlength{\fboxsep}{1em}%
  \begin{adjustbox}{minipage=\textwidth-2\fboxsep-2\fboxrule,fbox}
  \begin{tabbing}}
 {\end{tabbing}
  \end{adjustbox}}

This avoids the need to allocate a box bin.


Here is a version that allows page breaks (of course it shouldn't be set in a figure environment):

\usepackage{mdframed}
\newenvironment{boxedcode}
 {\setbox\boxedcodebox=\vbox\bgroup
  \begin{tabbing}}
 {\end{tabbing}\egroup
  \begin{mdframed}[
    leftmargin=\dimexpr(\textwidth-1em-\fboxrule-\wd\boxedcodebox)/2\relax,
    rightmargin=\dimexpr(\textwidth-1em-\fboxrule-\wd\boxedcodebox)/2\relax,
    innerleftmargin=1em,
    innerrightmargin=1em,
    innertopmargin=1em,
    innerbottommargin=1em,
    skipabove=\topsep,
  ]
  \vskip-\baselineskip\unvbox\boxedcodebox
  \end{mdframed}
 }
\newsavebox{\boxedcodebox}
\newlength{\boxedcodewd}

The environment should be always preceded and followed by blank lines and not put inside a center environment, because the centering is already taken care of by the set parameters.

If the class used is book the keys leftmargin and rightmargin should be replaced by innermargin and outermargin.

Please, keep the inner padding space.

17
  • 1
    @MarcoDaniel The minipage will be as wide as the tabbing environment requires anyway, if it doesn't exceed \textwidth. If the tabbing lines are too long one will notice it anyway. But I'll modify the code.
    – egreg
    Commented Apr 30, 2012 at 15:03
  • 1
    @MartinScharrer A minipage or \parbox containing only a tabbing environment won't be wider than necessary to enclose it. So adjustbox or varwidth are not necessary in this case.
    – egreg
    Commented Apr 30, 2012 at 15:06
  • 1
    @MartinScharrer I see, now.
    – egreg
    Commented Apr 30, 2012 at 15:26
  • 1
    @meh This is a rather different question. The package mdframed allows breaks in framed objects.
    – egreg
    Commented Apr 30, 2012 at 16:06
  • 1
    @meh I've added it.
    – egreg
    Commented Apr 30, 2012 at 16:45
3

If you don't need exotic control structures, you could also have a look at the algorithm package. It provides a set of statements that are aligned automatically. Some tweaking might me necessary, though. A stripped down version of your example:

\documentclass{article}
\pagestyle{empty}
\usepackage{algorithmic}
\renewcommand{\algorithmicrequire}{\textbf{Inputs:}}
\begin{document}
and each $A_\mathrm{next}(k)$ records a quantity of the form
\[
   \sum_{\ell=i}^j \hat{\sigma}(t_{i-1},t_\ell)h_{\ell+1}.
\]
\centerline{\fbox{
\begin{minipage}{7cm}
\begin{algorithmic}
   \REQUIRE $s_j(k), j=1,\ldots,M-i$ as in (3.101) and $h_1,\ldots,h_m$
   \STATE $A_\mathrm{prev}(k)\gets0,\, k=1,\ldots,d$
   \FOR{$j=1,\ldots,M-1$}
      \STATE $B_\mathrm{next}(k)\gets0$
      \FOR{$j=1,\ldots,d$}
      \STATE $A_\mathrm{prev}(k)\gets A_\mathrm{next}(k)$
      \ENDFOR
   \ENDFOR
   \RETURN $m_1,\ldots,m_{M-i}$
\end{algorithmic}
\end{minipage}}}
\end{document}

enter image description here

3
  • Please, avoid \centerline.
    – egreg
    Commented Apr 30, 2012 at 15:07
  • 1
    The line break after \fbox{ will add a space. You need to write \fbox{% or remove the line break in the source code. See my comment to @egreg's answer for a easy solution based on my adjustbox package. Commented Apr 30, 2012 at 15:10
  • It is also possible to use \STATE-only commands to remove any formatting, or modify the formatting altogether. Then there's also algpseudocode from the algorithmicx package.
    – Werner
    Commented Apr 30, 2012 at 15:58
1

You can use the package float to create custom environments. See wikibooks: custom floats.

(1) Import package and create an environment, e.g. pseudocode:

\usepackage{float} % Import of the package.
 \floatstyle{boxed} % This creates the box around your environment.
 \newfloat{pseudocode}{htb}{lop}[chapter] % Placing and numbering.
 \floatname{pseudocode}{Pseudocode} % Name of the float displayed in the document.

(2) At the location where you need the figure, add your created environment:

\begin{pseudocode}
 \centering
   % add anything you want here ...
 \caption{My pseudocode!}
 \label{code:mycode}
\end{pseudocode}

Updated - the aligning is not trivial, indeed. You can use minipages surrounded by \fbox with borders.

\begin{center}
 \fbox{
  \begin{minipage}[htb]{0.5\textwidth} % width 50%
    % add anything you want here ...
  \end{minipage}
 }
\end{center}
4
  • That doesn't horizontally fit the box to the content - it makes the box take up the entire horizontal page no matter how narrow the content is...
    – meh
    Commented Apr 30, 2012 at 13:02
  • Also, how do I change the top,bottom,left,right inner margins (the distances between content and box)?
    – meh
    Commented Apr 30, 2012 at 13:04
  • Thanks for your efforts. However, the updated code fixes the width at 50%. It's not flexible according to the content.
    – meh
    Commented Apr 30, 2012 at 13:39
  • 1
    You have some issues here: \fbox{ will add a space, you need to write \fbox{% or remove the line break. The minipage argument [htb] looks wrong, it's either t or b etc. Also center should not be used inside floats. Use \centering instead. Commented Apr 30, 2012 at 15:09

You must log in to answer this question.

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