3

Edit: this question is misinformed and the chosen answer answers a different question altogether.

\emph produces slightly better italicized text for certain symbols, e.g., punctuation. Right now, I use \itshape to force a style on a block of text.

\lstdefinelanguage{Scheme}{
  % basewidth=0.5em,
  sensitive=true,
  basicstyle=\normalsize\rmfamily,
  commentstyle=\itshape\rmfamily\color[rgb]{.9, .5,.1},
  keywordstyle=\bfseries\sffamily\color[rgb]{0,.3,.7},
  morecomment=[l]{;},
  frameshape={RYR}{Y}{Y}{RYR},
  identifierstyle=\emph\rmfamily,
  keywords={define, lambda, let, cond, else},
  escapeinside={(*;}{;*)},
  columns=fullflexible
}

The problem is that I want to use \emph for "comments" in my listing and not just \itshape. Is there something similar that I can use (because I'd rather not have to escape and unescape every instance where I want to use \emph)?

Edit: it seems that \em is what I'm looking for, but my problem is more involved than that. The problem is that the identifierstyle doesn't italicize question marks using the above listing. See the image and code listing below.

\documentclass{article}
\usepackage{tcolorbox}

\tcbuselibrary{listings, skins, breakable} \definecolor{myorange}{rgb}{0.9, 0.5, 0.1} \lstdefinelanguage{Scheme}{ % basewidth=0.5em, sensitive=true, basicstyle=\normalsize\rmfamily, commentstyle=\itshape\rmfamily\color[rgb]{.9, .5,.1}, keywordstyle=\bfseries\sffamily\color[rgb]{0,.3,.7}, morecomment=[l]{;}, frameshape={RYR}{Y}{Y}{RYR}, identifierstyle=\em\rmfamily, keywords={define, lambda, let, cond, else}, escapeinside={(;}{;)}, columns=fullflexible } \begin{document} \begin{lstlisting}[language=Scheme] ;; rember(;\textcolor{myorange}{\textrm{:}};) (;\textcolor{myorange}{\textsf{Atom}};) (;\textcolor{myorange}{\textsf{[ListOf Atom]}};) (;\textcolor{myorange}{$\to$};) (;\textcolor{myorange}{\textsf{Atom}};) ;; Comment here... (define rember ((;$\lambda$;) (a lat) (cond [(null? lat) (quote ())] [else (cond [(eqv? (car lat) a) (cdr lat)] [else (rember a (cdr lat))])]))) \end{lstlisting} \end{document}

enter image description here

TheProgrammer
  • 404
  • 2
  • 11

3 Answers3

5

Unless you redefine \emph{}, by default it uses \em internally, that uses \itshape internally, so both commands should produce exactly the same italics:

\emph definition:

\ifmmode \nfss@text {\em #1}\else \hmode@bgroup \text@command {#1}\em \check@icl #1\check@icr \expandafter \egroup \fi

\em definition:

\@nomath \em \ifx \emfontdeclare@clist \@empty \ifdim \fontdimen \@ne \font >\z@ \eminnershape \else \itshape \fi \else \edef \em@currfont {\csname \curr@fontshape /\f@size \endcsname }\expandafter \do@emfont@update \emfontdeclare@clist \do@emfont@update \fi

The main difference, is that \emph, like \textit, corrects the space after the italics, while with \itshape or \em is your work make the so called "italic correction". That is, \emph{l}l or \textit{l}l will have some extra spacing between the two letters, compared with {\em l}l or {\itshape l}l, that are too close, and then is your work type {\em l\/}l or {\itshape l\/}l to avoid that.

Well, another important difference is that \emph or \em means "emphasis", not "italics", and therefore {\em l{\em l}l} will produce

mwe

while {\itshape l{\itshape l}l} will produce only italics, because it have sense emphasize a text in italics, but not italicize it again.

Therefore, is OK redefine \emph or better \em only if you to make emphasis with some that is not italics (e.g., bold or slant font) or return main font in nested emphasis, but you should avoid redefine \textit or\itshape because then the meaning of the commands are misleading and will produce unexpected results in any package using these commands.

As Mico warns in the comment, still one more difference is the math mode, fact that you can already suspect trying to decipher above the cryptic definitions, so you can type $\emph{x}$ or $\textit{x}$, but not ${\em x}$ nor ${\itshape x}}$.

Fran
  • 80,769
  • 2
    +1. Another difference between \emph and \em is that the former can be used while in mode, whereas the latter can not. (This difference also applies to \textit vs \itshape, \textsc vs \scshape, and so on.) Hence, $\emph{l}$ is ok, whereas ${\em l}l$ can not. Of course, nobody actually recommends writing ${\em l}l$... – Mico Mar 17 '23 at 05:49
  • 1
    @Mico Very true. Added to the answer. – Fran Mar 17 '23 at 06:30
  • 1
    @Mico The fact that \emph can be used in math mode is purely coincidental. – egreg Mar 17 '23 at 08:11
  • 1
    @egreg - Agreed. – Mico Mar 17 '23 at 10:04
  • 1
    @egreg or @Mico Can you explain? Why is coincidental with \emph but not in \textit, for instance? Both commands have a \nfss@text{...}, that as far I see, is like put \em or \itshape in a \mbox, so? – Fran Mar 17 '23 at 11:14
  • 1
    @Fran You do want to be able to use \textit or similar commands in math (think to cases, for instance), but \emph doesn't really make much sense in this context. – egreg Mar 17 '23 at 11:37
  • 1
    @egreg Oh, I understand, in the semantic sense... I thought you refer to some kind of code serendipity in \emph definition, haha. Thanks for the clarification. – Fran Mar 17 '23 at 19:46
2

The corresponding switch for \emph is \em.

So instead of \itshape you can use \em.

Mico
  • 506,678
Mane32
  • 1,252
1

Listings will print the identifiers in italic, but the ? is not part of the identifiers.

You can force listings to print it in italic by making it a normal letter:

\documentclass{article}
\usepackage{tcolorbox}

\tcbuselibrary{listings, skins, breakable} \definecolor{myorange}{rgb}{0.9, 0.5, 0.1} \lstdefinelanguage{Scheme}{ basewidth=0.5em, sensitive=true, basicstyle=\normalsize\rmfamily, commentstyle=\itshape\rmfamily\color[rgb]{.9, .5,.1}, keywordstyle=\bfseries\sffamily\color[rgb]{0,.3,.7}, morecomment=[l]{;}, frameshape={RYR}{Y}{Y}{RYR}, identifierstyle=\itshape\rmfamily, alsoletter={?}, keywords={define, lambda, let, cond, else}, escapeinside={(;}{;)}, columns=fullflexible } \begin{document}

\begin{lstlisting}[language=Scheme] ;; rember(;\textcolor{myorange}{\textrm{:}};) (;\textcolor{myorange}{\textsf{Atom}};) (;\textcolor{myorange}{\textsf{[ListOf Atom]}};) (;\textcolor{myorange}{$\to$};) (;\textcolor{myorange}{\textsf{Atom}};) ;; Comment here... (define rember ((;$\lambda$;) (a lat) (cond [(null? lat) (quote ())] [else (cond [(eqv? (car lat) a) (cdr lat)] [else (rember a (cdr lat))])]))) \end{lstlisting}

\itshape null?

\end{document}

enter image description here

  • 1
    My only comment is that while this solves the problem, the question itself is perhaps misinformed and it makes sense for this answer to be attached to a new question which actually asks the correct question. – Don Hosek Mar 17 '23 at 20:02
  • @DonHosek I 100% agree that the question is misinformed. Should I create a new question, then post an answer, or should I let sam answer it? Not really sure what to do in this situation. – TheProgrammer Mar 17 '23 at 20:04
  • 1
    @TheProgrammer That's why your questions should always have a minimal working example which allow users to reproduce your problem. Disconnected code fragments are never a good idea. – samcarter_is_at_topanswers.xyz Mar 17 '23 at 20:09
  • 1
    @TheProgrammer I don't think a new question would be very useful, I'm pretty sure we already have some duplicate. Search the site for alsoletter. – samcarter_is_at_topanswers.xyz Mar 17 '23 at 20:10