8

I would like to set the \verb{} font so to correspond to the current family and size of my document. That is, the output must be indistinguishable by the rest of the text body whatever style is set as default.

2
  • 2
    What do you need \verb for, then?
    – egreg
    Sep 19, 2013 at 9:06
  • Good question. I need to cheat on my text editor. See here.
    – Francesco
    Sep 19, 2013 at 10:13

1 Answer 1

14

With the default font encoding OT1 you cannot switch the typewriter font to a normal text font, because of encoding issues (burden of compatiblity). The encoding issues are resolved in font encoding T1, for example. Then the font for \ttfamily can be changed to use the roman font instead, e.g.:

\documentclass{article}
\usepackage[T1]{fontenc}% NOT OT1!
\usepackage{lmodern}% Latin Modern fonts,
  % a modern variant of Computer Modern fonts
\let\ttdefault\rmdefault

\begin{document}
  Hello \texttt{World}! \verb|\relax %$&#|
\end{document}

Result

If you need to inherit the font settings from the context, then \verbatim@font can be redefined to do nothing. It is used for \verb and environment verbatim:

\documentclass{article}

\usepackage[T1]{fontenc}% NOT OT1
\usepackage{lmodern}

\makeatletter
\renewcommand*{\verbatim@font}{}
\makeatother

\begin{document}
  Hello \texttt{World}! \verb|\relax %$&#|

  {\sffamily Hello \texttt{World}! \verb|\relax %$&#|}

  {\itshape Hello \texttt{World}! \verb|\relax %$&#|}

  {\bfseries Hello \texttt{World}! \verb|\relax %$&#|}
\end{document}

Result

You must log in to answer this question.

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