
答案1
以下最小範例定義
\nosemic
:刪除行尾 (EOL) 字元的列印(通常為;
);\dosemic
:恢復 EOL 字元的列印;\pushline
:縮排一行1em
(典型縮排);由algorithm2e
as提供\Indp
;\popline
:刪除縮排\pushline
(取消縮排?);由algorithm2e
as提供\Indm
\documentclass{article}
\usepackage{algorithm2e}% http://ctan.org/pkg/algorithm2e
\makeatletter
\newcommand{\nosemic}{\renewcommand{\@endalgocfline}{\relax}}% Drop semi-colon ;
\newcommand{\dosemic}{\renewcommand{\@endalgocfline}{\algocf@endline}}% Reinstate semi-colon ;
\newcommand{\pushline}{\Indp}% Indent
\newcommand{\popline}{\Indm\dosemic}% Undent
\makeatother
\begin{document}
\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
\nosemic this is a very long statement that has to be\;
\pushline\dosemic wrapped over two lines\;
\popline current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}
\end{document}
因為我對你不熟悉algorithm2e
設置,我不確定您希望如何組合這四個命令。因此,我將它們分開以最大限度地(但手動)使用。
答案2
事實上,有一個更簡單的解決方案:使用經典的\hangafter
和\hangindent
。這與algorithm2e
.
筆記:
\skiptext
是演算法中新區塊的文字縮排長度;所以我選擇這個縮排的1/2;無需重新定義內部宏,因為
\algocf@endline
幾乎所有宏都可以在algorithm2e
.用於\SetEndCharOfAlgoLine
重新定義行尾
\hangafter
請參閱下面的使用和的範例\hangindent
:
\documentclass{article}
\usepackage{algorithm2e}% http://ctan.org/pkg/algorithm2e
%
\newcommand{\nosemic}{\SetEndCharOfAlgoLine{\relax}}% Drop semi-colon ;
\newcommand{\dosemic}{\SetEndCharOfAlgoLine{\string;}}% Reinstate
\newcommand{\pushline}{\Indp}% Indent
\newcommand{\popline}{\Indm\dosemic}% Undent
%
\begin{document}
\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
\nosemic this is a very long statement that has to be\;
\pushline\dosemic wrapped over two lines\;
\popline current section becomes this one\;
\hangindent=.5\skiptext\hangafter=1
this is a very long statement that should be indented automatically on the next
lines if this statement is longer than one line\;
Following lines should be indented normally\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}
\end{document}
答案3
\Indp
並可\Indm
在套件中用於algorithm2e
縮排和刪除縮排。我已經測試過了。