如何减少算法中的页边距?

如何减少算法中的页边距?

我正在编写一个算法的伪代码,但是我使用的模板边距很大。我想增加算法占用的空间,以便所有注释都可以放在一行中。到目前为止,我有这个:

\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}

它看起来像这样:

在此处输入图片描述

如何增加算法占用的水平尺寸?

谢谢。

答案1

虽然看起来很尴尬,但你可以做到:

在此处输入图片描述

\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}

algorithm您可以使用ere 选项设置[H],该选项允许您将其框起来。反过来,这允许您调整边距;上面我已将其添加5em到总宽度(或2.5em添加到任一侧,因为将\makebox其内容居中)。

algorithm由于盒子被放置在浮动figure环境内,因此该算法仍然允许浮动。


我更倾向于调整伪代码并堆叠一些元素以创建更多空间(伪代码或注释):

在此处输入图片描述

\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}

相关内容