3

I am trying to italicize variable names inside \verb | |.

I tried \cprotect\textit{\verb| |}, but it doesn't work.

I also tried \emph\texttt{variable names}. However, it does not italicize variable names that are separated by an underscore. For example: x_LT

\verb |x_LT| 
  • 3
    are you sure you want to be using \verb the whole point of \verb is that no latex command are active there. To get a _ you can use \_ – David Carlisle Apr 20 '20 at 21:11
  • Okay, you are saying it can't be done with at all in the \verb || environment. Okay, I will use _ as follows: \emph{\texttt(x_LT)} – Hrish Desai Apr 20 '20 at 21:16
  • I think you are saying this is the only option, right? – Hrish Desai Apr 20 '20 at 21:17
  • 2
    latex is open source macro processor it's always possible to redefine anything but sometimes it's the wrong thing to do. Also \textit better than \emph here – David Carlisle Apr 20 '20 at 21:18
  • Thank you @DavidCarlisle I will use \textit then – Hrish Desai Apr 20 '20 at 21:31
  • It depends what you're actually trying to do. There are various ways to format source code. The listings package provides one way. See e.g. How to format an inline source code and Emphasizing words within a string using the listings package in LaTeX for some examples. – Alan Munn Apr 20 '20 at 21:41
  • @AlanMunn I just want variable names in my paper to be formatted math style compared to rest of the text that is in Roman – Hrish Desai Apr 20 '20 at 21:47
  • You wrote, "I just want variable names in my paper to be formatted math style". In that case, why are you using text mode commands -- such as \texttt, \emph and \textit -- and \verb instead of placing the material in question into math mode, e.g., $x_{LT}$? Please peruse any Intro-to-LaTeX tutorial to familiarize yourself with the basics of text and math mode. – Mico Apr 20 '20 at 21:51
  • 1
    Hi @Mico, I was initially trying to format the variable names in a different font (esp. typewriter font). But, now I decided to do them math style. I have got information on Math Mode from tex stackexchange. So, I will be doing it now – Hrish Desai Apr 20 '20 at 21:53
  • Fundamentally you just want to change the font used by verbatim (when I look at the accepted answer). As such, this is a duplicate of Change font size of the verbatim environment. – Werner Apr 21 '20 at 04:22
  • 1
    Disagree with Werner and the decision to close. The solution may be related but the questions are far from being duplicates. Moreover, the comments here bring up issues of style for how best to display variables (and also talk of listings package that is more fit for the other question). Comments on the answer bring up syntax of \verb unrelated to the other question. Please re-open. – Donald Arseneau Apr 21 '20 at 23:52
  • @DonaldArseneau the questions 'how to make \verb italic' and 'how to make \verb small' are not exact duplicates, but they are very close. Additionally, the solutions are the same (modify \verbatim@font). Therefore, it makes sense to close, even if the discussion and the answers bring up additional relevant details. As Stack Exchange founder Jeff Atwood puts it: – Marijn Apr 22 '20 at 08:06
  • 'There are similar questions, yes, and so-called “exact” duplicates do happen, but they are kind of rare in my experience. It’s far more common to have many subtle variations of a question. [...] What we want is on the order of 4 or 5 similar-but-not-quite-the-same duplicates to cover all possible search terms and common permutations of the question. It is also OK for these duplicates to have their own answers so people who find them don’t have to click yet again to get to a good answer.' – Marijn Apr 22 '20 at 08:06
  • Well, the other wasn't how to make \verb small, but the verbatim environment. Three of the four answers suggest different environments, which are inappropriate here. Changing the font size also has different ancillary considerations from changing font entirely, but there are many questions on shrinking verbatim environment font, not closed.... That said, I found a close match to this one: how-to-globally-set-verb-font-style-to-match-the-default-document-style, but it did not pop out of search easily! – Donald Arseneau Apr 22 '20 at 09:25

1 Answers1

9

The problem is simple, on the surface, because LaTeX has an internal command \verbatim@font to select the font for verbatim text. So

\makeatletter \renewcommand\verbatim@font{\normalfont\itshape}

Some nuance arises on the more traditional versions of LaTeX where the default fonts have some characters missing or rearranged; it is called the "OT1" font encoding, where "O" is for "old" or "original". So, if you are running lualatex or xelatex, the one line setting above is sufficient. If you are running latex or pdflatex, you need to change the font encoding, probably with

\usepackage[T1]{fontenc}

Another idiosyncrasy is that the underscore character in many fonts looks too wide, at least to my eyes.

Also something about your question with \verb |x_LT|. That may give an error because it will use the space character for quoting the argument. You should use \verb|x_LT|.