0

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=\small\ttfamily,
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}[caption={test algorithm}]
#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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.