10

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?

CygnusX1
  • 518
  • if you can extend your example to include some particular situations where \slshape may or should not be used (e.g. with the listings package and in captions), it will be easier to come up with a workaround. i know it's been done, but the approach has to be quite environment-specific. – barbara beeton Mar 19 '15 at 13:54
  • I hope there is a solution that would be environment independent - e.g. defining globally ttfamily+itshape as ttfamily+slshape. – CygnusX1 Mar 19 '15 at 14:11

1 Answers1

13

Update 2023

in a current texlive 2023 the bug has been corrected and this works without error (and gives a slanted typewriter)

\documentclass{article}

\usepackage[lighttt]{lmodern}

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

enter image description here

0ld 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}

Ulrike Fischer
  • 327,261
  • This solves my issue! In my real document I use \usepackage[T1]{fontenc}. In the line DeclareFontShape I used {T1} instead of {OT1} and it works fine. – CygnusX1 Mar 19 '15 at 15:11
  • I am reluctant to report the bug to the creators though, since I know very little about inner workings of the package and the font system. The \DeclareFontShape... line is "black magic" for me. – CygnusX1 Mar 19 '15 at 15:12
  • The authors won't bite you. You can sent them the link to your question and my answer and if they really don't know what I mean they can contact me. – Ulrike Fischer Mar 19 '15 at 15:15
  • @UlrikeFischer I reported this bug on January 11, 2011 (sic) in a mail with destinatories Boguslaw Jackowski and Karl Berry (as I was using TeXLive and wasn't aware of how TeX/LaTeX world was structured). In my patched version I also use ssub rather than sub. There is also a problem with sb/it->m/sl which should be sb/it->sb/sl signaled in my patched version. –  Mar 19 '15 at 17:47
  • My mail started with: Dear Mr Jackowski [snip] it seems that some font shape substitutions are afflicted by typos in the files il2lmvtt.fd, l7xlmvtt.fd, ly1lmvtt.fd, ot1lmvtt.fd, ot4lmvtt.fd, qxlmvtt.fd, t1lmvtt.fd, t5lmvtt.fd, ts1lmvtt.fd ... [snip] –  Mar 19 '15 at 17:55
  • @jfbu: You could try to contact the CTAN team, or write to the texlive list. See Joseph's advice here: http://tex.stackexchange.com/a/232735/2388 – Ulrike Fischer Mar 20 '15 at 10:47
  • I've tried compiling this snippet but the font looks still slanted and not italic. It seems like the substitution is no longer working. – Atcold Apr 11 '23 at 20:05
  • @Atcold that it looks slanted is normal, that is a typewriter font. (the patch is no longer needed in a current texlive). – Ulrike Fischer Apr 11 '23 at 20:27
  • What you're saying makes no sense to me. The default LaTeX monospace font has an italic variant as well, and it's not simply a ‘slanted’ fake italic. – Atcold May 16 '23 at 21:01
  • I created this question where I can illustrate more clearly what's going on. Apologies for the succinctness of my previous message. – Atcold May 17 '23 at 18:52