如何为并排放置的算法添加边框

如何为并排放置的算法添加边框

我使用下面的代码将两种算法并排放置,但是它们没有边界,很难识别这两种算法。

代码:

\documentclass{llncs}% http://www.springer.com/computer/lncs/lncs+authors?SGWID=0-40209-0-0-0
%\usepackage[margin=1in]{geometry}% http://ctan.org/pkg/geometry
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage[compatibility=false]{caption}% http://ctan.org/pkg/caption
\begin{document}

\medskip

\noindent\begin{minipage}{.5\textwidth}
\captionof{algorithm}{Euclid’s algorithm}\label{algo1}
\begin{algorithmic}[1]
  \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
    \State $r\gets a\bmod b$
    \While{$r\not=0$}\Comment{We have the answer if r is 0}
      \State $a\gets b$
      \State $b\gets r$
      \State $r\gets a\bmod b$
    \EndWhile
    \State \textbf{return} $b$\Comment{The gcd is b}
  \EndProcedure
\end{algorithmic}
\end{minipage}%
\begin{minipage}{.5\textwidth}
\captionof{algorithm}{Euclid’s algorithm}\label{algo2}
\begin{algorithmic}[1]
  \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
    \State $r\gets a\bmod b$
    \While{$r\not=0$}\Comment{We have the answer if r is 0}
      \State $a\gets b$
      \State $b\gets r$
      \State $r\gets a\bmod b$
      \State $d$
    \EndWhile
    \State \textbf{return} $b$\Comment{The gcd is b}
  \EndProcedure
\end{algorithmic}
\end{minipage}

\medskip

On the left is Algorithm~\ref{algo1}. On the right is Algorithm~\ref{algo2}.

\end{document} 

它看起来像:

[![在此处输入图片描述][1]][1]

我希望它看起来像这样 1. 有边框 2. 顶部对齐

下面是绘制边框的结果。在此处输入图片描述

答案1

选择宽度稍小的 minipages 并添加可选的[t]。由于algorithm必须删除环境,我定义了一个ruled标题格式来模仿算法的“规则”标题格式。此外,我添加了inputenc[T1]{fontenc}包:文档缺少撇号,如果需要的话,可能还有重音字母:

\documentclass{llncs}% http://www.springer.com/computer/lncs/lncs+authors?SGWID=0-40209-0-0-0
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage[compatibility=false]{caption}% http://ctan.org/pkg/caption
\DeclareCaptionFormat{ruled}{\leavevmode\leaders\hrule height 0.8pt depth0pt\hfill\mbox{}\endgraf#1#2 #3 \vspace{-0.4\baselineskip}\leavevmode\leaders\hrule height 0.6pt\hfill\null\vspace*{-0.6\baselineskip}}
 \algrenewcommand\algorithmiccomment[1]{\hfill\({}\triangleright{}\){\footnotesize#1}}%

\begin{document}

\noindent
\captionsetup{format=ruled, labelfont=sc}
   \begin{minipage}[t]{.45\textwidth}
    \captionof{algorithm}{Euclid’s algorithm}\label{algo1}
    \begin{algorithmic}[1]
      \Procedure{Euclid}{$a,b$}

      \Comment{The g.c.d. of a and b}
        \State $r\gets a\bmod b$
        \While{$r\not=0$}\Comment{We have the answer if r is 0}
          \State $a\gets b$
          \State $b\gets r$
          \State $r\gets a\bmod b$
        \EndWhile
        \State \textbf{return} $b$\Comment{The gcd is b}
      \EndProcedure
    \end{algorithmic}
\kern2pt\hrule height.8pt\relax
    \end{minipage}%
    \hfill
    \begin{minipage}[t]{.45\textwidth}
    \captionof{algorithm}{Euclid’s algorithm}\label{algo2}
    \begin{algorithmic}[1]
      \Procedure{Euclid}{$a,b$}

      \Comment{The g.c.d. of a and b}
        \State $r\gets a\bmod b$
        \While{$r\not=0$}

        \Comment{We have the answer if r is 0}
          \State $a\gets b$
          \State $b\gets r$
          \State $r\gets a\bmod b$
          \State $d$
        \EndWhile
        \State \textbf{return} $b$\Comment{The gcd is b}
      \EndProcedure
    \end{algorithmic}
\kern2pt\hrule height.8pt\relax
\end{minipage}
\medskip

On the left is Algorithm~\ref{algo1}. On the right is Algorithm~\ref{algo2}.

\end{document} 

在此处输入图片描述

相关内容