1

I'm searching a way to correctly include one piece of C code into Latex document. However, it contains some unusual, but valid structures, including a two-line-comment. Minted handles it right, but its's output has some other problems. The main problem is that "spanned into two lines" does not appear as a comment line. Here is MWE

\documentclass{article}
\usepackage{listings}
\usepackage[dvipsnames]{xcolor}
\begin{document}
\lstset{language=C,
commentstyle=\color{NavyBlue}}
\begin{lstlisting}
int main(void) {
    //one line comment\
    spanned into two lines
    return 0;
}
\end{lstlisting}
\end{document}
Timppa
  • 31

2 Answers2

1

If you can switch to minted, the two-line comment is correctly parsed (David, in his comment, thought you were using it).

\documentclass{article}
\usepackage{minted}
\begin{document}
    \begin{minted}{c}
    int main(void) {
    //one line comment\
    spanned into two lines
    return 0;
    }
    \end{minted}
\end{document}

enter image description here

To know the differences between listings and minted see here: minted vs. listings: pros and cons.

CarLaTeX
  • 62,716
1

Because there were no easy solution, I cheated a little bit. I prefixed those problematic comment lines with '@' and added to \lstset

moredelim=[il][\color{NavyBlue}]{\@}

And this did the trick.

Timppa
  • 31