[Tex/LaTex] italic font in lmodern-lighttt

fontslistings

In a search for a good monospaced font for my code listings I found the lmodern fonts. I really like the lighttt variant of those. The difference between normal and bold version is clearly visible with the ttfamily.

Recently however I discovered I also need italic version of those. Unfortunately the following:

\documentclass{article}
\usepackage[lighttt]{lmodern}

\begin{document}
Abc {\ttfamily abc {\bfseries abc} {\itshape abc}}
\end{document}

throws an error:

! No declaration for shape OT1/lmtt/l/sl

Is there a way to enable italic in this font?


With a bit of experimentation I soon discovered, that while italic is not available, it is possible to use the slanted font. \slshape works with lmodern-lighttt-ttfamily well enough.

However, while in the above MWE I use the \itshape explicitly and can be easily exchanged with \slshape, in my real scenario it is not always the case.

  • I use listings package which automatically uses italic font in certain scenarios (e.g. comments in code)
  • Sometimes I put short pieces of code in a caption, which – as a whole – is italic (and shouldn't use slanted)

A quick hack: \renewcommand{\itshape}{\slshape} is not reasonable in longer run either as it changes all italic fonts to slanted, while my concern is only in the monospaced case – when typing source code.

Maybe there is a way to treat \itshape as \slshape only in the context of monospaced font?

Best Answer

That's a bug in the fd-files (OT1lmtt.fd and T1lmtt.fd): In the "lighttt" section they substitute the lmtt/m/it to lmtt/l/sl instead of lmtt/m/sl. You should report it.

You can get around it with

\documentclass{article}

\usepackage[lighttt]{lmodern}
\ttfamily
\DeclareFontShape{OT1}{lmtt}{m}{it}
     {<->sub*lmtt/m/sl}{}

\begin{document}
Abc {\ttfamily abc  \itshape abc}
\end{document}
Related Question