Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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!

share|improve this question
1  
Replace \Statex with \medskip, for instance. –  Bernard Feb 12 at 10:22
    
@Bernard That worked, thank you! –  Driu Feb 12 at 10:24

1 Answer 1

up vote 5 down vote accepted

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.