Latex 算法 Algorithm 一些使用总结 (基本用法,步骤标号,某一句加颜色)。

基本用法

步骤1:导入需要的算法包。

\usepackage[ruled,vlined]{algorithm2e}

步骤2:具体算法。

\begin{algorithm}[t]
	\caption{Framework of Meta-GNN.}
	\label{alg:algorithm1}
	\KwIn{Distribution over mete-training tasks: $p(\mathcal{T})$; Meta-testing tasks: $\mathcal{T}_{mt}$; Task-learning rate: $\alpha_{1}$; Meta-learning rate: $\alpha_{2}$.}
	\KwOut{Labels of nodes in query set of $\mathcal{T}_{mt}$.}  
	\BlankLine
	Initialize $\bm{\theta}$ randomly;
	
	\While{\textnormal{not converged}}{
		Sample batch of meta-training tasks $\mathcal{T}_{i} \sim p(\mathcal{T})$;
		
		\ForEach{task in $\mathcal{T}_{i}$}{
			Evaluate $\mathcal{L}_{\mathcal{T}_{i}}\left(f_{\bm{\theta}}\right)$ using $\mathcal{S}_{i}$;
			
			Compute adapted parameters $\bm{{\theta}^{\prime}_{i}}$;
			
			Evaluate $\mathcal{L}_{\mathcal{T}_{i}}\left(f_{\bm{{\theta}_{i}^{\prime}}}\right)$ using $\mathcal{Q}_{i}$;
		}
		Update $\bm{\theta}$ by; 
	}
	
	Compute adapted parameters $\bm{{\theta}^{\prime}_{mt}}$ using support set of $\mathcal{T}_{mt}$; 
	
	Predict labels of nodes in query set of $\mathcal{T}_{mt}$ using model $f_{\bm{{\theta}_{mt}^{\prime}}}$.
	

\end{algorithm}

最后的效果图:
Latex 算法 Algorithm 一些使用总结 (基本用法,步骤标号,某一句加颜色)。_第1张图片

算法的标题

\caption{Framework of Meta-GNN.}

算法的标签,方便在文章中引用,例如

\label{alg:algorithm1}
Algorithm~\ref{alg:algorithm1}

算法的输入

\KwIn{Distribution over mete-training tasks: $p(\mathcal{T})$; Meta-testing tasks: $\mathcal{T}_{mt}$; Task-learning rate: $\alpha_{1}$; Meta-learning rate: $\alpha_{2}$.}

算法的输出。

\KwOut{Labels of nodes in query set of $\mathcal{T}_{mt}$.}  

备注:

(1).如果想要给算法的步骤加上序号:

这样引入包:

\usepackage[ruled,linesnumbered]{algorithm2e}

效果如下:
Latex 算法 Algorithm 一些使用总结 (基本用法,步骤标号,某一句加颜色)。_第2张图片

(2). 某些步骤加颜色:

首先引入包:

\usepackage{color}

然后修改其中一句

{\color{red}Evaluate $\mathcal{L}_{\mathcal{T}_{i}}\left(f_{\bm{{\theta}_{i}^{\prime}}}\right)$ using $\mathcal{Q}_{i}$;}

实际效果:
Latex 算法 Algorithm 一些使用总结 (基本用法,步骤标号,某一句加颜色)。_第3张图片

你可能感兴趣的:(Latex)