将算法放入盒子里

将算法放入盒子里

以下是我的代码片段。请帮我在算法中放置一个方框。

\documentclass[journal,12pt]{IEEEtran}
\usepackage{algorithmic}
\usepackage{graphicx}
\usepackage{algorithm}
\usepackage{nicefrac}
\usepackage{mathtools}
\usepackage{tabularx,booktabs,ragged2e}

\begin{document}
%\begin{pseudocode}[<ovalbox>]{}{}
\textit{Algorithm 1}:  Collaborative Spectrum Sensing 
%\begin{description}
\begin{enumerate}
    \item[\textit{1.}] For each contributor $S_i$. 
    \item[\textit{2.}] Calculate the entropy.\newline
    \item[\textit{3.}] Select the channel and proceeds as follows:
    \begin{enumerate}
        \item[i.] Receives the utility.
        \item[ii.] Otherwise 
        \begin{enumerate}
            \item[a.] Get the largest entropy
            \item[b.]\begin{algorithmic} 
                \IF {($TTL$ $>$ 0)}
                \STATE  Set $PRE$ field with its $ID$; \newline
                \ELSE
                \STATE discard $M$
                \ENDIF
            \end{algorithmic}
       \end{enumerate}
    \end{enumerate}
    \item[\textit{4.}] Each user updates the prob of each action.
\end{enumerate}
%\end{pseudocode}
\end{document}

答案1

绘制框的一种方法是使用mdframed

在此处输入图片描述

代码:

\documentclass[journal,12pt]{IEEEtran}
\usepackage{algorithmic}
\usepackage{graphicx}
\usepackage{algorithm}
\usepackage{nicefrac}
\usepackage{mathtools}
\usepackage{tabularx,booktabs,ragged2e}
\usepackage{xcolor}
\usepackage{showframe}
\usepackage[framemethod=tikz]{mdframed}

\begin{document}
\begin{mdframed}[
    linecolor=red,
    linewidth=2pt,
    roundcorner=4pt,
    backgroundcolor=olive!15,
    userdefinedwidth=\textwidth,
]
%\begin{pseudocode}[<ovalbox>]{}{}
\textit{Algorithm 1}:  Collaborative Spectrum Sensing 
%\begin{description}
\begin{enumerate}
    \item[\textit{1.}] For each contributor $S_i$. 
    \item[\textit{2.}] Calculate the entropy.\newline
    \item[\textit{3.}] Select the channel and proceeds as follows:
    \begin{enumerate}
        \item[i.] Receives the utility.
        \item[ii.] Otherwise 
        \begin{enumerate}
            \item[a.] Get the largest entropy
            \item[b.]\begin{algorithmic} 
                \IF {($TTL$ $>$ 0)}
                \STATE  Set $PRE$ field with its $ID$; \newline
                \ELSE
                \STATE discard $M$
                \ENDIF
            \end{algorithmic}
       \end{enumerate}
    \end{enumerate}
    \item[\textit{4.}] Each user updates the prob of each action.
\end{enumerate}
%\end{pseudocode}
\end{mdframed}
\end{document}

相关内容