11

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.

9

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}
| improve this answer | |
  • 2
    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
0

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}

no ending

| improve this answer | |
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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