GeneralCode listing

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
muusle
Posts: 3
Joined: Sat Jan 03, 2009 2:31 am

Code listing

Post by muusle »

I’m migrating from Word to Latex for reason well known on this forum I’ll bet. Though I’m starting to see why I’m migrating, I’m not convinced – yet…

Code listings turned out to be a quite nice experience, of that I’m relieved, being one of the reasons why I stuck with Word (cause of the source formatting, copy/paste from my fav code editor).

There is one more piece to the puzzle I really want to achieve with the code listing; the ability to somehow highlight a line with a custom background color (indicating new futures etc).

I use this technique in Word to highlight a portion of the code, making it very easy to spot the essentials and at the same time show the context of the code, making it clearer.

I use the listings package.

Any suggestions/pointers are appreciated. :P

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Berticus
Posts: 8
Joined: Wed Dec 31, 2008 11:37 pm

Code listing

Post by Berticus »

Like changing the background color of a text?

Code: Select all

\definecolor{yellow}{cmyk}{0,0,1,0}
\colorbox{yellow}{this is the highlighted text}
Preamble needs to have:

Code: Select all

\usepackage{color}
muusle
Posts: 3
Joined: Sat Jan 03, 2009 2:31 am

Code listing

Post by muusle »

Tanks for the reply.

Yes, it's simple as that; applying a background color to portion of the code (without loosing the highlighting).

Here is what I've got so far (screenshot):
Image

Code: Select all

\lstset{caption=Code listing; netbeans style}
\begin{lstlisting}
for(int i=0; i < 10; i++) {
	System.out.printf("Counting up, current: %d", i);
}
\end{lstlisting} 
This is what I want to do (in a somewhat elegant/easy way):
Image

I don't see how I'm gonna use the the colorbox method to do this, having to break back to latex interpretation from the literal mode the listings function operates in.

Suggestions?
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Code listing

Post by phi »

Here is an example:

Code: Select all

\begin{lstlisting}[language=Java, caption=Code listing; netbeans style, moredelim={**[il][\rlap{\textcolor{yellow}{\rule[-3pt]{\linewidth}{\baselineskip}}}]{$}}]
for(int i=0; i < 10; i++) {
$   System.out.printf("Counting up, current: %d", i);
}
\end{lstlisting}
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Code listing

Post by phi »

And a variant that adds exactly the same margin above and below the highlighted line:

Code: Select all

\newcommand*{\alphabet}{ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz}
\newlength{\highlightheight}
\newlength{\highlightdepth}
\newlength{\highlightmargin}
\setlength{\highlightmargin}{2pt}
\settoheight{\highlightheight}{\alphabet}
\settodepth{\highlightdepth}{\alphabet}
\addtolength{\highlightheight}{\highlightmargin}
\addtolength{\highlightdepth}{\highlightmargin}
\addtolength{\highlightheight}{\highlightdepth}
\newcommand*{\highlightline}{\rlap{\textcolor{yellow}{\rule[-\highlightdepth]{\linewidth}{\highlightheight}}}}


\begin{document}

\begin{lstlisting}[language=Java, caption=Code listing; netbeans style, moredelim={**[il][\highlightline]{$}}]
muusle
Posts: 3
Joined: Sat Jan 03, 2009 2:31 am

Re: Code listing

Post by muusle »

Thanks! That was exactly what I was looking for! :)
Post Reply