2

I would like to fully justify the input/output blocks in the algorithm2e environment. How can this be achieved?

I cannot find anything regarding this matter in the documentation. To be precise, I would like to keep the indentation as it is, just have the text block fully justified. (I tried just using ragged2e and simply putting \justify in the beginning of the input contents, but that did weird things.)

MWE (ignore the algorithm):

\documentclass{article}
\usepackage[algoruled, linesnumbered]{algorithm2e}
\SetKwInOut{Input}{Input}
\DontPrintSemicolon
\begin{document}
\begin{algorithm}
    \caption{Getting things done}
    \Input{Many things with longish names that make the ragged right look ugly in my opinion. Many things with longish names that make the ragged right look ugly in my opinion. Many things with longish names that make the ragged right look ugly in my opinion.}
    \For{$k=1$ \KwTo $N$}{
        \tcp{Do something}
        $x_k^- = A_{k-1} x_{k-1}^+ + B_{k-1} u_{k-1}$ \;
        $P_{k}^- = A_{k-1} P_{k-1}^+ A_{k-1}^T + Q_{k-1}$ \;
        \tcp{And now do something else}
        $K_k = P_{k}^- C_k^T (C_k P_{k}^- C_k^T + R_{k})^{-1}$ \;
        $x_{k}^+ = x_{k}^- + K_{k}(y_{k}-C_k x_{k}^-)$ \;
        $P_{k}^+ = (I - K_{k} C_k) P_{k}^-$ \;
    }
\end{algorithm}
\end{document}  

enter image description here

1
  • 1
    To be a bit more specific, \Input{\justify Text...} introduces a line break after "Input:" and also removes the indentation.
    – Eike P.
    Commented Jul 28, 2020 at 15:20

1 Answer 1

3

Try this:

\documentclass{article}
\usepackage[algoruled, linesnumbered]{algorithm2e}
\usepackage{ragged2e}
\usepackage{xpatch}

\makeatletter
\xpatchcmd\SetKwInOut
  {\hangafter=1\parbox[t]}
  {\hangafter=1\justify\parbox[t]}
  {}{\fail}
\makeatother

\SetKwInOut{Input}{Input}
\DontPrintSemicolon

\begin{document}
\begin{algorithm}
    \caption{Getting things done}
    \Input{Many things with longish names that make the ragged right look ugly in my opinion. Many things with longish names that make the ragged right look ugly in my opinion. Many things with longish names that make the ragged right look ugly in my opinion.}
    \For{$k=1$ \KwTo $N$}{
        \tcp{Do something}
        $x_k^- = A_{k-1} x_{k-1}^+ + B_{k-1} u_{k-1}$ \;
        $P_{k}^- = A_{k-1} P_{k-1}^+ A_{k-1}^T + Q_{k-1}$ \;
        \tcp{And now do something else}
        $K_k = P_{k}^- C_k^T (C_k P_{k}^- C_k^T + R_{k})^{-1}$ \;
        $x_{k}^+ = x_{k}^- + K_{k}(y_{k}-C_k x_{k}^-)$ \;
        $P_{k}^+ = (I - K_{k} C_k) P_{k}^-$ \;
    }
\end{algorithm}
\end{document}

enter image description here

Some explanations:

When calling \SetKwInOut{Input}{Input}, it defines \Input by

% here #1 is first arg of \SetKwInOut, here it is "Input"
\algocf@newcommand{#1}[1]{%
    \ifthenelse{\boolean{algocf@hanginginout}}{\relax}{\algocf@seteveryparhanging{\relax}}%
    \ifthenelse{\boolean{algocf@inoutnumbered}}{\relax}{\algocf@seteveryparnl{\relax}}%
    % The following line control the output of \Input{##1}.
    % This paragraph starts at \parbox, hence the change of paragraph
    % indent (\justify) should be inserted before \parbox.
    {\let\\\algocf@newinout\hangindent=\inoutindent\hangafter=1\parbox[t]{\inoutsize}{\KwSty{#2\algocf@typo\hfill:}}~##1\par}%
    \algocf@linesnumbered% reset the numbering of the lines
    \ifthenelse{\boolean{algocf@hanginginout}}{\relax}{\algocf@reseteveryparhanging}%
}
4
  • This is great, thanks a lot! Can you briefly comment on how you come up with such an answer, e.g., what's the solution process? You look inside the source code of the package and search for \SetKwInOut and then attempt to figure out what's going on? And that call to \xpatchcmd does something like search & replace in the command definition?
    – Eike P.
    Commented Jul 29, 2020 at 11:32
  • 1
    @jhin Firstly I searched for \Input and input in algorithms2e.sty, then I found \algocf@inoutbox and \algocf@inputbox. This led me to the definition of \SetKwInOut and \SetKwInput, which shows how the hang-indent of keyword is implemented. Combining with the \SetKwInOut{Input}{Input} you used and with some attempts, I got the final solution. And yes, \xpatchcmd, provided by xpatch package, does something like search & replace. It saves space if the full definition is long. Commented Jul 29, 2020 at 12:16
  • Thanks, that's very helpful! Maybe, at some point, I'll be able to come up with these things myself...
    – Eike P.
    Commented Jul 29, 2020 at 13:01
  • @jhin Actually, I should have noticed \SetKwInOut{Input}{Input} before searching in algorithms2e.sty. Commented Jul 29, 2020 at 13:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .