2

I'm writing the pseudocode of an algorithm, however, I'm using a template with big margins. I wanted to increase the space occupied by the algorithm so all the comments can fit in one line only. So far I have this:

\begin{algorithm}
\begin{algorithmic}[1]
    \ForAll{pixels k} 
        \State $f_k\gets 0$
        \ForAll{pulses p}
            \State $R\gets || a_k - v_p ||$ \Comment{calculate distance from platform to pixel}
            \State $bin\gets \lfloor (R - R0)/\Delta R \rfloor$ \Comment{range bin (integer)}
            \If{$bin \in [0, N_bp - 2]$} 
                \State $w\gets \lfloor (R - R0)/\Delta R \rfloor - bin$ \Comment{interpolation weight}          
                \State $s\gets (1 - w)\cdot g(p, bin) + w \cdot g(p, bin + 1)$ \Comment{data sampled using linear interpolation}
                \State $f_k\gets f_k + e^{j \cdot ku \cdot R}$ \Comment{add pulse's contribution to the pixel}         
            \EndIf                
        \EndFor
    \EndFor    
\end{algorithmic}
\caption{Backprojection algorithm pseudocode. \\ \textbf{Source:} PERFECT Manual Suite \cite{perfect2013}.}
\label{alg-bp}
\end{algorithm}

It looks like this:

enter image description here

How can I increase the horizontal size occupied by the algorithm?

Thank you.

  • 1
    The adjustwidth from changepage can be helpful. – Bernard Dec 15 '17 at 16:40
  • @Bernard that's exactly what I wanted, thank you! – Hel Dec 15 '17 at 16:45
1

It looks awkward, but you can do it:

enter image description here

\documentclass{article}

\usepackage{algorithm,algpseudocode,changepage}
\usepackage{lipsum}

\begin{document}

\lipsum[1]

\begin{figure}
  \makebox[\linewidth]{%
  \begin{minipage}{\dimexpr\linewidth+5em}
    \begin{algorithm}[H]
      \begin{algorithmic}[1]
        \ForAll{pixels $k$}
          \State $f_k \gets 0$
          \ForAll{pulses $p$}
            \State $R \gets || a_k - v_p ||$ \Comment{calculate distance from platform to pixel}
            \State $b \gets \lfloor (R - R0)/\Delta R \rfloor$ \Comment{range bin (integer)}
            \If{$b \in [0, N_b p - 2]$}
              \State $w \gets \lfloor (R - R0)/\Delta R \rfloor - b$ \Comment{interpolation weight}
              \State $s \gets (1 - w) \cdot g(p, b) + w \cdot g(p, b + 1)$ \Comment{data sampled using linear interpolation}
              \State $f_k \gets f_k + e^{j \cdot k u \cdot R}$ \Comment{add pulse's contribution to the pixel}
            \EndIf                
          \EndFor
        \EndFor    
      \end{algorithmic}
      \caption[Backprojection algorithm pseudocode]
        {Backprojection algorithm pseudocode. \\
          \textbf{Source:} PERFECT Manual Suite \cite{perfect2013}.}
    \end{algorithm}
  \end{minipage}}
\end{figure}

\lipsum[2]

\begin{thebibliography}{x}
  \bibitem{perfect2013} Author, title.
\end{thebibliography}

\end{document}

You can set the algorithm using the [H]ere option, which allows you to box it. In turn, this allows you to adjust the margins; above I've added 5em to the total width (or 2.5em on either side, since the \makebox centres its contents).

The algorithm is still allowed to float, since the algorithm box is placed inside a floating figure environment.


I'd be more inclined to adjust the pseudocode and stacking some of the elements to create more room (either the pseudocode, or the comment):

enter image description here

\documentclass{article}

\usepackage{algorithm,algpseudocode}
\usepackage{lipsum}

\begin{document}

\lipsum[1]

\begin{algorithm}[!th]
  \begin{algorithmic}[1]
    \ForAll{pixels $k$}
      \State $f_k \gets 0$
      \ForAll{pulses $p$}
        \State $R \gets || a_k - v_p ||$ \Comment{calculate distance from platform to pixel}
        \State $b \gets \lfloor (R - R0)/\Delta R \rfloor$ \Comment{range bin (integer)}
        \If{$b \in [0, N_b p - 2]$}
          \State $w \gets \lfloor (R - R0)/\Delta R \rfloor - b$ \Comment{interpolation weight}
          \State $s \gets \begin{array}[t]{@{}l@{}}
            (1 - w) \cdot g(p, b) \\
            {} + w \cdot g(p, b + 1)
          \end{array}$ \Comment{data sampled using linear interpolation}
          \State $f_k \gets f_k + e^{j \cdot k u \cdot R}$ \Comment{add pulse's contribution to the pixel}
        \EndIf                
      \EndFor
    \EndFor    
  \end{algorithmic}
  \caption[Backprojection algorithm pseudocode]
    {Backprojection algorithm pseudocode. \\
      \textbf{Source:} PERFECT Manual Suite \cite{perfect2013}.}
\end{algorithm}

\lipsum[2]

\begin{thebibliography}{x}
  \bibitem{perfect2013} Author, title.
\end{thebibliography}

\end{document}

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.