Home » LaTeX26 May 2015

LaTeX: How to write algorithm?

There are different packages that help writing algorithms on LaTeX. I will be using algorithm2e package for writing algorithm in LaTeX. This package should work fine if you have installed ‘TeX Live Full‘. Otherwise, you may get error while using algorithm2e package. If you get an error then you need to install the package ‘texlive-science‘ which includes algorithm2e package.

On Ubuntu, you can install texlive-science package with the following command:

Here is the complete LaTeX code that has examples on writing algorithm using algorithm2e package. The first algorithm has While loop along with If/Else condition. The second algorithm example has nested ForEach loop with If/ElseIf/Else condition inside it.

Below is the pdf file generated after you compile the above LaTeX source:

Hope this helps.


Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • Kamil

    It would be ideal if you could attach also screenshots of result when compiling those examples with library you’re describing.

  • Thanks Kamil for your valuable suggestion. I have added the pdf file generated by the LaTeX source.


    hi every one. here is the algorithm. can any one send me the code of latex, or can create it here.

    1.on receiving data packet with seq. number seqNr:
    if cache not locked
    cache with certain probability
    if cache is empty
    forward unless node has retransmitted same
    seqNr within some time limit
    set link level ACK timeout

    2. on link level ACK timeout:
    lock cache
    set retransmission timer
    3. on receiving ACK with acknowledgment number
    ackSeq and sequence number cached in cache:
    update local RTT
    3.1 if ackSeq > cached
    cancel retransmission timer
    clear cache
    3.2 if ackSeq = cached
    if time since last transmission > local
    RTT ∗ factor
    retransmit cached segment
    3.3 if ackSeq < cached and SACK option set
    clear cache if cached in SACK block
    if cached not in SACK block
    add cached seqNr to SACK block
    if all gaps filled
    drop ACK
    3.4 if ackSeq < cached and no SACK option set and cache locked
    add SACK option with cached seqNr as SACK block
    4. on local retransmission timeout:
    retransmitted cached segment
    set retransmission timer


  • Mykola Servetnyk

    Hi! I found your example very useful. Thank you! Could you tell me how is it possible in Algorithm 2 to put end after if -> elseif -> else statement?