Contents | Start | End | Previous: KB0367: Why are boxes cut in half on Kindle? | Next: KB0369: Why do otherwise blank paragraphs show a hyphen, dash or underline when previewed?


KB0368: Monospace code formatting is not working with Kindle enhanced typesetting

Normally, you can ensure your ebook reader knows about fixed-width fonts such as Courier New by setting the configuration option Styles with generic font names to Code* to catch all paragraph styles with the Code prefix, and emit the monospace generic font family.

Or, you can set the option Preformatted paragraph styles to Code* to make all code paragraphs preformatted.

However, Kindle Previewer 3 (and probably books with enhanced typesetting on other viewers) ignores the monospace font family - and even the 'pre' HTML tag. This appears to be one of a number of KP3/enhanced typesetting bugs. A workaround for this is to use an embedded font for the Kindle version of your book. Here are the steps:

  1. Download a suitable monospace font, such as www.fontsquirrel.com/fonts/Bitstream-Vera-Sans-Mono.

  2. Add it to your Jutoh project via the menu command Format | Book | Embed Fonts or by adding a font document from the Documents toolbar button.

  3. When prompted, check the configurations you want to use embedded fonts with, e.g. Kindle. For Font substitutions, enter Courier New:VeraMono,monospace (use the appropriate embedded font name instead of VeraMono).

Assuming your project uses Courier New for monospace, the embedded font should now be used for your code formatting, and it will appear using a fixed-width font in Kindle Previewer 3 and other viewers.

Note that as of September 2020, using a monospace font such as "Courier New" and enabling the configuration option Generate generic font names does now show a monospace font in Kindle Previewer 3, but you may still want to embed a font to be sure this works on all platforms, and also so you know exactly which font will be displayed.

If you use spaces to create columns of data, for example, multiple spaces may still not be respected. Since HTML normally ignores multiple spaces, you need to replace the spaces with non-breaking ('hard') spaces. You could copy and paste a hard space that you inserted with Format | Insert | Symbols | Non-Breaking Space into the Find and Replace dialog, but an easier method is to use the code formatter.

Select the text with spaces you wish to replace, and choose Format | Insert | Code. This will give you the option of applying styles to the selected text, and also replacing spaces with hard spaces. Select Replace all spaces, and click OK. With a monospace font and hard spaces, this should now show preformatted text correctly in all e-readers.


Contents | Start | End | Previous: KB0367: Why are boxes cut in half on Kindle? | Next: KB0369: Why do otherwise blank paragraphs show a hyphen, dash or underline when previewed?