[Tex/LaTex] Center a Listing on page

horizontal alignmentlistings

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.

Best Answer

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}