1

How can I emphasize inline verbatim text? \emph{\verb|$Text|} creates an error. Nothing else I tried works either, e.g. \Verb. Is there any easy way to do this? I would need all kinds of special latex characters in the verbatim text (which contains program code fragments), not only "$".

Of course one can do \emph{\$Text}. But having to escape every single special character takes a lot of time and changes the highlighted program code fragment in the latex source file, which is not desirable.

2
  • 2
    Try \makeatletter\renewcommand\verbatim@font{\ttfamily}\makeatother and then use {\em \verb|$Text|} instead. Also, show us a compilable example of your document. Jan 22, 2020 at 15:57
  • @Phelype, this works. I tried it with all special characters I can think of at the moment. The drawback is, that in my text system (TeXStudio) the edit (autocomplete) macro for \verb does not work correctly any more and it becomes very complicated to type it. So basically I cannot use plain \verb any more. Not all inline verbatim text is to be highlighted. Jan 22, 2020 at 16:29

2 Answers 2

1

Use fancyvrb:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{fancyvrb}

\begin{document}

\Verb|$Text$|

\Verb[fontshape=it]|$Text|

\end{document}

enter image description here

4
  • A nice and simple solution, thanks a lot. However, I get the same problems with my editor (TeXStudio) as described for the answer by Steven B. Segletes: the editor gets skrewed up by taking the $ inside of \Verb as the opening of a math environment. Since this does not happen with \verb, I imagine this could be adapted in the editor somehow. Jan 23, 2020 at 10:38
  • @RolandSalz Ask the support of TeXStudio.
    – egreg
    Jan 23, 2020 at 11:53
  • @RolandSalz As far as I can see, \verb and \verb* are treated specially in the source code for the program, so I don't see a way to add other “verbatim” exceptions without a new version.
    – egreg
    Jan 23, 2020 at 12:03
  • Thanks a lot for your investigation. Well, let's leave the issue as it is for the moment. For the time being I will stick with escaping individual characters in a normal, emphasized text string. Your solution probably is the easiest one within the scope of latex, the rest is a text editor problem. Jan 23, 2020 at 18:37
2

Here is an approach with verbatimbox.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{verbatimbox}
\begin{document}
Here is emphasized
\begin{verbbox}[\itshape]
$Text\end{verbbox}
\theverbbox{}
to be shown inline.
\end{document}

enter image description here

4
  • \documentclass[a4paper,11pt,oneside]{book} \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc} \usepackage{lmodern} \usepackage{xspace} \usepackage{preview} \usepackage{verbatimbox} With the last line I get an error message: Package listofitems Error: The \expanded primitive is not provided by your TeX engine. What's wrong here? Jan 22, 2020 at 16:55
  • @RolandSalz The core of TeX/LaTeX was upgraded in December 2018 to make use of the \expanded command as a primitive. The listofitems package was upgraded to take advantage of that. However, if you still have an old engine, listofitems should load a prior version of itself that does not rely on \expanded. So, hopefully, the error message is more "informative" rather than "debilitating." Best solution: upgrade your LaTeX installation. See documentation at ctan.org/pkg/listofitems Jan 22, 2020 at 17:53
  • Thanks a lot for the solution and your hint. Now it works. But the next problem is, that my latex editor (TeXStudio) does not understand this construction properly and takes the $ inside of the verbbox as the beginning of a mathmode environment, with the end missing. Thus, the rest of the source code is screwd up in the editor. I am tempted to give up... Jan 22, 2020 at 20:15
  • @RolandSalz Working with verbatim can definitely wreak havoc on text editors that expect a non-verbatim syntax.. Jan 22, 2020 at 20:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .