Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
You should introduce manual line breaks yourself and correct for the indentation, if needed. See Include a line break in algorithmic while maintaining indentation. If such manual intervention is what you're after, then I would consider this a duplicate, since the answer there should solve your problem (using something like \Statex or perhaps the varwidth package). Give some feedback after investigating. –  Werner Jul 8 '12 at 22:52
    
@Werner: Thanks for the link ! The varwidth package did it for me. Thanks a lot. Is it required to delete this question (if it is possible ) since the answer to it was actually given in an earlier post ? –  R.Bahl Jul 8 '12 at 23:21
add comment

1 Answer

up vote 3 down vote accepted

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

share|improve this answer
    
Mico: Thanks for the tip ! This has also worked out quite well for me. –  R.Bahl Jul 8 '12 at 23:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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