Sign up ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

2 Answers 2

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.