TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm typesetting pseudocode with algorithm and algorithmicx to get results like the following (taken from the package documentation): algorithm + algorithmicx

Now I would like to print real C++ code with the listings package. Is there any way to make a lstlisting look exactly the same (showing something like Listing 1 instead of Algorithm 1, of course)?

share|improve this question
How can C code look exactly the same as pseudocode? – egreg Sep 20 '12 at 17:04
I refered to the design of the algorithm environment, not to the code itself, sorry :) More precisely, I would like to have the three horizontal rules, the left aligned caption and the line numbers. – ph4nt0m Sep 20 '12 at 17:09
up vote 10 down vote accepted

The caption package can be used to change the caption formatting: typeset the label in boldface font, deactivate centered captions when they fit into a single line, suppress the default label separator and replace it with a space, and add the rule before the caption. The rule after the caption and the one at the end of the caption can be added using the frame= option for \lstset; numbering and some indentations can also be controlled using features provided by the listings package:






\begin{lstlisting}[caption={test algorithm}]
#include <iostream>
using namespace std;

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


enter image description here

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.