Skip to content

Instantly share code, notes, and snippets.

@robertoaloi
Created July 27, 2012 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertoaloi/3187943 to your computer and use it in GitHub Desktop.
Save robertoaloi/3187943 to your computer and use it in GitHub Desktop.
Highlighting lines in code listings
Compiling it with:
latexmk -pdf main.tex
/* hello.c -- The most famous program of them all ..
*/
#include <stdio.h>
int main(void) {
printf("Hello World!\n");
// return 0;
}
\documentclass [11pt,oneside,onecolumn]{article}
\usepackage{my}
\begin{document}
\code[4]{hello.c}
\end{document}
\usepackage{listings}
\usepackage{tikz}
\usepackage{xifthen}
\lstset{
numbers=left,
basicstyle=\footnotesize,
aboveskip=0pt,
belowskip=0pt,
showlines = true
}
\lstdefinestyle{highlight}{
backgroundcolor=\color{orange}
}
\newcounter{linefrom}
\newcounter{lineto}
\def\code{\@ifnextchar[{\@with}{\@without}}
\def\@with[#1]#2{
\setcounter{linefrom}{1}
\setcounter{lineto}{1}
\foreach \x in {#1}{
\ifthenelse{\equal{\x}{\thelinefrom}}{
\begingroup\edef\y{\endgroup
\noexpand\lstinputlisting[linerange=\thelinefrom-\thelineto,style=highlight,firstnumber=last]}%
\y{#2}
\addtocounter{linefrom}{1}
}{
\setcounter{lineto}{\x}
\addtocounter{lineto}{-1}
\begingroup\edef\y{\endgroup
\noexpand\lstinputlisting[linerange=\thelinefrom-\thelineto,firstnumber=last]}%
\y{#2}
\addtocounter{lineto}{1}
\setcounter{linefrom}{\value{lineto}}
\begingroup\edef\y{\endgroup
\noexpand\lstinputlisting[linerange=\thelinefrom-\thelineto,style=highlight,firstnumber=last]}%
\y{#2}
\addtocounter{linefrom}{1}
}
}
\addtocounter{linefrom}{1}
\lstinputlisting[firstline=\the\value{linefrom},firstnumber=last]{
#2
}
}
\def\@without#1{
\lstinputlisting[]{
#1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment