如何修改算法/algpseudocode 中的行距?

如何修改算法/algpseudocode 中的行距?

我想增加algorithm环境中语句之间的行距。我使用该setspace包作为这里,但也会影响换行符之间的不想要的空格。以下是示例代码

\begin{algorithm}
\begin{algorithmic}[1]
\State lines are long so I get line breaks. I want to increase space between statements, but not between different lines of the same statement.     
\State lines are long so I get line breaks. I want to increase space between statements,  but not between different lines of the same statement.
\end{algorithmic}
\end{algorithm}

在序言中我已经说过:

\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\algrenewcommand\alglinenumber[1]{{\sf\footnotesize#1}}
\usepackage{setspace}
\let\Algorithm\algorithm
\renewcommand\algorithm[1][]{\Algorithm[#1]\setstretch{1.2}}

答案1

这是一种可能性;在内部,algorithmic使用\list,因此我们可以使用该xpatch包来修补此列表并添加所需的值\itemsep(在我使用的示例中2ex plus2pt):

\documentclass[a4paper,10pt]{report} 
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{xpatch}

\algrenewcommand\alglinenumber[1]{{\sffamily\footnotesize#1}}

\makeatletter
\xpatchcmd{\algorithmic}{\itemsep\z@}{\itemsep=2ex plus2pt}{}{}
\makeatother

\begin{document} 
\begin{algorithm}
\begin{algorithmic}[1]
\State lines are long so I get line breaks. I want to increase space between statements, but not between different lines of the same statement.     
\State lines are long so I get line breaks. I want to increase space between statements,  but not between different lines of the same statement.
\end{algorithmic}
\end{algorithm}

\end{document}

在此处输入图片描述

修补也可以借助以下方法etoolbox完成xpatch

\usepackage{etoolbox}

\makeatletter
\expandafter\patchcmd\csname\string\algorithmic\endcsname{\itemsep\z@}{\itemsep=2ex plus2pt}{}{}
\makeatother

相关内容