Page Layoutalgorithm environment

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
svenskmand
Posts: 5
Joined: Wed Aug 06, 2008 1:52 pm

algorithm environment

Post by svenskmand »

Hello all,

I am using algorithmic to format algorithms, but to get the nice numbering of algorithms and to make references to them I have to put them in floats that is like this

Code: Select all

\begin{algorithm}[htb]
	\caption{Insert($k$)}
	\textbf{Input:} A new key $k$ to insert into the heap. \\
	\textbf{Output:} Nothing. \\
	\textbf{Metode:}
	\begin{algorithmic}[1]
    \State{$Q.add(k)$}
    \If{$Q.size() >= Pm$}
      \State{Rename $l$ to $l+1$ and write the insert buffer $I$ to file $l$}
    \EndIf
    \If{$parent{l+1} \neq parent{l}$}
      \State{$Bubble(l+1)$}
    \EndIf
    \If{$l \neq 0$}
      \State{$Bubble(l)$}
    \EndIf
		\algstore*{Insert}
	\end{algorithmic}
	\label{alg:Insert}
\end{algorithm}
and I do not want them to be floats, but when I do this

Code: Select all

	\begin{algorithmic}[1]
    \State{$Q.add(k)$}
    \If{$Q.size() >= Pm$}
      \State{Rename $l$ to $l+1$ and write the insert buffer $I$ to file $l$}
    \EndIf
    \If{$parent{l+1} \neq parent{l}$}
      \State{$Bubble(l+1)$}
    \EndIf
    \If{$l \neq 0$}
      \State{$Bubble(l)$}
    \EndIf
		\algstore*{Insert}
	\end{algorithmic}
I cannot make references to the code with labes as the environment "algorithmic" does not have any counter.

Then i decided to try and make my own with the following

Code: Select all

\newcounter{algo}
\setcounter{algo}{0}
\newenvironment{algo}[1][]{
  \refstepcounter{algo}
  \makebox[\linewidth]{\line(1,0){\linewidth}}
  \noindent \textbf{Algorithm} #1 \hspace*{\fill} \newline
  \makebox[\linewidth]{\line(1,0){\linewidth}}
  \noindent
}{
  \makebox[\linewidth]{\line(1,0){\linewidth}}
}


See \ref{test}
\begin{algo} %\label{test}
  \begin{algorithmic}[1]
    \State{$Q.add(k)$}
	\end{algorithmic}
\end{algo}
See \ref{test}
which is more or less what I want, the spaceing should be adjusted, but the main problem is that I get tons of warnings, and I do not like that see

Code: Select all

This is pdfTeXk, Version 3.141592-1.40.3 (Web2C 7.5.6)
 %&-line parsing enabled.
