當斷行時調整演算法包內的縮排

當斷行時調整演算法包內的縮排

我有這個代碼

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}
   \caption{Minimal Working Example for my Problem}
\begin{algorithmic}[1]
   \While{Indentation is a mess}
      \State Examine a very long line that looks horrible because the indentation is all messed up.
   \EndWhile
\end{algorithmic}
\end{algorithm}
\end{document}​

我希望將斷開的文字縮排到語句開始的同一列。

答案1

將長行包裹在top-aligned中\parbox

在此輸入影像描述

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\begin{document}
\begin{algorithm}
  \caption{Minimal Working Example for my Problem}
  \begin{algorithmic}[1]
    \While{Indentation is a mess}
      \State \parbox[t]{\dimexpr\linewidth-\algorithmicindent}{Examine a very long line that looks horrible 
        because the indentation is all messed up.\strut}
    \EndWhile
  \end{algorithmic}
\end{algorithm}
\end{document}​

當前縮排 ( \algorithmicindent) 被刪除,\linewidth以恰好適合水平線寬度。\strut在末尾添加 a可以\State在沒有下降部分的行中的行(或 s)之間進行正確的垂直對齊。

相關內容