Skip to content

Instantly share code, notes, and snippets.

@robertoaloi
Created July 27, 2012 13:20
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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}
\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}
\foreach \x in {#1}{
\setcounter{lineto}{\x}
\addtocounter{lineto}{-1}
\lstinputlisting[linerange={\thelinefrom-\thelineto}]{
#2
}
\addtocounter{linefrom}{\x}
\addtocounter{linefrom}{-1}
\addtocounter{lineto}{1}
\lstinputlisting[linerange={\thelinefrom-\thelineto},style=highlight,firstnumber=last]{
#2
}
\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