在框中输入算法

在框中输入算法

我正在尝试编写伪代码

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[colorinlistoftodos]{todonotes}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\algnewcommand\algorithmicinput{\textbf{Input:}}
\algnewcommand\INPUT{\item[\algorithmicinput]}
\algnewcommand\algorithmicoutput{\textbf{Output:}}
\algnewcommand\OUTPUT{\item[\algorithmicinput]}

\algrenewcommand{\algorithmicforall}{\textbf{for each}}
\newcommand{\INDSTATE}[1][1]{\STATE\hspace{#1\algorithmicindent}}



\def\ForEach{\ForAll}

\begin{algorithm}
\caption{My Algorithm}{ }
\begin{algorithmic}[1]


\INPUT  
\Statex a , \Comment The a of the algo
\Statex $b$  \Comment The b of the algo ,

\Statex $c$  \Comment The c of the algo 

\noindent \Statex  \hrulefill
\State $V \gets 0$


    \end{algorithmic}
    \end{algorithm}
    \end{document}
  1. 我希望它\INPUT位于一个框中或以某种方式被边框包围

  2. 目前我只是添加了一条水平线,但是它缩进了一下,有没有办法去除缩进?

答案1

您可以使用 TikZ\tikzmark来实现这一点:首先,在输入\tikzmark结束后立即放置一个标记\INPUT,并在输入结束处放置另一个标记,然后使用\DrawBox带有这两个标记的命令来绘制框架;可选参数允许您控制框架的某些属性(颜色,线宽):

\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[colorinlistoftodos]{todonotes}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage{tikz}

\newcommand\tikzmark[1]{%
  \tikz[remember picture,overlay]\node[inner sep=2pt] (#1) {};}
\newcommand\DrawBox[3][]{%
  \tikz[remember picture,overlay]\draw[#1] ([xshift=-3.5em,yshift=7pt]#2.north west) rectangle (#3.south east);}

\algnewcommand\algorithmicinput{\textbf{Input:}}
\algnewcommand\INPUT{\item[\algorithmicinput]}

\begin{document}

\begin{algorithm}
\caption{Expand $K$- Plex}{ }
\begin{algorithmic}[1]
\Statex
\INPUT\tikzmark{a}  
\Statex $a$ \Comment The a of the algo
\Statex $b$  \Comment The b of the algo
\Statex $c$  \Comment The c of the algo\tikzmark{b}
\State $V \gets 0$
\end{algorithmic}
\end{algorithm}

\DrawBox{a}{b}
    
\begin{algorithm}
\caption{Expand $K$- Plex}{ }
\begin{algorithmic}[1]
\Statex
\INPUT\tikzmark{c}  
\Statex $a$ \Comment The a of the algo
\Statex $b$  \Comment The b of the algo
\Statex $c$  \Comment The c of the algo\tikzmark{d}
\State $V \gets 0$
\end{algorithmic}
\end{algorithm}

\DrawBox[cyan]{c}{d}

\end{document}

在此处输入图片描述

该代码需要运行三次才能稳定。

在评论中,有人要求为框架设置背景颜色;这可以轻松实现,使用tikzmark库(该库提供了的复杂版本\tikzmark):

\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[colorinlistoftodos]{todonotes}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}

\newcommand\DrawBox[3][]{%
  \begin{tikzpicture}[remember picture,overlay]
    \draw[overlay,fill=gray!30,#1] 
    ([xshift=-3.7em,yshift=2.1ex]{pic cs:#2}) 
    rectangle 
    ([xshift=2pt,yshift=-0.7ex]pic cs:#3);
  \end{tikzpicture}%
}

\algnewcommand\algorithmicinput{\textbf{Input:}}
\algnewcommand\INPUT{\item[\algorithmicinput]}

\begin{document}

\DrawBox{a}{b}

\begin{algorithm}
\caption{Expand $K$- Plex}{ }
\begin{algorithmic}[1]
\Statex
\INPUT\tikzmark{a}  
\Statex $a$ \Comment The a of the algo
\Statex $b$  \Comment The b of the algo
\Statex $c$  \Comment The c of the algo\tikzmark{b}
\Statex
\State $V \gets 0$
\end{algorithmic}
\end{algorithm}

\DrawBox[draw=orange,fill=orange!30]{c}{d}

\begin{algorithm}
\caption{Expand $K$- Plex}{ }
\begin{algorithmic}[1]
\Statex
\INPUT\tikzmark{c}  
\Statex $a$ \Comment The a of the algo
\Statex $b$  \Comment The b of the algo
\Statex $c$  \Comment The c of the algo\tikzmark{d}
\Statex
\State $V \gets 0$
\end{algorithmic}
\end{algorithm}

\end{document}

在此处输入图片描述

一些评论:

  1. 该库提供了一个复杂的版本\tikzmark,因此一旦加载了该库,就无需再定义它。

  2. \DrawBox必须使用该命令将应用到的算法;否则,后台将覆盖输入的内容。

  3. 需要运行两到三次才能使代码稳定下来。

相关内容