パッケージ algorithm2e の改行

パッケージ algorithm2e の改行

私はアルゴリズム2e擬似コード用のパッケージ。折り返す必要のある非常に長い行がいくつかありました。次の(改行された)行をインデントしたり、最初の行にマークを付けて改行が発生したことを示す方法はありますか(リスティングパッケージのソリューション

似たようなものを見つけた質問リスティングパッケージについては、以前に回答しましたが、私は明確にアルゴリズム2e解決。

答え1

次の最小限の例では、

  • \nosemic: 行末文字 (EOL) の印刷を削除します (通常は;)。
  • \dosemic: EOL 文字の印刷を復元します。
  • \pushline: 行をインデントします1em(通常のインデント)。 によって提供されalgorithm2eます\Indp
  • \popline: インデントを削除\pushline(インデント解除?); algorithm2easによって提供される\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

ノート:

  1. \skiptextは、新しいブロックのアルゴリズムにおけるテキストインデントの長さです。したがって、このインデントの 1/2 を選択します。

  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テストしました。

関連情報