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 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:

Custom ON-block in algorithmicx package

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.

share|improve this question

1 Answer 1

up vote 6 down vote accepted

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

enter image description here

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}

enter image description here

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}
share|improve this answer
2  
I defer to your answer and have deleted my own. –  Steven B. Segletes Apr 17 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 at 16:56

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.