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

This question already has an answer here:

I am trying to center a listing horizontally within a page.

So far I have tried defining the following macro:

    {\centering \lstset{float=htpb,#1}} 

But it did not work.

I have read this question and answer, but I would prefer a solution that does not rely in figures or tables.

share|improve this question

marked as duplicate by Jubobs, Werner, Martin Schröder, mafp, Jesse Jan 16 '14 at 3:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

only possible when you define a width for a minipage:

share|improve this answer
Your suggestion is ot working, it gives a ! Emergency stop. error and the resulting file does not contain the listing. – Tiago Veloso Feb 5 '11 at 15:39
@Tiago: than give me a minimal example, which shows this error. For me it works! – Herbert Feb 5 '11 at 16:42

If you set the line width i.e. \lstset{linewidth=0.6\textwidth}and want that centered, you can use margins instead. i.e.:


I usually use this method, but I haven't tried with the float= setting.

share|improve this answer

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