Center Latex lstlisting

Instead of using linewidth you should consider to use xleftmargin and xrightmargin (cf. texdoc listings, Chapter 4.10). The following code works without any center or minipage environment: \lstset{ caption=Descriptive Caption Text, basicstyle=\footnotesize, frame=tb, xleftmargin=.2\textwidth, xrightmargin=.2\textwidth } \begin{lstlisting} printf(“this should be centered!”); \end{lstlisting}

Highlighting a Chunk of Code within a lstlisting

You can use \colorbox and an escape character inside your listing: Add to your preamble \usepackage{color} \definecolor{light-gray}{gray}{0.80} then use it like this in your document: \begin{lstlisting}[escapechar=!] def mult(m: Matrix[Int], n: Matrix[Int]) { val p = !\colorbox{light-gray}{new MatrixInt}!(m.rows, n.cols) } \end{lstlisting}

How to change title in listings?

Put these lines to preamble (the preamble is everything from the start of the LaTeX source file until the \begin{document} command): \renewcommand\lstlistingname{Algorithm} \renewcommand\lstlistlistingname{Algorithms} The first changes the caption name for listings. The second — the header name for the list of listings which is printed by the \lstlistoflistings command. You would also probably want to … Read more

Math symbols in listings

You can use the option mathescape for your environment which gives you the ability to use the normal latex behavior of the $-signs. Try: \begin{lstlisting}[mathescape] … \end{lstlisting} For more info, take a look into the listings package manual.

tech