# linerange reference in lstinputlisting

I have a listing that refers to my c code; I would like to reference the code using commented section in the code, as seen in the code example bellow.

I have created a list style which does this, but if I try to hide the range markers, it can’t end the range. I have concluded that if I delete the comment from the language. It works as well, but I would like the section to still be a comment section.

In the included example, three types of using the listing is encluded, to try and highlight the issue.

\documentclass[a4paper,10pt,oneside]{memoir}
\usepackage{listings}
\usepackage{lipsum}
\usepackage{filecontents}

\lstdefinestyle{c}{
breaklines=true,%
rangeprefix={\/\/\ \{\ },% curly left brace plus space
rangesuffix={\ \}},% space plus curly right brace
rangebeginsuffix={\ start},
rangeendsuffix={\ end},
includerangemarker=false
}

\lstdefinelanguage{nocomc}[]{c}
{sensitive=false,
deletecomment=[s]{/*}{*/},
}

\begin{document}

\begin{filecontents*}{foo.c}
#define one 1
#define two 2
#define three 3
#define four 4

// { main start }
/*----------------------------------------------------------
* Namme        : turnBehaviorCan
* Description  :
*        The pathfinding of the robot
*
* Parameters   : None
* Return Value : None
*----------------------------------------------------------*/
int main(){
do stuff
}
// { main end }

// { main2 start }
void main2(){
do stuff
}
// { main2 end }
\end{filecontents*}}

\lstinputlisting[
language=nocomc,
style=c,
linerange={main}] {foo.c}

\lstinputlisting[
language=c,
style=c,
linerange={main}] {foo.c}

\lstinputlisting[
language=c,
style=c,
includerangemarker=true,
linerange={main}] {foo.c}

\end{document}

-
Since this post i have tested further and so far i have only seen the problem when using the c language. I have used it without problems in VHDL and C# (this is done by setting language=[Sharp]C in the style). –  Jonhen Oct 21 '12 at 22:40

Having narrowed it down further i have concluded that the problem is caused by the use of a multi-line comment. If the comments on the function description is replaced by // on every line instead of a single /* */ it also works. I don't know it this is a bug or a constraint of the latex itself!

The following code works in all three cases.

\documentclass[a4paper,10pt,oneside]{memoir}
\usepackage{listings}
\usepackage{lipsum}
\usepackage{filecontents}

\lstdefinestyle{c}{
breaklines=true,%
rangeprefix={\/\/\ \{\ },% curly left brace plus space
rangesuffix={\ \}},% space plus curly right brace
rangebeginsuffix={\ start},
rangeendsuffix={\ end},
includerangemarker=false
}

\lstdefinelanguage{nocomc}[]{c}
{sensitive=false,
deletecomment=[s]{/*}{*/},
}

\begin{document}

\begin{filecontents*}{foo.c}
#define one 1
#define two 2
#define three 3
#define four 4

// { main start }
//----------------------------------------------------------
// Namme        : turnBehaviorCan
// Description  :
//        The pathfinding of the robot
//
// Parameters   : None
// Return Value : None
//----------------------------------------------------------
int main(){
do stuff
}
// { main end }

// { main2 start }
void main2(){
do stuff
}
// { main2 end }
\end{filecontents*}}

\lstinputlisting[
language=nocomc,
style=c,
linerange={main}] {foo.c}

\lstinputlisting[
language=c,
style=c,
linerange={main}] {foo.c}

\lstinputlisting[
language=c,
style=c,
includerangemarker=true,
linerange={main}] {foo.c}

\end{document}


It is not the best solution, but it is the best workaround i have found!

-