
私はアルゴリズム2e擬似コード用のパッケージ。折り返す必要のある非常に長い行がいくつかありました。次の(改行された)行をインデントしたり、最初の行にマークを付けて改行が発生したことを示す方法はありますか(リスティングパッケージのソリューション)
似たようなものを見つけた質問リスティングパッケージについては、以前に回答しましたが、私は明確にアルゴリズム2e解決。
答え1
次の最小限の例では、
\nosemic
: 行末文字 (EOL) の印刷を削除します (通常は;
)。\dosemic
: EOL 文字の印刷を復元します。\pushline
: 行をインデントします1em
(通常のインデント)。 によって提供されalgorithm2e
ます\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
セットアップでは、4 つのコマンドをどのように組み合わせるかわかりません。そのため、最大限に (ただし手動で) 使用できるように、これらを別々に保持しています。
答え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
テストしました。