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?


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




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}$\\
return $m_1,\dots,m_{M-i}$
\caption{Calculation of discrete drift parameters}\label{algo:drift}

enter image description here

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


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


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.

| improve this answer | |
  • 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 Apr 30 '12 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 Apr 30 '12 at 15:06
  • 1
    @MartinScharrer I see, now. – egreg Apr 30 '12 at 15:26
  • 1
    @meh This is a rather different question. The package mdframed allows breaks in framed objects. – egreg Apr 30 '12 at 16:06
  • 1
    @meh I've added it. – egreg Apr 30 '12 at 16:45

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:

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}.
   \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$
      \STATE $B_\mathrm{next}(k)\gets0$
      \STATE $A_\mathrm{prev}(k)\gets A_\mathrm{next}(k)$
   \RETURN $m_1,\ldots,m_{M-i}$

enter image description here

| improve this answer | |
  • Please, avoid \centerline. – egreg Apr 30 '12 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. – Martin Scharrer Apr 30 '12 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 Apr 30 '12 at 15:58

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:

   % add anything you want here ...
 \caption{My pseudocode!}

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

  \begin{minipage}[htb]{0.5\textwidth} % width 50%
    % add anything you want here ...
| improve this answer | |
  • 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 Apr 30 '12 at 13:02
  • Also, how do I change the top,bottom,left,right inner margins (the distances between content and box)? – meh Apr 30 '12 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 Apr 30 '12 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. – Martin Scharrer Apr 30 '12 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.