algorithm.sty 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. %%%%%%%%%%% ALGORITHMS STYLE %%%%%%%%%%%
  2. % This style defines an environment for algorithms with the following
  3. % features:
  4. %
  5. % 1) Defines an Theorem like environment (not exactly one) for
  6. % algorithms called ``algorithm''.
  7. % \begin{algorithm}{NAME} ... \end{algorithm}
  8. % NAME is the NAME of the algorithm.
  9. % Algorithms are numbered using a counter called ``algorithm''.
  10. %
  11. % 2) Inside this environment, the following commands are defined.
  12. %
  13. % \= Put the small left arrow commonly used for assignment.
  14. %
  15. % \invariant{INVARIANT} Use to describe invariants, put its parameter
  16. % between ``{}'' in math mode.
  17. %
  18. % \begin{Block} ... \end{Block} Environment use to start a indented
  19. % block of instructions \end{Block} put
  20. % an ``end'' to close the block.
  21. %
  22. % \nextBlock{SEPARATOR} Inside a Block, it allows to put a separator
  23. % of parts of the indented block. Usefull for
  24. % constructions like ``IF..THEN..ELSE..END''
  25. % i.e. If cond then
  26. % \begin{Block}
  27. % then part
  28. % \nextBlock{else}
  29. % else part
  30. % \end{Block}
  31. %
  32. % 3) \Blockindent and \algorithmindent are length that specify the
  33. % indentention of Blocks and of the algorithm resp.
  34. %
  35. % 4) Instructions are separated by ``\\'', ``\par'' or by leaving a
  36. % blank line. Before an ``\end{Block}'' a ``\\'' can't be used.
  37. %
  38. %
  39. % Created by Jose Alberto Fernandez R.
  40. % e-mail: alberto@cs.umd.edu
  41. %
  42. %
  43. \newcounter{algorithm}
  44. \newtheorem{Alg@orithm}[algorithm]{Algorithm}
  45. % Invariants
  46. \newcommand{\inv@ariant}[1]{\mbox{$\{#1\}$}}
  47. % Steps environment
  48. \newenvironment{ste@ps}[1]{
  49. \begin{list}{}
  50. {\setlength\labelsep{0in}
  51. \addtolength\partopsep\topsep
  52. \addtolength\partopsep\parsep
  53. \setlength\parsep{0in}
  54. \setlength\topsep{0in}
  55. \setlength\itemsep{0in}
  56. \setlength\labelwidth{0in}
  57. \setlength\rightmargin{0in}
  58. \setlength\leftmargin{#1}}
  59. }{
  60. \end{list}
  61. }
  62. % Block environment
  63. % Indentation of the Block
  64. \newlength{\Blockindent}
  65. % NextBlock command
  66. \newcommand{\next@Block}[1]{
  67. \end{ste@ps}
  68. #1
  69. \begin{ste@ps}{\Blockindent}
  70. \item
  71. }
  72. \newenvironment{Blo@ck}{
  73. \let\nextBlock\next@Block
  74. \begin{ste@ps}{\Blockindent}
  75. \item
  76. }{
  77. \end{ste@ps}
  78. end
  79. }
  80. % Algorithm environment
  81. % Assign command
  82. \newcommand{\ass@ign}{\mbox{$\leftarrow$}}
  83. % Indentation of the algorithm
  84. \newlength{\algorithmindent}
  85. % Algorithm definition
  86. \newenvironment{algorithm}[1]{
  87. \let\=\ass@ign
  88. \let\invariant\inv@ariant
  89. \let\Block\Blo@ck
  90. \let\endBlock\endBlo@ck
  91. \begin{Alg@orithm} #1
  92. \rm\par
  93. \begin{ste@ps}{\algorithmindent}
  94. \item
  95. }{
  96. \end{ste@ps}
  97. \bf end
  98. \end{Alg@orithm}
  99. }
  100. % Setting default indentation
  101. \setlength{\algorithmindent}{1em}
  102. \setlength{\Blockindent}{2em}
  103. % End of algorithm.sty