tags:

views:

295

answers:

1

How do I center text within a listing in Latex?

Or another way not using listing. (Need a box with monospace font)

A: 

I don't have an answer for the listing package on top of my head, but you could try the following:

\framebox[.9\linewidth]{\parbox{.85\linewidth}{\tt Hello World\\Second line}}

That produces a box with 90% of the line width, with text of width 85% of line width.

If you want it centered you just put \centering in front of the \tt command:

\framebox[.9\linewidth]{\parbox{.85\linewidth}{\centering \tt Hello World\\Second line}}

If you prefer the box without a frame, simply change \framebox into \makebox (and keep the arguments as they stand).

aioobe