15

I am trying to write an algorithm with multiple inputs in algorithm2e.

The result I would like to have is something like

Algorithm
-------------------
Input: Input number 1
       Input number 2
       Input number 3

[Some clever algorithm]

The best I could do yet was one of those two solutions

\begin{algorithm}
\KwIn{Input number 1}
\KwIn{Input number 2\\ Input number 3
\end{algorithm}

which archives

Algorithm
-------------------
Input: Input number 1
Input: Input number 2
Input number 3

Any idea how to get the desired result?

10

You can define a new command to give you the desired indentation:

\documentclass{article}
\usepackage{algorithm2e}

\newlength\mylen
\newcommand\myinput[1]{%
  \settowidth\mylen{\KwIn{}}%
  \setlength\hangindent{\mylen}%
  \hspace*{\mylen}#1\\}

\begin{document}

\begin{algorithm}
\KwIn{Input number 1}
\myinput{Input number 2}
\myinput{Input number 3 spanning more than one line just as an illustration for the example}
\end{algorithm}

\end{document}

enter image description here

  • 1
    Using \usepackage[linesnumbered]{algorithm2e}, the additional input lines receive a linenumber. How can I prevent this from happening? – BJPrim Jun 28 '19 at 7:41
9
\SetKwInOut{Input}{input}  
\Input{a\\
b\\
c  
}

works fine

3

I solved the problem by using \newline instead of \\. Here it is a working example:

\documentclass{article}
\usepackage[ruled,vlined,dotocloa]{algorithm2e}
\usepackage{listings}

\begin{document}
\begin{algorithm}[!h]
    \SetAlgoLined
    \small
    \DontPrintSemicolon
    \LinesNumbered

    \KwIn{Input 1\newline
            Input 2\newline
            Input 3}

    Some smart pseudocode line 1 \;
    Some smart pseudocode line 2 \;
    Some smart pseudocode line 3 \;

    \KwOut{ Output 1\newline
            Output 2}
    \caption{My Fabulous Algorithm}

\end{algorithm}
\end{document}
  • 1
    Welcome to TeX.SX! This seems to be also a good solution, but it will be even more helpful if you edit it to become a minimal working example, which should begin with \documentclass{...} and end with \end{document}. – Vincent Dec 23 '19 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.