Created
July 27, 2012 13:20
-
-
Save robertoaloi/3187943 to your computer and use it in GitHub Desktop.
Highlighting lines in code listings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Compiling it with: | |
| latexmk -pdf main.tex |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* hello.c -- The most famous program of them all .. | |
| */ | |
| #include <stdio.h> | |
| int main(void) { | |
| printf("Hello World!\n"); | |
| // return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| \documentclass [11pt,oneside,onecolumn]{article} | |
| \usepackage{my} | |
| \begin{document} | |
| \code[4]{hello.c} | |
| \end{document} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| \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