algorithm2e 完全合理的输入/输出块

algorithm2e 完全合理的输入/输出块

我想完全证明环境中的输入/输出块algorithm2e。如何实现?

我在文档中找不到有关此事的任何信息。确切地说,我想保持缩进不变,只是让文本块完全对齐。(我尝试过只使用ragged2e并简单地将其放在\justify输入内容的开头,但这样做会产生奇怪的效果。)

MWE(忽略算法):

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

在此处输入图片描述

答案1

尝试这个:

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

在此处输入图片描述

一些解释:

调用时\SetKwInOut{Input}{Input},它定义\Input

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

相关内容