Re: listings-package - Comment-delimiters in different font than comment-text



Just in case somebody else is interested:

I wrote:

Actually I wanted to get the comment-delimiters written in a
different font than the comment-text - like in the single-line-
comments.

In the listings-package you can specify the formatting of all
comments via the "commentstyle"-key and the formatting of special
kinds of comments via some optional argument which can be passed
with the value of the "morecomments"-key.

I *think* I found---after reading in the listings-developers-
guide---a solution for modifying those comments which get their
formatting from the "commentstyle"-key:

Locally redefine within the AtBeginComment-hook the macros
\lst@DelimOpen and \lst@DelimClose which actually print the
delimiter.

I've introduced another key whose name is "commenttextstyle".
If you don't specify it or specify it equal to "commentstyle",
both comment-text and comment-delimiters will be formatted
as specified in "commentstyle". Otherwise comment-text will
be formatted according to what is given in "commenttextstyle"
and comment-delimiters will be formatted according to what is
given in "commentstyle".

All this does not apply to comments which get their formatting
provided through the "morecomments"-key.

I give no warranties of any kind as this was the first time that
I took a closer look at the internals of the listings-package.
Also this is not extensively tested.

Ulrich


\documentclass{article}
\usepackage{listings}

\begin{filecontents*}{mylisting.txt}
aA/ A single comment-line.
bB/ A comment spanning multiple comment-lines.
The comment-delimiters are not nestable.
The comment-delimiters are not nestable. /Bb
cC/ A comment spanning multiple comment-lines.
cC/ The comment-delimiters are nestable.
cC/ More nesting. /Cc /Cc
/Cc
dD/ A comment spanning one line.
dD/ Not a comment spanning one line.
\end{filecontents*}

\makeatletter
\lst@Key{commenttextstyle}\relax{%
{\def\@tempa{#1}\def\@tempb{commentstyle}%
\expandafter}\ifx\@tempa\@tempb
\let\lst@commenttextstyle\lst@commentstyle
\else
\def\lst@commenttextstyle{#1}%
\fi}%
\lst@AddToHook{AfterBeginComment}{%
\@ifundefined{lst@commentdelimstyleback}{%
\let\lst@commentdelimstyle\lst@commentstyle
\@ifundefined{lst@commenttextstyle}{}{%
\let\lst@commentstyle\lst@commenttextstyle
}%
\def\lst@commentdelimstyleback{%
\let\lst@commentstyle\lst@commentdelimstyle
}%
}{}%
\def\lst@DelimOpen##1##2##3##4##5##6\@empty{%
\lst@TrackNewLines \lst@XPrintToken
\lst@DelimPrint##1{\lst@commentdelimstyleback##6}%
\lst@EnterMode{##4}{\def\lst@currstyle##5}%
\lst@DelimPrint{##1##2}{\lst@commentdelimstyleback##6}%
##3}%
\def\lst@DelimClose##1##2##3\@empty{%
\lst@TrackNewLines \lst@XPrintToken
\lst@DelimPrint{##1##2}{\lst@commentdelimstyleback##3}%
\lst@LeaveMode
\lst@DelimPrint{##1}{\lst@commentdelimstyleback##3}}%
}%
\makeatother

\begin{document}
text\xleaders\hbox{ text}\hfill\null
\lstinputlisting[%
language={[iso]C++},
basicstyle=\ttfamily,
morecomment={[l]{aA/}},
morecomment={[s]{bB/}{/Bb}},
morecomment={[n]{cC/}{/Cc}},
morecomment={[f][commentstyle][3]{dD/}},
commentstyle=\rmfamily\itshape,
commenttextstyle=\rmfamily\scshape,
columns=fixed,
]{mylisting.txt}
text\xleaders\hbox{ text}\hfill\null
\end{document}
.



Relevant Pages

  • Re: find and replace
    ... is still note finding words in my footnotes. ... then specify the text or formatting in the Find what box. ... Specify the formatting correctly. ... through only the main document (not headers (header and footer: ...
    (microsoft.public.word.docmanagement)
  • Re: find and replace
    ... case it will go through all the footnotes and then stop, ... > then specify the text or formatting in the Find what box. ... Microsoft Word displays the comment in a balloon in the ...
    (microsoft.public.word.docmanagement)
  • Re: K&R2 ex 1-8
    ... Which you should specify in the body, ... > int c,n1,n2,n3; ... complain about unused parameters. ... The formatting is purely a matter of style. ...
    (comp.lang.c)
  • Re: find and replace
    ... things would find words in the main body and footnotes. ... >> then specify the text or formatting in the Find what box. ... A footer appears at the bottom of every page. ...
    (microsoft.public.word.docmanagement)
  • Re: word document recover
    ... You should be able to at least get the unformatted text, if you specify ... With luck, the text including the formatting might ... Regards, ...
    (microsoft.public.word.docmanagement)