使用 algorithm2e usepackage 缩进块

使用 algorithm2e usepackage 缩进块

我怎样才能在这行之后缩进伪代码块function Karatsuba(p,q)

\documentclass{article}
\usepackage{german,t1enc}
\usepackage{lipsum}
\usepackage{fancyhdr}
\pagestyle{fancy}

\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

%pseudocode.
\usepackage[linesnumbered,ruled,vlined]{algorithm2e} 

%Color
\usepackage[dvipsnames]{xcolor}
\renewcommand{\baselinestretch}{1.5}
%blacksquare
\usepackage{amssymb }
\begin{document}

\begin{algorithm}[H]
\SetAlgoLined
function Karatsuba(p,q) \\
  $n \leftarrow  degree(p) +1 $ \\
  \eIf{$n = 1$}{
   return $p.q$  
  }{
   $p(x) = p_1 * x^{\frac{n}{2}} + p_0$ \\
$ q(x) = q_1 * x^{\frac{n}{2}} + q_0$ \\
$p.q = p_1q_1.(x)^n + ( (p_0+p_1)(q_0+q_1) - p_0q_0 - p_1q_1). (x)^\frac{^n}{2} + p_0q_0$
  }

\caption{}
\label{alg: initializer}
\end{algorithm}
\end{document}

答案1

一种方法是使用algorithm2e命令\SetKwBlock{}{}{}(CTAN 手册第 11.2 节)如下:

\documentclass{article}

\usepackage[linesnumbered,ruled,vlined]{algorithm2e} 
\renewcommand{\baselinestretch}{1.5}

\begin{document}

\begin{algorithm}[H]
\SetKwBlock{Func}{function Karatsuba(p,q)}{}
\SetAlgoLined
\Func{
  $n \leftarrow  degree(p) +1 $ \\
  \eIf{$n = 1$}{
   return $p.q$  
  }{
   $p(x) = p_1 * x^{\frac{n}{2}} + p_0$ \\
$ q(x) = q_1 * x^{\frac{n}{2}} + q_0$ \\
$p.q = p_1q_1.(x)^n + ( (p_0+p_1)(q_0+q_1) - p_0q_0 - p_1q_1). (x)^\frac{^n}{2} + p_0q_0$
  }
}
\caption{}
\label{alg: initializer}
\end{algorithm}
\end{document}

结果是:

在此处输入图片描述

相关内容