Generallong lines in algorithmic

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
pepperjack
Posts: 5
Joined: Mon Oct 06, 2008 1:55 pm

long lines in algorithmic

Post by pepperjack »

Hello
I was hoping someone can help me... I've not used Latex a great deal before and I'm trying to write some pseudocode using the algorithmic environment, but for some reason it seems to get the line widths all wrong and long lines of code extend off the edge of the page before wrapping round to the next line.
I'm not using the 'algorithm' environment because I don't really want it to be a float and the code extends over quite a few pages - from what I have read this can cause problems.
As a hack, I tried adding a heap of '\quad's where I wanted the line break, but that didn't work... Does anyone have any idea what the problem may be? Or if not, then a hack that might fix it? This is just one appendix of many at the end of my thesis, the rest of the document is fine (though this is all the pseudocode I've got - the rest is text and figures).
Thanks!
Ruth

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

long lines in algorithmic

Post by Juanjo »

It is quite difficult to figure out what could be the problem. You should try to build a minimal working example and post it here.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
pepperjack
Posts: 5
Joined: Mon Oct 06, 2008 1:55 pm

Re: long lines in algorithmic

Post by pepperjack »

Hello

Here's an example - the line starting 'usolnft' falls off the edge of the page! Any help very much appreciated.

Thanks

Ruth


\documentclass[a4paper,12pt]{article} \usepackage{algorithmic}
\textwidth=15cm%6.25true in
\textheight=23cm%9.65true in
\voffset=-1.5cm%-1true in
\hoffset=-0.30true in
\begin{document}
\begin{algorithmic}
\FOR{something}
\FOR{something else}
\FOR{even another thing}
\STATE usolnft(i,j) = (0.5*epsilon1(j)*y(i)*vsolnft(i,j) + ci*k(j)*dvsolndy + ci*k(j)*thfft(i,j))/(k(j)**2. + epsilon1(j)**2.)
\ENDFOR
\ENDFOR
\ENDFOR
\end{algorithmic}
\end{document}
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

long lines in algorithmic

Post by gmedina »

Hi Ruth,

you could use \\ to change the line and \hspace to add some horizontal space, as the following example suggests:

Code: Select all

\documentclass[a4paper,12pt]{article} 
\usepackage{algorithmic}

\begin{document}

\begin{algorithmic}
  \FOR{something}
    \FOR{something else}
      \FOR{even another thing}
        \STATE usolnft(i,j) = (0.5*epsilon1(j)*y(i)*vsolnft(i,j) + ci*k(j)*dvsolndy + \\\hspace{2.5cm}
          ci*k(j)*thfft(i,j))/(k(j)**2. + epsilon1(j)**2.)
      \ENDFOR
    \ENDFOR
  \ENDFOR
\end{algorithmic}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

long lines in algorithmic

Post by Juanjo »

You have several solutions:
  • Manually cut long lines with the \\ command and indent the second line with \hspace, as suggested by gmedina in his quick answer (more than mine ;) )
  • Write in math mode, surrounding the formula by $ and $ or by \( and \). The line will be automatically broken, but it will not be properly indented.
  • Write in math mode, using a suitable environment for breaking the formula. For example, load the amsmath package and replace the \STATE line by

    Code: Select all

    \STATE $\mathit{usolnft}(i,j)=\!
            \begin{aligned}[t]
               & (0.5*\mathit{epsilon}1(j)*y(i)*\mathit{vsolnft}(i,j) + ci*k(j)*\mathit{dvsolndy} \\
               &+ ci*k(j)*\mathit{thfft}(i,j))/(k(j){**}2. + \mathit{epsilon}1(j){**}2.)
            \end{aligned}$
    
In the preceding code, note the \mathit commands that I find better to format variables with long names (remove then and compare). Likewise, the braces around ** prevents LaTeX to consider * as a binary operator, so leaving too much space. Finally, the \! command in the first line removes a small space left by the aligned environment.

Looking at your code, I would suggest to never modify \voffset and \hoffset. If you master lengths to fix the page layout, do it by yourself. If not, it is better to use the geometry package. Finally, you may consider to use the algorithmicx bundle. For providing a smooth transition from the algorithmic package, you could use its replacement, that is, the algcompatible package. For new algorithms, it is recommended to load algpseudocode.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
pepperjack
Posts: 5
Joined: Mon Oct 06, 2008 1:55 pm

Re: long lines in algorithmic

Post by pepperjack »

Thanks very much, both solutions work well. Thanks also for the tips on beautifying my code - it was looking far uglier than necessary before! (I had wondered about using math mode, but had previously been math-moding almost everything, so it was all italicized... This looks much nicer)
May I ask why you recommend not modifying \voffset and \hoffset?
Cheers
R
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

long lines in algorithmic

Post by Juanjo »

pepperjack wrote: May I ask why you recommend not modifying \voffset and \hoffset?
There are other lengths to set margins (\oddsidemargin and \evensidemargin horizontally, \topmargin vertically). The lengths \voffset and \hoffset, whose default value is 0pt, are intended to move the whole page if required. For example, if a printer does not correctly loads paper and starts printing, say, 1 cm below the right point; then the problem could be solved by generating and printing a document with \voffset=-1cm. Modifying those lengths can have side effects with some packages, like pdfpages.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
Post Reply