0

I want to italicize a part of a single line comment, for example:

string new = old; // new is a *copy* of old 

In the above, I want "copy" to be italicized, while keeping what ever formatting the comments already have (i.e. the font size, color, etc...).

I've tried using moredelim but it does not work inside comments. Any help would be greatly appreciated, thank you.

1
  • 2
    Welcome! Please make you snippet into a complete minimal document we can compile to reproduce the issue. Right now, the code isn't even related to TeX. We don't know what TeX code you are trying to use at all. Commented May 23, 2017 at 1:31

2 Answers 2

3

As you are using tag , I assume you are using package listings. With listings, you can use escapechar=* to escape from the listing to LaTeX and escapebegin=\itshape to make the escaped part italic:

\documentclass{article} \usepackage{listings} \begin{document} \begin{lstlisting}[escapechar=*,escapebegin=\itshape] string new = old; // new is a *copy* of old \end{lstlisting} \end{document} 

escaped, italic "copy"

See the manual for more information about escaping.

3

If you redefine the commenting mechanism for this style of comment, then you can make its style accumulative (with a *) and apply another style afterwards:

Sample output

\documentclass{article} \usepackage{listings} \begin{document} \lstset{language=C,morecomment=*[l][]{//},moredelim=*[is][\itshape]{*}{*}} \begin{lstlisting} string new = old; // new is a *copy* of old \end{lstlisting} \end{document} 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.