Discussion:
listings package with latex escape character
Add Reply
David Topham
2018-11-24 19:27:59 UTC
Reply
Permalink
I use the listings package by including source code via \lstinputlisting then within the source code file I can add latex comments because I set a
\lstset{escapechar=@}

i.e. Inside the source file (cpp) I can do this:

/*@ comments including Latex commands @*/

This works well except for one thing, the paragraph flow does not work.
i.e. I must use \space ( or \ ) at end of each line or the words get appended.

/*@ comments including Latex commands if more than one
line do not split correctly.
@*/

produces:
comments including Latex commands if more than oneline do not split correctly.

Is there a setting to allow latex to do its paragraph magic in those escaped comments?
Muzzi
2018-11-25 07:07:46 UTC
Reply
Permalink
Post by David Topham
I use the listings package by including source code via \lstinputlisting then within the source code file I can add latex comments because I set a
This works well except for one thing, the paragraph flow does not work.
i.e. I must use \space ( or \ ) at end of each line or the words get appended.
line do not split correctly.
@*/
comments including Latex commands if more than oneline do not split correctly.
Is there a setting to allow latex to do its paragraph magic in those escaped comments?
In your code, since all code between two "@" characters is interpreted as LaTeX code, the "newline" is also interpreted as "space" character, as this is what LaTeX normally do.

If you want to have LaTeX code not to interpret "newline" into "space", use \obeylines instead.

For example:

/*@ \obeylines comments including Latex commands if more than one
line do not split correctly.
@*/

will produce

/* comments including Latex commands if more than one
line do not split correctly.
*/
David Topham
2018-11-25 16:18:24 UTC
Reply
Permalink
Post by Muzzi
Post by David Topham
I use the listings package by including source code via \lstinputlisting then within the source code file I can add latex comments because I set a
This works well except for one thing, the paragraph flow does not work.
i.e. I must use \space ( or \ ) at end of each line or the words get appended.
line do not split correctly.
@*/
comments including Latex commands if more than oneline do not split correctly.
Is there a setting to allow latex to do its paragraph magic in those escaped comments?
If you want to have LaTeX code not to interpret "newline" into "space", use \obeylines instead.
line do not split correctly.
@*/
will produce
/* comments including Latex commands if more than one
line do not split correctly.
*/
Post by David Topham
I use the listings package by including source code via \lstinputlisting then within the source code file I can add latex comments because I set a
This works well except for one thing, the paragraph flow does not work.
i.e. I must use \space ( or \ ) at end of each line or the words get appended.
line do not split correctly.
@*/
comments including Latex commands if more than oneline do not split correctly.
Is there a setting to allow latex to do its paragraph magic in those escaped comments?
If you want to have LaTeX code not to interpret "newline" into "space", use \obeylines instead.
line do not split correctly.
@*/
will produce
/* comments including Latex commands if more than one
line do not split correctly.
*/
Post by David Topham
I use the listings package by including source code via \lstinputlisting then within the source code file I can add latex comments because I set a
This works well except for one thing, the paragraph flow does not work.
i.e. I must use \space ( or \ ) at end of each line or the words get appended.
line do not split correctly.
@*/
comments including Latex commands if more than oneline do not split correctly.
Is there a setting to allow latex to do its paragraph magic in those escaped comments?
If you want to have LaTeX code not to interpret "newline" into "space", use \obeylines instead.
line do not split correctly.
@*/
will produce
/* comments including Latex commands if more than one
line do not split correctly.
*/
Post by David Topham
I use the listings package by including source code via \lstinputlisting then within the source code file I can add latex comments because I set a
This works well except for one thing, the paragraph flow does not work.
i.e. I must use \space ( or \ ) at end of each line or the words get appended.
line do not split correctly.
@*/
comments including Latex commands if more than oneline do not split correctly.
Is there a setting to allow latex to do its paragraph magic in those escaped comments?
If you want to have LaTeX code not to interpret "newline" into "space", use \obeylines instead.
line do not split correctly.
@*/
will produce
/* comments including Latex commands if more than one
line do not split correctly.
*/
Post by David Topham
I use the listings package by including source code via \lstinputlisting then within the source code file I can add latex comments because I set a
This works well except for one thing, the paragraph flow does not work.
i.e. I must use \space ( or \ ) at end of each line or the words get appended.
line do not split correctly.
@*/
comments including Latex commands if more than oneline do not split correctly.
Is there a setting to allow latex to do its paragraph magic in those escaped comments?
If you want to have LaTeX code not to interpret "newline" into "space", use \obeylines instead.
line do not split correctly.
@*/
will produce
/* comments including Latex commands if more than one
line do not split correctly.
*/
Thank you Muzzi, but that does not seem to work for me still. Here is the code I have:
in lab.tex
\documentclass{article}
\usepackage{listings}
\lstset{escapechar=\@,language=C++,keywordstyle=\color{blue}}
\begin{document}
\lstinputlisting{lab.h}
\end{document}
and in lab.h
/*@ \obeyspaces
Function declarations, constants, and global variables
that are used within several modules. The {\bf extern} keyword
prevents multiple definition errors.
@*/
but result in pdf is:
/∗ Function declarations, constants, and global variablesthat are used within
several modules. The extern keywordprevents multiple definition errors.∗/
David Topham
2018-11-25 16:25:25 UTC
Reply
Permalink
Post by Muzzi
Post by David Topham
I use the listings package by including source code via \lstinputlisting then within the source code file I can add latex comments because I set a
This works well except for one thing, the paragraph flow does not work.
i.e. I must use \space ( or \ ) at end of each line or the words get appended.
line do not split correctly.
@*/
comments including Latex commands if more than oneline do not split correctly.
Is there a setting to allow latex to do its paragraph magic in those escaped comments?
If you want to have LaTeX code not to interpret "newline" into "space", use \obeylines instead.
line do not split correctly.
@*/
will produce
/* comments including Latex commands if more than one
line do not split correctly.
*/
That works great Muzzi; thank you very much!

Loading...