TransWikia.com

LaTeX listing package Pseudocode with Syntax Highlighting

TeX - LaTeX Asked by Raja Ayaz on December 1, 2020

I am trying to use listings package for writing algorithm. I have used algorithm2e but its hard and i cannot customize it specially the fonts.

I am following this stackexchange answer:
documentclass{article}
usepackage{listings}
usepackage{caption}

lstset{
language=C++,
basicstyle=smallttfamily,
numbers=left,
numbersep=5pt,
xleftmargin=20pt,
frame=tb,
framexleftmargin=20pt
}

renewcommand*thelstnumber{arabic{lstnumber}:}

DeclareCaptionFormat{mylst}{hrule#1#2#3}
captionsetup[lstlisting]{format=mylst,labelfont=bf,singlelinecheck=off,labelsep=space}

begin{document}

begin{lstlisting}
#include <iostream>
using namespace std;

int main()
{
  cout << "Welcome to the wonderful world of C++!!!n";
  return 0;
}
end{lstlisting}

end{document}

this code produces output which looks like this:
enter image description here

Is it possible to make the output look like this?
enter image description here

  • The caption should be Algorithm, and the Algorithm and Listings should have separate counters as i use both of them.
  • Include Input and Output
  • Vertical lines between code blocks

Any ideas how to achieve this using listings?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP