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.

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.