我是 Latex 和 Latex 的新手Algorithm2e
。我试图在一个文档中定义多个函数。到目前为止,我有以下信息:
\documentclass{article}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\begin{document}
\begin{algorithm}[!ht]
\DontPrintSemicolon
{
\KwIn{tags per post ($T_i,_p$) within a given cell ($C$) for the entire grid ($G$) }
\KwOut{set of weighted tags within given cell (\^T$_c$) calculated across the entire grid (G)}
}
\SetKwFunction{FMain}{tfidf}
\SetKwProg{Fn}{Function}{:}{}
\Fn{\FMain{$T_i,_p$, $C$, $G$}}
{
\ForAll{$C$ within G}
{
\ForEach{($T_i,_p$) within C}{
$TermFrequency$\;
}
{
\ForEach{tag($t$) across $G$}{
$InverseDocumentFrequency$\;
}
\ForEach{($T_i,_p$) within $C$ for $G$}{
\^T$_c$ $\leftarrow$TF-IDF($t$, $C$,$G$)
}
}
}
{return \^T$_c$\;}
}
\caption{Calculation of TF-IDF}
\end{algorithm}
\begin{algorithm}[!ht]
\DontPrintSemicolon
{
\KwIn{spatially normalised tags in cell $(V_1)$, spatially normalised tags in cell $(V_2)$}
\KwOut{cosine similarity measure ($C_s$) for $V_1$, $V_2$}
}
\SetKwFunction{FTest}{cosine_similarity}
\SetKwProg{FTest}{Function}{:}{}
\FTest{\FTest{$V_1$, $V_2$}}
{
\For{$V_1$, $V_2$}{
transform($V_1$, $V_2$) \tcp*[f]{Transform vectors to same magnitude}}
$C_s$$\leftarrow$ cos($V_1$, $V_2$) = $V_1$ . $V_2$ / ||$V_1$|| * ||$V_2$||
{return $C_s$\;}
}
\caption{Calculation of Semantic Similarity (Cosine-based algorithm)}
\end{algorithm}
\end{document}
它打印出以下内容。第一个函数很好,但是语法在第二个算法/函数中没有达到我想要的效果。我该如何定义算法以便将其写入"Function Cosine_similarity(V1, V2):"
第 1 行?
答案1
你有
\FTest{\FTest{$V_1$, $V_2$}}
应该是
\Ftest{$V_1,V_2$}
我做了几处更改,请注意。
\documentclass{article}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\begin{document}
\begin{algorithm}[!ht]
\DontPrintSemicolon
\KwIn{tags per post ($T_i,_p$) within a given cell ($C$) for the entire grid ($G$) }
\KwOut{set of weighted tags within given cell ($\hat{T}_c$) calculated across the entire grid ($G$)}
\SetKwFunction{FMain}{tfidf}
\SetKwProg{Fn}{Function}{:}{}
\Fn{\FMain{$T_{i,p}$, $C$, $G$}}
{
\ForAll{$C$ within $G$}
{
\ForEach{$(T_{i,p})$ within $C$}
{
TermFrequency\;
}
{
\ForEach{tag$(t)$ across $G$}
{
InverseDocumentFrequency\;
}
\ForEach{$(T_{i,p})$ within $C$ for $G$}
{
$\hat{T}_c\leftarrow\FMain{$t, C,G$}$
}
}
}
{
return $\hat{T}_c$\;
}
}
\caption{Calculation of TF-IDF}
\end{algorithm}
\begin{algorithm}[!ht]
\DontPrintSemicolon
\KwIn{spatially normalised tags in cell $(V_1)$, spatially normalised tags in cell $(V_2)$}
\KwOut{cosine similarity measure ($C_s$) for $V_1$, $V_2$}
\SetKwFunction{FTest}{cosine_similarity}
\SetKwProg{FTest}{Function}{:}{}
\FTest{$V_1,V_2$}
{
\For{$V_1$, $V_2$}
{
transform($V_1$, $V_2$) \tcp*[f]{Transform vectors to same magnitude}
}
$C_s\leftarrow \cos(V_1, V_2) = V_1 \bullet V_2 / (\|V_1\|\cdot\|V_2\|)$
{
return $C_s$\;
}
}
\caption{Calculation of Semantic Similarity (Cosine-based algorithm)}
\end{algorithm}
\end{document}