# [Insight-users] Writing Code in LaTeX in Insight Journal Papers

Dan Mueller dan.muel at gmail.com
Mon Jan 26 07:26:12 EST 2009

Hi Insight Users,

As per Luis' suggestion, please find below some instructions regarding
use of the "listings" LaTeX package for including code in Insight
Journal Papers.

http://www.ctan.org/tex-archive/macros/latex/contrib/listings/

I use the MiKTeX distribution on Windows, which has a package
browser/installer. I'm not sure how it is works for other systems, so
you're on your own getting the package set up and working.

2. Next you have to "use" the package in the preamble of your article:
\usepackage{listings}

3. Now you have to include your code in the article. There are two
options: "inline", or from a "source" file. I tend to use the source
option the most.

3a. Using an "inline" listing:

\begin{lstlisting}
unsigned int lifeUniverseAndEverything = 42;
\end{lstlisting}

3b. Using (part of) a raw "source" file:

\begin{center}
\lstinputlisting[linerange={2-10}]
{../Examples/example1.cxx}
\end{center}

4. The package provides heaps of options to make the code look
"pretty" (i.e. code highlighting, line numbers, borders, background
colour, etc). I tend to encapsulate these options by defining new
command(s) in the article preamble:

eg. for C++ code:

\newcommand{\lstset at cpp}
{
\lstset{frame = tb,
framerule = 0.25pt,
float,
backgroundcolor={\color{listlightgray}},
basicstyle = {\ttfamily\footnotesize},
keywordstyle = {\ttfamily\color{listkeyword}\textbf},
identifierstyle = {\ttfamily},
stringstyle = {\ttfamily},
showstringspaces = false,
showtabs = false,
numbers = left,
numbersep = 6pt,
numberstyle={\ttfamily\color{listnumbers}},
tabsize = 2,
language=[ANSI]C++,
floatplacement=!h
}
}

eg. for python

\newcommand{\lstset at python}
{
\lstset{frame = tb,
framerule = 0.25pt,
float,
backgroundcolor={\color{listlightgray}},
basicstyle = {\ttfamily\footnotesize},
keywordstyle = {\ttfamily\color{listkeyword}\textbf},
identifierstyle = {\ttfamily},
stringstyle = {\ttfamily},
showstringspaces = false,
showtabs = false,
numbers = left,
numbersep = 6pt,
numberstyle={\ttfamily\color{listnumbers}},
tabsize = 2,
language=Python,
floatplacement=!h
}
}

etc.

These commands require the "color" package and some additional defines:

\usepackage{color}

\definecolor{listcomment}{rgb}{0.0,0.5,0.0}
\definecolor{listkeyword}{rgb}{0.0,0.0,0.5}
\definecolor{listnumbers}{gray}{0.65}
\definecolor{listlightgray}{gray}{0.955}
\definecolor{listwhite}{gray}{1.0}

Now you simply call the desired command when needed.

4a. For "inline" listings

\lstset at cpp % Add this to make "pretty" cpp code
\begin{lstlisting}
unsigned int lifeUniverseAndEverything = 42;
\end{lstlisting}

4b. For "source" listings

\begin{center}
\lstset at cpp % Add this to make "pretty" cpp code
\lstinputlisting[linerange={2-10}]
{../Examples/example1.cxx}
\end{center}

Other supported languages include: C, Gnuplot, tcl, XML, Java, [Sharp]C, Matlab.

Some of my other IJ articles have the LaTeX source included,
<shamelessplug>so feel free to check them out</shamelessplug> for a
full example:
http://www.insight-journal.org/browse/publication/117
http://www.insight-journal.org/browse/publication/151

I notice Nick Tustison's papers also have "pretty" code, so he might
http://www.insight-journal.org/browse/publication/306

http://www.ctan.org/get/macros/latex/contrib/listings/listings.pdf

HTH

Regards, Dan

2009/1/24 Luis Ibanez <luis.ibanez at kitware.com>:
>
> Hi Dan,
>
> We like very much the formatting of source code in your paper
>
> "Fast Marching Minimal Path Extraction in ITK"
>
> Could you please share with the mailing-list the method that
> you are using to format this code for LaTeX ?
>
> It will be very useful to other Insight Journal contributors.
>
> We probably should add it to the guidelines of the Insight Journal Wiki.
>
>
>   Thanks,
>
>
>      Luis
>
>
> ---------------------
>>>
>>>
>>> I'm trying to use your latex template to write my paper. I would like to
>>> add an example of the code to my paper. (For example, like in page 4 of the
>>> paper http://www.insight-journal.org/browse/publication/213).
>>>
>>>
>>>
>>> Can you please send me a .tex example on how to write the code in the
>>> format of the paper above (such an example does not appear in the provided
>>> template in the Insight Journal website)?
>>>
>>>
>>>
>>> Thank you,
>>>
>>>
>>>
>>> Lior Weizman
>>>
>>>
>>>
>>> System: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; GTB5;
>>> .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.4506.2152;
>>> .NET CLR 3.5.30729)
>>>
>>
>