2

This is a part of my code:

\begin{codebox}

\Procname{$\proc{\textbf{Connect_two_SamplePoints}}(Graph,S1, S2,connectionCostMat,cost)$}
\li \Comment S1 and S2 are sample points 
\li  cost[S1][S2] $=$ $\infty$ 
\li \Comment  cost holds the length of shortest path between S1 and S2
\li \For $pnt$ in connectionCostMat[S1]: \>\>\>\>\>\>\>\>\Comment candidate point for S1
\li \> \For $pnt2$ in connectionCostMat[S2]:  \>\>\>\>\>\>\>\>\Comment candidate point for S2
\li \> \> \If shortestPathLength($pnt$,$pnt2$) $+$ connectionCostMat[$S1$][$pnt$] $+$ connectionCostMat[$S2$][$pnt2$]< cost[S1][S2]:
\li \Then \> \> cost[S1][S2] $=$ shortestPathLength($pnt$,$pnt2$) $+$ connectionCostMat[$S1$][$pnt$] $+$ connectionCostMat[$S2$][$pnt2$] \End

\end{codebox}

You can see how long the name of the function is, as well as few line in the actual code. Using the default settings of clrscode3e, I get an ugly set of code, with function inputs and lines in code extended all the way to the margin and disappeared... pretty awful. I tried Centering clrscode3e codebox horizontally, but it didn't make things better.

Any help is appreciated!

1

I'm new to this package, but perhaps the following may be close to what you're after:

\documentclass{article}
\usepackage{clrscode3e}

\begin{document}

\begin{codebox}
\Procname{$\proc{ConnectTwoSamplePoints}(Graph, S1, S2, connectionCostMat, cost)$}
\li  cost[S1][S2] $= \infty$
\li \Comment  cost holds the length of the shortest path between S1 and S2
\li \For $pnt$ in connectionCostMat[S1]: \Comment candidate point for S1
\li \For $pnt2$ in connectionCostMat[S2]: \Comment candidate point for S2
\li \If shortestPathLength($pnt , pnt2$) +
\\ \quad connectionCostMat[$S1$][$pnt$] +
\\ \quad connectionCostMat[$S2$][$pnt2$] \textless cost[S1][S2]
\li \Then cost[S1][S2] $=$ shortestPathLength($pnt$,$pnt2$) +
\\  \quad \quad \quad connectionCostMat[$S1$][$pnt$] +
\\ \quad \quad \quad connectionCostMat[$S2$][$pnt2$]
\End
\end{codebox}

\end{document}

giving:

enter image description here

This package isn't on my (recent) CTAN so I used the .sty file from here.

Problems I encountered:

  1. Underscore (i.e. "_") doesn't seem to play well with \Procname - hopefully this is not essential...
  2. Need to use \\ to force newline. I've done this manually as it appears that the package isn't designed to cope with such long lines of code.
  3. Explict \Then has been deprecated and appears it's been replaced with a \quad spacing.
  4. Manually insert \quad spacing to have the text line up as expected.

I'm not sure this package is ideally suited to presenting the type of code you have in mind...

1
  • 2
    i don't think we've ever had clrscode3e on ctan. clrscode ought to be appropriate, given the nature of the book it was written for... ;-) Nov 30 '13 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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