我正在尝试为下面算法顶部和底部出现的 3 条水平线着色。我在网上找不到类似的问题。您能提供建议吗?
\documentclass[11pt,a4paper]{article}
\usepackage[no-math]{fontspec}
\usepackage[dvipsnames,svgnames,table,fixpdftex]{xcolor}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[ruled,linesnumbered]{algorithm2e}
\begin{document}
\begin{algorithm}
\SetArgSty{textnormal}
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\DontPrintSemicolon
\Input{A point $P$, an $n$-bit integer $k=\displaystyle\sum_{i=0}^{n-1} k_{(i)}2^i$}
\Output{$Q=[k]P$}
\Begin{
$Q \longleftarrow \mathcal{O}$\;
\For{$i=n-1$ to $0$}
{
$Q \longleftarrow [2]Q$\;
\If{$k_{(i)}=1$}{
$Q \longleftarrow Q+P$
}
}
\Return{$Q$}
}
\caption{Binary method for EC point multiplication} \label{alg1}
\end{algorithm}
\end{document}
答案1
以下设置允许您通过提供的宏调整这些颜色以满足您的需要:
\setalgotoprulecolor{<colour>}
\setalgomidrulecolor{<colour>}
\setalgobotrulecolor{<colour>}
\documentclass{article}
\usepackage[dvipsnames,svgnames,table]{xcolor}
\usepackage{amsmath,amssymb}
\usepackage[ruled,linesnumbered]{algorithm2e}
% algorithm2e settings
\SetArgSty{textnormal}
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\DontPrintSemicolon
\newcommand{\assign}{\longleftarrow}
\makeatletter
\newcommand{\setalgotoprulecolor}[1]{\colorlet{toprulecolor}{#1}}
\let\old@algocf@pre@ruled\@algocf@pre@ruled % Adjust top rule colour
\renewcommand{\@algocf@pre@ruled}{\textcolor{toprulecolor}{\old@algocf@pre@ruled}}
\newcommand{\setalgobotrulecolor}[1]{\colorlet{bottomrulecolor}{#1}}
\let\old@algocf@post@ruled\@algocf@post@ruled % Adjust middle rule colour
\renewcommand{\@algocf@post@ruled}{\textcolor{bottomrulecolor}{\old@algocf@post@ruled}}
\newcommand{\setalgomidrulecolor}[1]{\colorlet{midrulecolor}{#1}}
\renewcommand{\algocf@caption@ruled}{%
\box\algocf@capbox{\color{midrulecolor}\kern\interspacetitleruled\hrule
width\algocf@ruledwidth height\algotitleheightrule depth0pt\kern\interspacealgoruled}}
\makeatother
\setalgotoprulecolor{blue!30}% Default
\setalgobotrulecolor{red!30}% Default
\setalgomidrulecolor{green!30}% Default
\begin{document}
\begin{algorithm}
\Input{A point $P$, an $n$-bit integer $k = \sum_{i=0}^{n-1} k_{(i)}2^i$}
\Output{$Q = [k]P$}
\Begin{
$Q \assign \mathcal{O}$\;
\For{$i = n-1$ to $0$}
{
$Q \assign [2]Q$\;
\If{$k_{(i)} = 1$}{
$Q \assign Q + P$
}
}
\Return{$Q$}
}
\caption{Binary method for EC point multiplication}
\end{algorithm}
\end{document}
上面的代码特定ruled
于algorithm2e
。