From charlesreid1

A guide to using the Algorithm package from LyX.

Important Note!

It is important for you to note: if you are getting errors when using the algorithm module/package, it may not be a problem with the installation of the package - it may be a problem with how you're using it! The algorithm package is particularly picky about matching statements like "if" with "else" and "endif" - if you open an "if" and don't close with an "endif", algorithm WILL throw errors, and they will NOT clearly state that the problem is with "if" not being closed with an "endif".

See the #Errors section below for details.

Using Algorithm Package from LyX on Mac (Mountain Lion)

Step 1: Get It

Download the algo for LyX package from here: http://bgu.perso.libertysurf.fr/

It is a zip file containing the following files:

Readme.txt
algo.lyx
algo.ps
algo.tex
algolyx.sty
algorithm.inc
article-algo.layout
examples
lang
readme.lyx

Step 2: Copy necessary files

You'll need to copy the layout and inc files to LyX's layouts directory:

$ cp article-algo.layout ~/Library/Application\ Support/LyX-2.0/layouts/.

 $ cp algorithm.inc ~/Library/Application\ Support/LyX-2.0/layouts/.

and put the algorithm style file where LaTeX

$ cp algolyx.sty ~/Library/texmf/tex/latex/.

Step 3: Reconfigure LyX

The last step is to reconfigure LyX. Once you do this,

Using the Algorithm Package from Arbitrary LaTeX Classes

If you follow the instructions above (from the website http://bgu.perso.libertysurf.fr/), you'll see that the only way you can use the Algorithm package is by using the "Article (algo)" class that is provided. This is a problem if, say, you are using a custom class, for your thesis or for a technical report.

This is easy to fix - simply add the following line to your LyX .layout file:

# Use LyX algorithm definitions
Input algorithm.inc

(Note that the layout file is a LyX wrapper for a LaTeX class file; see LyX#Layouts_vs._Classes for details.)

Using the Algorithm Package from LyX

To use the algorithm package, start by inserting an algorithm float, via Insert > Float > Algorithm:

AlgoFloat.png

Next, to put algorithm-style text into the algorithm float, change the text layout style (using the upper-left-hand drop-down menu) to "Algorithm":

AlgorithmLayout.png

Finally, enter some test text:

AlgorithmTestLyx.png

which should result in the following algorithm in your PDF file:

AlgorithmTestPdf.png

Algorithm Class Keywords

When you write an algorithm in Lyx, you have to use reserved keywords that are specified by the algorithm class - you can't just start lines with any words you want.

You also have to create control structures - that's right, STRUCTURES - and if they aren't assembled correctly, the algorithm class will throw errors.

Here are some examples:

for something
  do something
endfor

To enter this in Lyx, select your algorithm float, change the text style to algorithm, and literally type, "for something". The word "for" will be in bold, because the Algorithm class recognizes that it is a keyword. When you type the next line, "do something", it should be indented with a tab. So type, "tab", then "do something". Finally, to close your loop, type "shift+tab" to de-indent, and then type endfor.

You can do the same with a while loop, too:

while something
  do something
endwhile

If you are getting problems with your algorithm and you aren't sure where the errors are coming from, try simplifying your control structure: add things in, one level at a time, so that you can isolate where the issue is coming from.

Base Keywords

Algorithm class keywords include the following (via http://cs.brown.edu/about/system/software/latex/doc/algodoc.pdf):

  • if
  • then
  • else
  • fi (or endif)
  • do
  • rof (or endfor)
  • while
  • elihw (or endwhile)
  • repeat
  • until

Even More Keywords

More keywords:

  • let
  • to
  • downto
  • and
  • or
  • not
  • return
  • if
  • then
  • else
  • stop
  • nil
  • true
  • false
  • comment

Errors

When I first used the algorithm package, I saw a list of errors that looked like this:

! LaTeX Error: \begin{ALC@g} on input line 87 ended by \end{algorithmic}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.118 
      
Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.


! LaTeX Error: \begin{ALC@rpt} on input line 87 ended by \end{algor}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.118 
      
Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.

! Extra }, or forgotten \endgroup.
\@endfloatbox ...pagefalse \outer@nobreak \egroup 
                                                  \color@endbox 
l.118 
      
I've deleted a group-closing symbol because it seems to be
spurious, as in `$x}$'. But perhaps the } is legitimate and
you forgot something else, as in `\hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing `I$}'.

! Extra }, or forgotten \endgroup.
\color@endbox ->\color@endgroup \egroup 
                                        
l.118 
      
I've deleted a group-closing symbol because it seems to be
spurious, as in `$x}$'. But perhaps the } is legitimate and
you forgot something else, as in `\hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing `I$}'.


! LaTeX Error: \begin{algorithmic} on input line 86 ended by \end{algorithm}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.118 
      
Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.


! LaTeX Error: Not in outer par mode.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.121 \begin
            {algor}
You've lost some text.  Try typing  <return>  to proceed.
If that doesn't work, type  X <return>  to quit.


! LaTeX Error: \begin{algor} on input line 85 ended by \end{document}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.333 \end{document}
                    
Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.

(./siluria_algo.aux)

LaTeX Warning: There were undefined references.

! You can't use `\end' in internal vertical mode.
\enddocument ... \endgroup \deadcycles \z@ \@@end 
                                                  
l.333 \end{document}
                    
Sorry, but I'm not programmed to handle this case;
I'll just pretend that you didn't ask for it.
If you're in the wrong mode, you might be able to
return to the right one by typing `I}' or `I$' or `I\par'.


! LaTeX Error: \begin{algor} on input line 85 ended by \end{document}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.333 \end{document}
                    
Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.

)
! Emergency stop.
<*> siluria_algo.tex
                    
*** (job aborted, no legal \end found)

This was caused by the fact that I was opening algorithm statements with keywords like "if" and "while" without properly closing those loops with their corresponding keywords ("endif" or "endwhile"). Algorithm is picky about this, and it doesn't explain the problem clearly when you do it wrong.

Try entering the following algorithm test text, which uses the proper Algorithm format, to ensure you are properly testing your Algorithm package install:

repeat {first comment}
  if something true {second comment}
    * action 1 {third comment} 
  else
    * last possible action
  endif
until it is still true {the big loop condition}