entering extended mode
(./Report.tex
LaTeX2e <2005/12/01>
Babel <v3.8h> and hyphenation patterns for english, usenglishmax, dumylang, noh
yphenation, arabic, farsi, croatian, ukrainian, russian, bulgarian, czech, slov
ak, danish, dutch, finnish, basque, french, german, ngerman, ibycus, greek, mon
ogreek, ancientgreek, hungarian, italian, latin, mongolian, norsk, icelandic, i
nterlingua, turkish, coptic, romanian, welsh, serbian, slovenian, estonian, esp
eranto, uppersorbian, indonesian, polish, portuguese, spanish, catalan, galicia
n, swedish, ukenglish, pinyin, loaded.
(/usr/share/texmf-texlive/tex/latex/base/article.cls
Document Class: article 2005/09/16 v1.4f Standard LaTeX document class
(/usr/share/texmf-texlive/tex/latex/base/size10.clo))
(/usr/share/texmf-texlive/tex/latex/base/inputenc.sty
(/usr/share/texmf-texlive/tex/latex/base/ansinew.def))
(/usr/share/texmf-texlive/tex/latex/base/fontenc.sty
(/usr/share/texmf-texlive/tex/latex/base/t1enc.def))
(/usr/share/texmf-texlive/tex/latex/amsmath/amsmath.sty
For additional information on amsmath, use the `?' option.
(/usr/share/texmf-texlive/tex/latex/amsmath/amstext.sty
(/usr/share/texmf-texlive/tex/latex/amsmath/amsgen.sty))
(/usr/share/texmf-texlive/tex/latex/amsmath/amsbsy.sty)
(/usr/share/texmf-texlive/tex/latex/amsmath/amsopn.sty))
(/usr/share/texmf-texlive/tex/latex/amsfonts/amssymb.sty
(/usr/share/texmf-texlive/tex/latex/amsfonts/amsfonts.sty))
(/usr/share/texmf-texlive/tex/latex/amscls/amsthm.sty)
(/usr/share/texmf-texlive/tex/latex/geometry/geometry.sty
(/usr/share/texmf-texlive/tex/latex/graphics/keyval.sty)
(/usr/share/texmf-texlive/tex/xelatex/xetexconfig/geometry.cfg))
(/usr/share/texmf-texlive/tex/latex/ae/ae.sty
(/usr/share/texmf-texlive/tex/latex/base/fontenc.sty
(/usr/share/texmf-texlive/tex/latex/base/t1enc.def)
(/usr/share/texmf-texlive/tex/latex/ae/t1aer.fd)))
(/usr/share/texmf-texlive/tex/latex/algorithms/algorithm.sty
Document Style `algorithm' - floating environment
(/usr/share/texmf-texlive/tex/latex/float/float.sty)
(/usr/share/texmf-texlive/tex/latex/base/ifthen.sty))
(/usr/share/texmf-texlive/tex/latex/algorithmicx/algorithmicx.sty
Document Style algorithmicx 1.2 - a greatly improved `algorithmic' style
) (/usr/share/texmf-texlive/tex/latex/algorithmicx/algpseudocode.sty
Document Style - pseudocode environments for use with the `algorithmicx' style
) (/usr/share/texmf-texlive/tex/latex/tools/enumerate.sty)
(/usr/share/texmf-texlive/tex/latex/ltxmisc/url.sty)
(/usr/share/texmf-texlive/tex/latex/hyperref/hyperref.sty
(/usr/share/texmf-texlive/tex/latex/hyperref/pd1enc.def)
(/etc/texmf/tex/latex/config/hyperref.cfg)
(/usr/share/texmf-texlive/tex/latex/oberdiek/kvoptions.sty)
Implicit mode ON; LaTeX internals redefined
)
*hyperref using default driver hdvips*
(/usr/share/texmf-texlive/tex/latex/hyperref/hdvips.def
(/usr/share/texmf-texlive/tex/latex/hyperref/pdfmark.def))
(/usr/share/texmf-texlive/tex/latex/graphics/graphicx.sty
(/usr/share/texmf-texlive/tex/latex/graphics/graphics.sty
(/usr/share/texmf-texlive/tex/latex/graphics/trig.sty)
(/etc/texmf/tex/latex/config/graphics.cfg)
(/usr/share/texmf-texlive/tex/latex/graphics/dvips.def)))
(/usr/share/texmf-texlive/tex/latex/pstricks/pstricks.sty
(/usr/share/texmf-texlive/tex/generic/pstricks/pstricks.tex
`PSTricks' v1.15  <2006/12/22> (tvz)
(/usr/share/texmf-texlive/tex/generic/pstricks/pstricks.con))
(/usr/share/texmf/tex/latex/xcolor/xcolor.sty
(/etc/texmf/tex/latex/config/color.cfg)))
(/usr/share/texmf-texlive/tex/latex/stmaryrd/stmaryrd.sty)
(/usr/share/texmf-texlive/tex/latex/tools/multicol.sty)
(/usr/share/texmf-texlive/tex/latex/colortbl/colortbl.sty
(/usr/share/texmf-texlive/tex/latex/tools/array.sty))
(/usr/share/texmf-texlive/tex/latex/listings/listings.sty
(/usr/share/texmf-texlive/tex/latex/listings/lstpatch.sty)
(/usr/share/texmf-texlive/tex/latex/listings/lstmisc.sty)
(/usr/share/texmf-texlive/tex/latex/listings/listings.cfg))
(/usr/share/texmf-texlive/tex/latex/subeqnarray/subeqnarray.sty)
(/usr/share/texmf-texlive/tex/latex/listings/lstlang1.sty)
(/usr/share/texmf-texlive/tex/latex/listings/lstlang1.sty)
(/usr/share/texmf-texlive/tex/latex/listings/lstmisc.sty) (./Report.aux)
(/usr/share/texmf-texlive/tex/latex/hyperref/nameref.sty
(/usr/share/texmf-texlive/tex/latex/oberdiek/refcount.sty)) (./Report.out)
(./Report.out) (/usr/share/texmf-texlive/tex/latex/amsfonts/umsa.fd)
(/usr/share/texmf-texlive/tex/latex/amsfonts/umsb.fd)
(/usr/share/texmf-texlive/tex/latex/stmaryrd/Ustmry.fd)
(/usr/share/texmf-texlive/tex/latex/ae/t1aett.fd) (./figure/Datastructure.pst)
[1]
! Missing number, treated as zero.
<to be read again> 
                   \vrule 
l.228   \begin
              {algorithmic}[1]
? ! Illegal unit of measure (pt inserted).
<to be read again> 
                   \vrule 
l.228   \begin
              {algorithmic}[1]
? ! Missing number, treated as zero.
<to be read again> 
                   \vrule 
l.228   \begin
              {algorithmic}[1]
? ! Illegal unit of measure (pt inserted).
<to be read again> 
                   \vrule 
l.228   \begin
              {algorithmic}[1]
? ! Missing number, treated as zero.
<to be read again> 
                   \vrule 
l.231 \end{algo}
                
? 
! Emergency stop.
<to be read again> 
                   \vrule 
l.231 \end{algo}
                
Output written on Report.dvi (1 page, 15420 bytes).
Transcript written on Report.log.
Hope someone can help :)

Best Regards.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

algorithm environment

Post by gmedina »

Hi,

since the algorithm environment implemented by the algorithms package (which I guess is the package that you are using) is built with the float package, then you can use the "H" modifier (which means "place this object HERE") to specify the placement; i.e., you can use something like

Code: Select all

\begin{algorithm}[H]
...(some code)
\end{algorithm}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Post Reply