Take the 2-minute tour ×
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:

\lstnewenvironment{snippet}[1][]
    {\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
add comment

marked as duplicate by Jubobs, Werner, Martin Schröder, mafp, Jesse Jan 16 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

only possible when you define a width for a minipage:

\lstnewenvironment{snippet}[1][]
    {\hfill\lstset{frame=single,#1}\minipage{0.6\linewidth}}
    {\endminipage\hfill\null}
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
add comment

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

\lstset{linewidth=\textwidth,
xleftmargin=0.2\textwidth,
xrightmargin=0.2\textwidth}

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

share|improve this answer
add comment

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