# How do I get rid of that extra line at the end of my custom algorithmicx block?

I'm using the algorithmicx package and have created a custom block called \On, but even with the noend option for algpseudocode I get an extra blank line at the end of the block.

Here is a complete example:

\documentclass[a4paper]{report}

\usepackage{algorithmicx}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

% Defines custom \On ... \EndOn block
\algblockdefx[ON]{On}{EndOn}
[2]{\textbf{on}~#1~\textbf{from}~#2~\textbf{do}}

\begin{document}
\begin{algorithm}
\begin{algorithmic}
\On{$foo$}{$bar$}
\State baz
\EndOn
\end{algorithmic}
\end{algorithm}
\end{document}


This is the result:

As you can see, I get an extra blank line. The predefined blocks like \If and \For, however, work as expected with the noend option.

I've read the documentation several times, but am still clueless in how I would fix this.

I'm using pdflatex from MacTeX (TeX Live) 2013.

I recently faced the same problem, but I came up with the following workaround after looking into the algpseudocode source code:

% Tell algorithmicx not to print an empty line if noend' option is set
\makeatletter
\ifthenelse{\equal{\ALG@noend}{t}}%
{\algtext*{EndOn}}
{}%
\makeatother


The need for such a fix for each user-defined block can be considered a bug in algpseudocode, in my opinion. Perhaps you should drop the maintainer an email...

Edit: In case you decide not to use the noend option, the following definition for your "On" block probably makes more sense.

\algnewcommand\algorithmicon{\textbf{on}}
\algnewcommand\algorithmicfrom{\textbf{from}}
\algblockdefx[ON]{On}{EndOn}[2]
{\algorithmicon\ #1\ \algorithmicfrom\ #2\ \algorithmicdo}
{\algorithmicend\ \algorithmicon}


## Complete code

\documentclass[a4paper]{report}

\usepackage{algorithmicx}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

% Defines custom \On ... \EndOn block
\algnewcommand\algorithmicon{\textbf{on}}
\algnewcommand\algorithmicfrom{\textbf{from}}
\algblockdefx[ON]{On}{EndOn}[2]
{\algorithmicon\ #1\ \algorithmicfrom\ #2\ \algorithmicdo}
{\algorithmicend\ \algorithmicon}

% Tells algorithmicx not to print an empty line if noend' is set
\makeatletter
\ifthenelse{\equal{\ALG@noend}{t}}%
{\algtext*{EndOn}}
{}%
\makeatother

\begin{document}
\begin{algorithm}
\begin{algorithmic}
\On{$foo$}{$bar$}
\State baz
\EndOn
\end{algorithmic}
\end{algorithm}
\end{document}

• I defer to your answer and have deleted my own. – Steven B. Segletes Apr 17 '14 at 16:46
• Thank you so much! (and thanks for Steven as well) I believe this is the best we can do given the state of the package code. – csl Apr 17 '14 at 16:56

Try this

\algblockdefx[ON]{On}{EndOn}
[2]{\textbf{on}~#1~\textbf{from}~#2~\textbf{do}}
{\vspace{-\baselineskip}}


Just strech the vertical space to 0

For example

\documentclass{article}
\usepackage{algpseudocode}
\begin{document}
%case block
\algblockdefx[caseBlock]{case}{endCase}
[1]{\textbf{case} #1 \textbf{of}}
{\textbf{end case}}
%
%caseToken block
\algblockdefx[caseTokenBlock]{caseToken}{endCaseToken}
[1]{\textbf{#1: }}
{\vspace{-\baselineskip}}
%
%noEnd block
\algblockdefx[noEndBlock]{noEnd}{noEndEnd}
[1]{#1}
{\vspace{-\baselineskip}}
%

\begin{algorithmic}[0]
\Procedure{\textit{factor}}{}
\noEnd{\textbf{Begin}}
\case{token}
\caseToken{(}
\State match(\textbf{(})
\State exp
\State match(\textbf{)})
\endCaseToken
\caseToken{\textit{number}}
\State match(\textit{\textbf{number}})
\endCaseToken
\noEnd{\textbf{else: } \textit{error}}
\noEndEnd
\endCase
\noEndEnd
\EndProcedure
\end{algorithmic}
\end{document}


• Welcome to TeX.SE. Can you put a complete example with a screenshot to see your solution? Thank you very much. – Sebastiano Mar 16 at 11:53