[Tex/LaTex] Pseudocode overflow out of column

algorithmicxalgorithmsieeetranline-breaking

I am writing pseudocode using the algorithmicx package in IEEEtran class (double column). My problem is, some of the statement in the code are quite long which I expected to wrap to next line. However, the lines are overflowing out of the margin, something like:

\documentclass[10pt,conference,letterpaper]{IEEEtran}
\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Network similarity}
\begin{algorithmic}[2]
\Require{$G\_ref, G\_tar,ref\_community, tar\_community$}
\ForAll{$ref\_node \in G\_ref $}
\ForAll{$tar\_node \in G\_tar$}
\State{ $pair\_sim\_table[ref\_node,tar\_node] \gets Calculate\_similarity(ref\_feature\_vec[ref\_node],tar\_feature\_vec[tar\_node])$}
\EndFor
\EndFor \\
\Return pair\_sim\_table, cluster\_sim
\end{algorithmic}
\end{algorithm}
\end{document}

Since the code associated with the \State command is quite long, it overflows out of the margin, instead of wrapping around. Excuse me if this is a trivial question. Any help would be appreciated !

algo

Best Answer

I think a principal problem in your code is the use of the $ math-mode delimiters to set text in italics. By doing this, you get very poorly (unkerned) lettering in the words (most notably between "e" and "f", as in "ref"). Worse, in at least one case TeX sees the whole expression as one big in-line math formula and can't figure out (by its own syntax rules) where to place a line break; hence the overflow.

I suggest you try the following approach instead, which only uses $ for explicit math items such as \in and \gets:

\documentclass[10pt,conference,letterpaper]{IEEEtran}
\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Network similarity}
\begin{algorithmic}[2]
\Require{\emph{G\_ref}, \emph{G\_tar}, \emph{ref\_community}, \emph{tar\_community}}
\ForAll{\emph{ref\_node $\in$ G\_ref}}
\ForAll{\emph{tar\_node $\in$ G\_tar}}
\State{\emph{pair\_sim\_table}[\emph{ref\_node},\emph{tar\_node}] $\gets$  
  \emph{Calculate\_similarity}(\emph{ref\_feature\_vec}[\emph{ref\_node}], 
  \emph{tar\_feature\_vec}[\emph{tar\_node}])}
\EndFor
\EndFor \\
\Return \emph{pair\_sim\_table}, \emph{cluster\_sim}
\end{algorithmic}
\end{algorithm}
\end{document}

enter image description here