Italicize part of single line comment

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.

• 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. – cfr May 23 '17 at 1:31

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:

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


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}