6

I am formatting an article with one of the ACM journal formats: acmsmall. However, when I look at algorithms, I noticed that the lines of my algorithm lost their indentation.

This problem does not occur in article class.

I have prepared a minimal example to demostrate the issue.

\documentclass{acmsmall}
%\documentclass{article}

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

\begin{document}
    \begin{algorithm}
        \caption{MyAlgorithm}
        \begin{algorithmic}[1]
            \State $A \gets X$
            \State $A \gets Y$
            \State $A \gets Z$
            \State $A \gets X$
            \State $A \gets Y$
            \State $A \gets Z$
            \State $A \gets X$
            \State $A \gets Y$
            \State $A \gets Z$
            \State $A \gets X$
            \State $A \gets Y$
            \State $A \gets Z$
        \end{algorithmic}
    \end{algorithm}
\end{document}

I have attached the screenshots of both acmsmall and article. As you can see, after line 10, the indentation of acmsmall is broken.

How can I fix the problem?

acmsmall

article

1 Answer 1

5

The acmsmall class is causing the numbers to be centered rather than right-aligned. You can correct this by redefining \alglinenumber as follows (updated to get fontsize):

\newcommand{\myalgnumfont}{\fontsize{8pt}{9pt}\selectfont}
\algrenewcommand{\alglinenumber}[1]{\myalgnumfont #1:}

in the preamble.

Sample output

\documentclass{acmsmall}
%\documentclass{article}

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

\newcommand{\myalgnumfont}{\fontsize{8pt}{9pt}\selectfont}
\algrenewcommand{\alglinenumber}[1]{\hss\myalgnumfont #1:}

\begin{document}
    \begin{algorithm}
        \caption{MyAlgorithm}
        \begin{algorithmic}[1]
            \State $A \gets X$
            \State $A \gets Y$
            \State $A \gets Z$
            \State $A \gets X$
            \State $A \gets Y$
            \State $A \gets Z$
            \State $A \gets X$
            \State $A \gets Y$
            \State $A \gets Z$
            \State $A \gets X$
            \State $A \gets Y$
            \State $A \gets Z$
        \end{algorithmic}
    \end{algorithm}
\end{document}

The standard algorithmic definition use \footnotesize to set the fontsize. The problem is that acmsmall redefines the \footnotesize to

\newcommand{\footnotesize}{%
    \@setfontsize\footnotesize\@viiipt{9\p@}%
    \abovedisplayskip 4\p@ \@plus \p@
    \belowdisplayskip \abovedisplayskip
    \abovedisplayshortskip 4\p@ \@minus \p@
    \belowdisplayshortskip 4\p@ \@minus \p@
    \def\@listi{%
        \leftmargin\leftmargini
        \topsep 4\p@ \@plus \p@
        \parsep \z@ \@plus .5\p@
        \itemsep \p@ \@plus .7\p@}
}%

The problem here is the final \itemsep which in size10.clo loaded by article.cls is

   \def\@listi{\leftmargin\leftmargini
               \topsep 3\p@ \@plus\p@ \@minus\p@
               \parsep 2\p@ \@plus\p@ \@minus\p@
               \itemsep \parsep}%

In particular acmsmall is adding some glue to the end of \itemsep, which then affects the label alignment.

6
  • Do you agree that the new sizing version I posted above does work? Commented Aug 5, 2013 at 15:20
  • That works very good. Do we still need the \hss in the \alglinenumber definition? Commented Aug 5, 2013 at 15:26
  • No the \hss is no longer necessary. Now deleted from my code. Commented Aug 5, 2013 at 15:28
  • How can I get this solution to work if I am using \footnotesize within the algorithm environment? Commented Jan 27, 2016 at 8:58
  • 1
    I found that, by moving the \footnotesize command from the beginning of the environment to inside the first statement of the algorithm, as in \State \footnotesize foo, this solution works. Commented Jan 27, 2016 at 13:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .