3

I am trying to replicate the style of the following pseudocode. Original pseudocode

The following minimal working example almost produces what I want except the extra vertical space between lines 7 and 8. How can I resolve this issue? Is there an easier way to format pseudocodes this way?

\documentclass[12pt]{article}

\usepackage[noend]{algpseudocode}

\setlength\parindent{0pt}
\newcommand{\Def}[3]{\textbf{#1}\ \textsc{#2}\ifthenelse{\equal{#3}{}}{}{(#3)}}
\algrenewcommand{\alglinenumber}[1]{\footnotesize#1\hspace{1em}}% <- This line

\begin{document}

    \Def{procedure}{Euclidian}{$a,b$}
    \begin{algorithmic}[1]
        \State $x \gets a$
        \State $y \gets b$
        \While{$x \not= y$}
            \If{$x < y$}
                \State $x \gets x - y$
            \Else
                \State $y \gets y - x$
            \EndIf
        \EndWhile
        \State \textbf{return} $x$
    \end{algorithmic}

\end{document}

Edit I have tried clrscode3e this time. It worked, but required me to hack the package code. I still wonder if there is an easier solution.

\documentclass[12pt]{article}

\usepackage{clrscode3e}

\setlength\parindent{0pt}

% Restore \gets
\let\gets=\leftarrow

% Print 'do' and 'then'
\renewcommand{\Do}{\textbf{do}\addtocounter{indent}{1}}
\renewcommand{\Then}{\textbf{then}\addtocounter{indent}{1}}

% Indent recursively (\hspace is used instead of \>)
\renewcommand{\putindents}{\ifnum\value{thisindent}>0%
    \hspace{1.5em}\addtocounter{thisindent}{-1}\putindents%
\fi}

% Line number separation (hspace is added)
\makeatletter
\renewcommand{\liprint}{\protected@xdef\@lilabel{\thecodelinenumber}%
    \ifnumberedline\thecodelinenumber\fi\'\hspace{0.25em}\Indent%
}
\makeatother

\begin{document}

    \begin{codebox}
    \Procname{\textbf{procedure} $\proc{Euclidian}(a,b)$}
    \li $x \gets a$
    \li $y \gets b$
    \li \While $x \not= y$ \Do
    \li     \If $x < y$ \Then
    \li         $x \gets x - y$
    \li     \Else
    \li         $y \gets y - x$
            \End
        \End
    \li \Return $x$
    \end{codebox}   

\end{document}

Edit 2 The patch proposed by @egreg fixes vertical alignment, but I have one more issue. In the first MWE, the marked line left-aligns the line numbers by increasing the separation. However, it cannot handle multidigit line numbers. What are your suggestions?

Burak Gök
  • 133
  • 4

1 Answers1

2

While Spurious whitespace with algpseudocode and noend corrects the vertical alignment, the horizontal alignment of the line numbering can be changed using a fixed-width box:

enter image description here

\documentclass{article}

\usepackage[noend]{algpseudocode}

\usepackage{etoolbox}
\makeatletter
\patchcmd{\ALG@doentity}{\item[]\nointerlineskip}{}{}{}
\makeatother

\setlength{\parindent}{0pt}
\newcommand{\Def}[3]{\textbf{#1}\ \textsc{#2}\ifthenelse{\equal{#3}{}}{}{(#3)}}
\algrenewcommand{\alglinenumber}[1]{\makebox[1.5em][l]{\footnotesize#1}}
\algrenewcommand{\Return}[1]{\State \textbf{return} #1}

\makeatletter
\newcommand{\@procedure}[1][\relax]{%
  \def\procedurearg{#1}%
  \textbf{procedure}
  \textsc{\procedurename}%
  \ifx\procedurearg\relax\else
    (#1)%
  \fi
  \par
  \begin{algorithmic}[1]
}
\newenvironment{procedure}[1]
  {\par
   \setlength{\parindent}{0pt}% Remove paragraph indent
   \def\procedurename{#1}%
   \@procedure
  }
  {\end{algorithmic}}
\makeatother

\begin{document}

\begin{procedure}{Euclidian}[$a,b$]
  \State $x \gets a$
  \State $y \gets b$
  \While{$x \not= y$}
    \If{$x < y$}
      \State $x \gets x - y$
    \Else
      \State $y \gets y - x$
    \EndIf
  \EndWhile
  \Return{$x$}
\end{procedure}

\end{document}

For convenience, I've wrapped the procedure-algorithmic mix into a single procedure environment.

Werner
  • 603,163