To restart line numbering from 1, you have to reset the counter ALG@line
, so we define a new command \resetline
to be issued when you want that change:
\makeatletter
\newcommand{\resetline}{\setcounter{ALG@line}{0}}
\makeatother
In regards of the spacing between two functions, you can use a normal skip instead of issuing a \Statex
(\smallskip
, \medskip
, \bigskip
).
MWE
\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\makeatletter
\newcommand{\resetline}{\setcounter{ALG@line}{0}}
\makeatother
\begin{document}
\begin{algorithm}[H]
\caption{Calcola il grado di ogni vertice del grafo $G = (V,E)$}
\label{degree}
\begin{algorithmic}[1]
\Function{\textsc{Map}$( \left \langle u;v \right \rangle )$}{}
\State \textbf{emit} $\left \langle u;v \right \rangle$
\EndFunction
\medskip\resetline
\Function{\textsc{Reduce}$( \left \langle u;\Gamma(u) \right \rangle )$}{}
\State $d(u) \gets 0$
\ForAll{$v \in \Gamma(u)$}
\State $d(u) \gets d(u) + 1$
\EndFor
\State \textbf{emit} $\left \langle u;d(u) \right \rangle$
\EndFunction
\end{algorithmic}
\end{algorithm}
\end{document}
Output:
The little gap related to the noend
option is to indicate the end of the function, so I'll leave it as is, but if you really want to remove it, you can add these lines in your preamble (require the etoolbox
package):
\patchcmd{\ALG@doentity}
{\item[]\nointerlineskip}
{\relax}
{}
{}
Complete code:
\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{etoolbox}
\makeatletter
\newcommand{\resetline}{\setcounter{ALG@line}{0}}
\patchcmd{\ALG@doentity}
{\item[]\nointerlineskip}
{\relax}
{}
{}
\makeatother
\begin{document}
\begin{algorithm}[H]
\caption{Calcola il grado di ogni vertice del grafo $G = (V,E)$}
\label{degree}
\begin{algorithmic}[1]
\Function{\textsc{Map}$( \left \langle u;v \right \rangle )$}{}
\State \textbf{emit} $\left \langle u;v \right \rangle$
\EndFunction
\medskip\resetline
\Function{\textsc{Reduce}$( \left \langle u;\Gamma(u) \right \rangle )$}{}
\State $d(u) \gets 0$
\ForAll{$v \in \Gamma(u)$}
\State $d(u) \gets d(u) + 1$
\EndFor
\State \textbf{emit} $\left \langle u;d(u) \right \rangle$
\EndFunction
\end{algorithmic}
\end{algorithm}
\end{document}
Output:
You need to define a command with an argument giving the font specification and then use the name of this command in the argument for \SetCommentSty
:
\documentclass{article}
\usepackage{xcolor}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\newcommand\mycommfont[1]{\footnotesize\ttfamily\textcolor{blue}{#1}}
\SetCommentSty{mycommfont}
\begin{document}
\begin{algorithm}[H]
\DontPrintSemicolon
\KwData{Training set $x$}
$\Delta_{ji}^l := 0$ \tcp*{will be used to compute $\partial x$}
\tcc{iterate over all training examples}
\caption{Example code}
\end{algorithm}
\end{document}
Best Answer
Use the command
\SetNoFillComment
locally inside analgorithm
environment or use the global optionnofillcomment
when loadingalgorithm2e
.MWE (borrowed from the linked answer):
Output: