[Tex/LaTex] Algorithm input in a box

algorithmicxalgorithms

I am trying to write pseudo code

\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. I want the \INPUT to be in a box or somehow surrounded in a border

  2. Currently I just added an horizontal line, but it is indented, is there a way to remove the indentation?

Best Answer

You can use TikZ and \tikzmark for this: first, using \tikzmark you place a mark right after \INPUT and another one right where your inputs end, and then use the \DrawBox command with those two marks to draw the frame; the optional argument allows you to control some attributes (color, line width) of the frame:

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

enter image description here

The code needs three runs to stabilize.

In a comment it has been requested to have a background color for the frame; this can be easily achieved using the tikzmark library (the library provides a sophisticated version of \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}

enter image description here

Some remarks:

  1. The library provides a sophisticated version of \tikzmark, so there's no need to define it once the library has been loaded.

  2. The \DrawBox command must be used before the algorithm to which it will be applied; otherwise, the background will overwrite the contents of the input.

  3. Two or three runs are necessary for the code to stabilize.

Related Question