Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
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

1 Answer 1

up vote 0 down vote accepted

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*}}

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}

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.