Home General LaTeX tips and tricks → Listing source code [Previous Page] [Question index] [Next Page]

11. How can I get source code listings nicely printed?


Answer

Sorry, you are on your own, more or less.

I know that there are third-party LaTeX packages that are tailored towards formatting and displaying of source code listings in LaTeX documents. I did not have the time to investigate these packages for my own purposes, and so I was stuck with something really basic I wrote myself. Just in case that this quick 'n dirty hack might be all you need, too, I include it here.


Example usage

In the preamble, or in an appropriate include file:


\newenvironment{mylisting}
{\begin{list}{}{\setlength{\leftmargin}{1em}}\item\scriptsize\bfseries}
{\end{list}}

\newenvironment{mytinylisting}
{\begin{list}{}{\setlength{\leftmargin}{1em}}\item\tiny\bfseries}
{\end{list}}

In the manuscript:


\begin{mylisting}
\begin{verbatim}
  double a[n], b[n], c[n];

  for (i = 0; i < n; i++)
      c[i] = a[i] + b[i];
\end{verbatim}
\end{mylisting}


Result, before

'Before' screenshot

Result, after

'After' screenshot

[Previous page] [Question index] [Next page]

Leo Breebaart (leo@lspace.org)
Last updated: 27 June 2016