TransWikia.com

Mutliple inputs with line breaking

TeX - LaTeX Asked by Thilo on April 1, 2021

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?

4 Answers

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

documentclass{article}
usepackage{algorithm2e}

newlengthmylen
newcommandmyinput[1]{%
  settowidthmylen{KwIn{}}%
  setlengthhangindent{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

Correct answer by Gonzalo Medina on April 1, 2021

SetKwInOut{Input}{input}  
Input{a
b
c  
}

works fine

Answered by user1436280 on April 1, 2021

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 1newline
            Input 2newline
            Input 3}

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

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

end{algorithm}
end{document}

Answered by Piero Macaluso on April 1, 2021

Simply use the defined command Indp in the package

    KwIn{
            Indp Indp
            Input 1  
            Input 2 
            Input 3
    }

MWE:

documentclass{article}
usepackage{algorithm2e}

begin{document}
    abc
    begin{algorithm}
        KwIn{
            Indp Indp
            Input 1  
            Input 2 
            Input 3
        }
        Begin{
            Instruction ;
            Instruction ;
            Instruction ;
            Instruction ;
        }
    end{algorithm}
    abc
end{document}

enter image description here

Answered by duylamvo on April 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP