TeX - LaTeX Asked on September 29, 2021
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}
Try this:
documentclass{article}
usepackage[algoruled, linesnumbered]{algorithm2e}
usepackage{ragged2e}
usepackage{xpatch}
makeatletter
xpatchcmdSetKwInOut
{hangafter=1parbox[t]}
{hangafter=1justifyparbox[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}
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.
{letalgocf@newinouthangindent=inoutindenthangafter=1parbox[t]{inoutsize}{KwSty{#2algocf@typohfill:}}~##1par}%
algocf@linesnumbered% reset the numbering of the lines
ifthenelse{boolean{algocf@hanginginout}}{relax}{algocf@reseteveryparhanging}%
}
Correct answer by muzimuzhi Z on September 29, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP