1

I am using the algpseudocode environment provided by the algorithmicx package in my thesis. I am wrapping the pseudocode inside the algorithmic environment provided by the algorithms package.

The issue is that the algorithm occupies the whole textwidth, which makes it a bit hard to parse (particularly comments are too far separated from the corresponding line). Furthermore, it is rather unnecessary to use the full text-width for typesetting small algorithms with one or two mathematical variables.

I would like to have a single place in my preamble to set the width of 12 algorithms in my thesis to a custom width, say 0.75\textwidth, so that it is easily changeable. This looks like a candidate for custom environment.

To adjust width, I am using a minipage, which works if I give up the float capabilities of the algorithms (which is fine). However, the algorithm is typeset flushed left in the page. To center it, the \makebox[\textwidth][c]{ ...} construct does not work with the custom environment.

Here's the MWE to illustrate the issue

\documentclass{article}

\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage[margin=1cm]{geometry}

\newcommand{\tempcaption}{}% stores the caption
\newcommand{\templabel}{}% stores the label

\newenvironment{customalgo}[2]
{%
    \begin{minipage}{0.7\textwidth}
        \begin{algorithm}[H]
            \centering
            \gdef\tempcaption{#1}% store the caption so we can use it later
            \gdef\templabel{#2}% store the label so we can use it later
            \begin{algorithmic}[1]
            }%
            {%
            \end{algorithmic}
            \caption{\tempcaption}% use the stored caption
            \label{\templabel}
        \end{algorithm}
        \bigskip
    \end{minipage}
}%

%%%%%% End of Preamble%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\textbf{Needs centering}\par
\begin{customalgo}{Computing a Sum - needs centering}{alg:algosumnonmakebox}
    \Procedure{SUM}{ $\{x\}$}

    \State $y\gets0$
    \For{$i \gets 1 : N^{x}$} \Comment{Time series $\{x\}$ has length $N^{x}$}
    \State $y\gets y+x(i)$ \Comment{Summing up.}
    \EndFor

    \State \textbf{return}  $y$
    \EndProcedure
\end{customalgo}

\textbf{Centered with makebox}\par
\noindent\makebox[\textwidth][c]{%
    \begin{customalgo}{Computing a Sum}{alg:algosum}
        \Procedure{SUM}{ $\{x\}$}

        \State $y\gets0$
        \For{$i \gets 1 : N^{x}$} \Comment{Time series $\{x\}$ has length $N^{x}$}
        \State $y\gets y+x(i)$ \Comment{Summing up.}
        \EndFor

        \State \textbf{return}  $y$
        \EndProcedure
    \end{customalgo}
}

\end{document}

which produces

centering algo boxes

The question is, how do I achieve centering of all algorithms without manually centering each one of these?

  • Place the customalgo macro, inside the \begin{center} \end{center} environment. Hope this helps. – Mishra S May 21 '18 at 12:08
-1

For me the below mentioned code works perfectly. Please check the attached screen shot.

enter image description here

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage[margin=1cm]{geometry}
\newcommand{\tempcaption}{}% stores the caption
\newcommand{\templabel}{}% stores the label
\newenvironment{customalgo}[2]
{%
\begin{minipage}{0.8\textwidth}
\centering
\begin{algorithm}[H]
\gdef\tempcaption{#1}% store the caption so we can use it later
\gdef\templabel{#2}% store the label so we can use it later
\begin{algorithmic}[1]
}%
{%
\end{algorithmic}
\caption{\tempcaption}% use the stored caption
\label{\templabel}
\end{algorithm}
\bigskip
\end{minipage}
}%
%%%%%% End of Preamble%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\textbf{Needs centering}
\begin{center}
\begin{customalgo}{Computing a Sum - needs centering {alg:algosumnonmakebox}
\Procedure{SUM}{ $\{x\}$}

\State $y\gets0$
\For{$i \gets 1 : N^{x}$} \Comment{Time series $\{x\}$ has length $N^{x}$}
\State $y\gets y+x(i)$ \Comment{Summing up.}
\EndFor

\State \textbf{return}  $y$
\EndProcedure
\end{customalgo}
\end{center}
\par \textbf{Centered with makebox}
%\noindent\makebox[\textwidth][c]{%
%\qquad
\begin{center}
\begin{customalgo}{Computing a Sum}{alg:algosum}
\Procedure{SUM}{ $\{x\}$}

\State $y\gets0$
\For{$i \gets 1 : N^{x}$} \Comment{Time series $\{x\}$ has length $N^{x}$}
\State $y\gets y+x(i)$ \Comment{Summing up.}
\EndFor

\State \textbf{return}  $y$
\EndProcedure
\end{customalgo}
%}
\end{center}

\end{document}
  • you did not understand my question. I had already achieved what I wanted within the body text. I have 12 algorithms to typeset and don't want to enclose each of them in a center environment. What I wanted to do was to declare a custom environment in the preamble that is pre-centered and readily usable multiple times within the body. – Krishna May 21 '18 at 16:44

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.