11

I found this answer and I applied it but this is the result.

enter image description here

This is my code.

\documentclass[conference]{IEEEtran}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{dsfont}
\usepackage{algorithm}
\PassOptionsToPackage{noend}{algpseudocode}% comment out if want end's to show
\usepackage{algpseudocode}

\makeatletter
% start with some helper code
% This is the vertical rule that is inserted
\newcommand*{\algrule}[1][\algorithmicindent]{\makebox[#1][l]{\hspace*{.5em}\vrule height .75\baselineskip depth .25\baselineskip}}%

\newcount\ALG@printindent@tempcnta
\def\ALG@printindent{%
    \ifnum \theALG@nested>0% is there anything to print
    \ifx\ALG@text\ALG@x@notext% is this an end group without any text?
    % do nothing
    \addvspace{-3pt}% FUDGE for cases where no text is shown, to make the rules line up
    \else
    \unskip
    % draw a rule for each indent level
    \ALG@printindent@tempcnta=1
    \loop
    \algrule[\csname ALG@ind@\the\ALG@printindent@tempcnta\endcsname]%
    \advance \ALG@printindent@tempcnta 1
    \ifnum \ALG@printindent@tempcnta<\numexpr\theALG@nested+1\relax% can't do <=, so add one to RHS and use < instead
    \repeat
    \fi
    \fi
}%
\usepackage{etoolbox}
% the following line injects our new indent handling code in place of the default spacing
\patchcmd{\ALG@doentity}{\noindent\hskip\ALG@tlm}{\ALG@printindent}{}{\errmessage{failed to patch}}
\makeatother
% end vertical rule patch for algorithmicx
\makeatother


\begin{document}

\IEEEpeerreviewmaketitle

    \begin{algorithm}
        \caption{Arbitrary Algorithm}\label{IS2OSLS}
        \begin{algorithmic}[1]
            \Require A matrix $\mathbf{A}$ of size $m\times n$.
            \Ensure Something.
            \For{$i$ in $m$}
                \For{$j$ in $n$}
                    \If{$i=j$}
                        \State Select a random action
                    \Else
                        \If{$i=j+1$}
                            \State Stay silent 
                        \Else 
                            \State Break
                        \EndIf
                    \EndIf
                \EndFor
            \EndFor
        \end{algorithmic}
    \end{algorithm}
\end{document}

I wold like to have vertical lines that start just after the first letter of the keyword, i.e., for the keyword for the vertical line should start after f, etc. In the figure above, the vertical lines start after the o not the f. Also I need the pseudo code without the end keyword as shown.

Thank you.

EDIT

I can change \hspace*{.5em} to \hspace*{.1em} in order to move the vertical line a little to the left. This is solved. But why do the lines overlaps at the end of the algorithm? (If I add the end keywords the problem is solved.)

Chiba
  • 449

1 Answers1

12

An \addvspace instruction that is the cause for overlapping; remove it. I also added another patch that avoids spurious vertical space when an “end” tag is omitted, see Spurious whitespace with algpseudocode and noend

For moving left the rules, act on the \hspace{.5em}, as you discovered; in the example I used .2em.

In the following code I left only the necessary packages.

\documentclass[conference]{IEEEtran}

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

\makeatletter
% start with some helper code
% This is the vertical rule that is inserted
\newcommand*{\algrule}[1][\algorithmicindent]{%
  \makebox[#1][l]{%
    \hspace*{.2em}% <------------- This is where the rule starts from
    \vrule height .75\baselineskip depth .25\baselineskip
  }
}

\newcount\ALG@printindent@tempcnta
\def\ALG@printindent{%
    \ifnum \theALG@nested>0% is there anything to print
    \ifx\ALG@text\ALG@x@notext% is this an end group without any text?
    % do nothing
    \else
    \unskip
    % draw a rule for each indent level
    \ALG@printindent@tempcnta=1
    \loop
    \algrule[\csname ALG@ind@\the\ALG@printindent@tempcnta\endcsname]%
    \advance \ALG@printindent@tempcnta 1
    \ifnum \ALG@printindent@tempcnta<\numexpr\theALG@nested+1\relax
    \repeat
    \fi
    \fi
}
% the following line injects our new indent handling code in place of the default spacing
\patchcmd{\ALG@doentity}{\noindent\hskip\ALG@tlm}{\ALG@printindent}{}{\errmessage{failed to patch}}
\patchcmd{\ALG@doentity}{\item[]\nointerlineskip}{}{}{} % no spurious vertical space
% end vertical rule patch for algorithmicx
\makeatother

\begin{document}

\IEEEpeerreviewmaketitle

\begin{algorithm}
\caption{Arbitrary Algorithm}\label{IS2OSLS}

\begin{algorithmic}[1]
  \Require A matrix $\mathbf{A}$ of size $m\times n$.
  \Ensure Something.
  \For{$i$ in $m$}
    \For{$j$ in $n$}
      \If{$i=j$}
        \State Select a random action
      \Else
        \If{$i=j+1$}
          \State Stay silent 
        \Else 
          \State Break
        \EndIf
      \EndIf
    \EndFor
  \EndFor
\end{algorithmic}

\end{algorithm}

\end{document}

enter image description here

egreg
  • 1,121,712
  • Thank you. Is there an easy way to change the color of the vertical lines (like, gray more transparent)? For example, this color #c2c2c2 Color Hex Gray76. – Chiba Feb 14 '16 at 23:38
  • 1
    @Chiba Yes, of course: add \color{whatever} just before \vrule. – egreg Feb 15 '16 at 00:07
  • For some reason, the vertical rules are sometimes dashed like | | | (in vertical fashion). I mean there is a little space like every 1 cm. What would I get this? – Chiba Mar 29 '16 at 19:26
  • @Chiba I suspect you're using setspace – egreg Mar 29 '16 at 19:28
  • I am not using setspace package. But, when I remove this line \usepackage[lite,subscriptcorrection,slantedGreek,nofontinfo]{mtpro2}, there is no spaces and the vertical rules are fine. Do you know how can I fix this? – Chiba Mar 29 '16 at 19:39
  • 1
    @Chiba Sorry, but if I add that call to the code above, I get solid rules. Please, ask a new question with a minimal example showing the issue. – egreg Mar 29 '16 at 19:45
  • I asked a new question here – Chiba Mar 29 '16 at 21:44
  • Nice, but it does not work when using \usepackage{algpseudocode} one has to use \usepackage[noend]{algpseudocode} which makes the algorithm not look as good since missing all the end if and other end. It will be great if this worked with end support. – Nasser Jun 12 '20 at 08:34
  • fyi, I am OK now. Found another solution which worked without using noend. https://tex.stackexchange.com/questions/52473/is-it-possible-to-have-connecting-loop-lines-like-algorithm2e-in-algorithmic . Thanks. – Nasser Jun 12 '20 at 08:43