3

I am using the algorithm2e package for my pseudo code since I like the like the options this package offers. However, the boxed option gives a text-width sized box and it doesn't look appealing to me when the code is much more narrow. This is a MWE.

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{amsmath} %
\usepackage{amssymb}

% real R
\newcommand{\R}{\mathbb{R}}

\usepackage[noline, boxed]{algorithm2e}

\begin{document}

 % ALGORITHM
 \begin{algorithm}
 \DontPrintSemicolon
 % \SetKwData{Left}{left} % normal
 \SetKwFunction{SDud}{SDud}\SetKwFunction{Tol}{tol}\SetKwFunction{Break}{break}% tt
 \SetKwInOut{Input}{input}
 \BlankLine
 \Input{$A \in \R^{m \times n} (m<n), \:\: b \in \R^m, \:\: x_{(0)}\in \R^n$.}
 \BlankLine
 \emph{\% Initialization}\;
 $x = x_{(0)}$\;
 $r = b - Ax$\;
 $p = A^Tr$\;
 \BlankLine
 \emph{\% Start \SDud}\;
 \For{$t = 0,1,2\ldots,t_{\max}$}{
  \BlankLine
  \emph{\% Compute $\alpha_{(t)}$}\;
  $q = Ap$ \;
  $\rho = p^Tp, \: \sigma = q^Tq, \: \alpha = \rho / \sigma$ \;
  \BlankLine
  \emph{\% Check for convergence}\;
  \lIf{$\rho \leq$ \Tol}{\Break}
  \BlankLine
  \emph{\% Compute new iterate $x_{(t+1)}$}\;
  $x \leftarrow x + \alpha p$
  \BlankLine
  \emph{\% Update residual $p_{(t+1)}$}\;
  $w = A^Tq$ \;
  $p \leftarrow p - \alpha w$
 }
 \caption{My alg}
 \end{algorithm}

\end{document}

And this is how it looks:

enter image description here

I don't like the white space on the right, so I wish it was a tight, centered box around the pseudo code. Unfortunately the algorithm2e documentation doesn't provide me with any insight regarding this issue. Can somebody here help me out?

Cheerse

3

This is adapted from Thomas F. Sturm's solution to this question. You can make your algorithm unfloatable and box it within another floatable environment---in this case, an invisible tcolorbox. Change the width variable to adapt the box around your algorithm.

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{amsmath} %
\usepackage{amssymb}
\usepackage[noline,boxed]{algorithm2e}
\usepackage[skins]{tcolorbox}

% real R
\newcommand{\R}{\mathbb{R}}

\begin{document}

\begin{tcolorbox}[blanker,width=(\linewidth-3.5cm)]
\begin{algorithm}[H]
 \DontPrintSemicolon
 % \SetKwData{Left}{left} % normal
 \SetKwFunction{SDud}{SDud}\SetKwFunction{Tol}{tol}\SetKwFunction{Break}{break}% tt
 \SetKwInOut{Input}{input}
 \BlankLine
 \Input{$A \in \R^{m \times n} (m<n), \:\: b \in \R^m, \:\: x_{(0)}\in \R^n$.}
 \BlankLine
 \emph{\% Initialization}\;
 $x = x_{(0)}$\;
 $r = b - Ax$\;
 $p = A^Tr$\;
 \BlankLine
 \emph{\% Start \SDud}\;
 \For{$t = 0,1,2\ldots,t_{\max}$}{
  \BlankLine
  \emph{\% Compute $\alpha_{(t)}$}\;
  $q = Ap$ \;
  $\rho = p^Tp, \: \sigma = q^Tq, \: \alpha = \rho / \sigma$ \;
  \BlankLine
  \emph{\% Check for convergence}\;
  \lIf{$\rho \leq$ \Tol}{\Break}
  \BlankLine
  \emph{\% Compute new iterate $x_{(t+1)}$}\;
  $x \leftarrow x + \alpha p$
  \BlankLine
  \emph{\% Update residual $p_{(t+1)}$}\;
  $w = A^Tq$ \;
  $p \leftarrow p - \alpha w$
 }
 \caption{My alg}
\end{algorithm}
\end{tcolorbox}

\end{document}

My result looks like this:

Algorithm within a tcolorbox

  • Sorry the delayed response, anyway, thank you, I like this solution! – Koen Jul 20 '16 at 8:33
1

I have found a solution from other topics, though not the most elegant one I suppose. You can do it with minpages but then you have to give your algorthm environment the [H] specifier such that it isn't a float anymore. I'd prefer if it would remain a float but it will do...

Here's an example

\begin{center}
\begin{minipage}{0.7\textwidth}
\begin{algorithm}[H]
 \DontPrintSemicolon
 % \SetKwData{Left}{left} % normal
 \SetKwFunction{SDud}{SDud}\SetKwFunction{Tol}{tol}\SetKwFunction{Break}{break}% tt
 \SetKwInOut{Input}{input}
 \BlankLine
 \Input{$A \in \R^{m \times n} (m<n), \:\: b \in \R^m, \:\: x_{(0)}\in \R^n$.}
 \BlankLine
 \emph{\% Initialization}\;
 $x = x_{(0)}$\;
 $r = b - Ax$\;
 $p = A^Tr$\;
 \BlankLine
 \emph{\% Start \SDud}\;
 \For{$t = 0,1,2\ldots,t_{\max}$}{
  \BlankLine
  \emph{\% Compute $\alpha_{(t)}$}\;
  $q = Ap$ \;
  $\rho = p^Tp, \: \sigma = q^Tq, \: \alpha = \rho / \sigma$ \;
  \BlankLine
  \emph{\% Check for convergence}\;
  \lIf{$\rho \leq$ \Tol}{\Break}
  \BlankLine
  \emph{\% Compute new iterate $x_{(t+1)}$}\;
  $x \leftarrow x + \alpha p$
  \BlankLine
  \emph{\% Update residual $p_{(t+1)}$}\;
  $w = A^Tq$ \;
  $p \leftarrow p - \alpha w$
 }
 \caption{My alg}
 \end{algorithm}
\end{minipage}
\end{center}

Any improvements are welcome still.

  • Nobody that has a solution? It would save me a lot of hassle if the algorithms could remain to be a float – Koen May 3 '16 at 19:42
0

I would suggest to use the figure environment as a wrapper to keep the algorithm as a float. Without adding a caption or label in the figure you should not have problems with the \listoffigures

\begin{figure}[htb]
    \centering
    \begin{minipage}{.7\linewidth}
    \begin{algorithm}[H]
        \KwData{this text}
        \KwResult{how to write algorithm with \LaTeX2e }
        initialization\;
        \While{not at end of this document}{
            read current\;
            \eIf{understand}{
                go to next section\;
                current section becomes this one\;
            }{
                go back to the beginning of current section\;
            }
        }
        \caption{How to write algorithms}
        \label{algo:black draw}
    \end{algorithm}
  \end{minipage}
\end{figure}

You could also go to a cleaner solution as defining a new env to wrap everything as described in this topic

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.