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*}}
Works without comments
\lstinputlisting[
language=nocomc,
style=c,
linerange={main}] {foo.c}
Fails with comments
\lstinputlisting[
language=c,
style=c,
linerange={main}] {foo.c}
Works with comments
\lstinputlisting[
language=c,
style=c,
includerangemarker=true,
linerange={main}] {foo.c}
\end{document}