[Tex/LaTex] Customizing some aspect of algorithmic environment

algorithmicxalgorithms

I'm using the algorithmic environment for typesetting algorithm in my document, but I'd like to customize some aspect and fix problems.

I'm using the algpseudocode package with noend option, but it produces an ugly vertical gap when the end lines are suppressed (see picture below between the sixth and the seventh lines). How can I fix this problem?

Second, I'm using function command to produce two distinct procedures in the same algorithm environment, but I'd like to have only the function body to be line-numbered and the line count should restart from 1 for the Reduce function.

Third and last, I've separated the two functions using the \Statex command, but I'd like to have a less vertical space width than a whole white line.

This is my code.

\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
        \Statex
        \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}

enter image description here

Thank you!

Best Answer

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:

enter image description here

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:

enter image description here