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

share|improve this question
add comment

3 Answers

up vote 6 down vote accepted

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.

share|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
show 12 more comments

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

share|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
add comment

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}
share|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
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.