# Customizing some aspect of algorithmic environment

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}


Thank you!

-
Replace \Statex with \medskip, for instance. – Bernard Feb 12 '14 at 10:22
@Bernard That worked, thank you! – Driu Feb 12 '14 at 10:24

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:

-
Thank you for the comprehensive answer! Since we both come from Italy, I appreciate it even much! ;) – Driu Feb 12 '14 at 12:40
@Driu Happy to help. And happy to discover that one more Italian person is over here. Ciao :-) – karlkoeller Feb 12 '14 at 13:14