# tex4ebook - Change encoding of output to latin1

My tex4ebook ebook is formated in utf8. However, in the Kindle preview, characters are not shown correctly. Indeed, Kindle publishing seems to require latin1 encoding ... ( https://kdp.amazon.com/en_US/help/topic/G200652310 ) .

If I put \usepackage[latin1]{inputenc} in the tex header, the tex4ebook output (xhtmls) is still in utf8 (independently of the tex file encoding). If I convert the epub to latin1 with calibre, the result is also not satisfying.

Is there are way to tell tex4ebook to create latin1 output? Or should I manually convert all xhtmls and then rebuild the epub somewhow?

Kind regards

• It is really strange. Isn't this document obsolete? It would mean that you cannot have Kindle ebooks in languages that don't support Latin 1. I am pretty sure that there are Kindle ebooks in Czech, that contains characters besides Latin 1. Moreover, Epub requires UTF-8 output, so tex4ebook always produced this encoding by default. It should be possible to support Latin 1 if it was really necessary, for example using iconv, but I think the real issue must be something else. Jan 12, 2021 at 19:19

I don't think this issue is caused by UTF-8 encoding, I am pretty sure that it is supported by Kindle. But anyway, it is not hard to change the encoding of the HTML file, using make4ht build file and filters.

Save the following code as mybuild.lua:

local filter = require "make4ht-filter"

local process = filter {
function(s)
return s:gsub("charset=utf%-8", "charset=ISO 8859-1")
end
}
Make:match("html$", "iconv -f utf-8 -t latin1${filename}")
Make:match("html$", process)  The two Make:match commands will modify every output HTML file. Make:match("html$", "iconv -f utf-8 -t latin1 ${filename}")  This command converts HTML file from utf8 to latin 1 using the Iconv command. Make:match("html$", process)


This command calls the process function to modify the HTML file. We need to change the encoding that is specified in the HTML file. We can use just simple Lua string substitution command:

local process = filter {
function(s)
return s:gsub("charset=utf%-8", "charset=ISO 8859-1")
end
}


The filter command is provided by make4ht-filter library and it enables to chain multiple functions that changes contents of the processed file and then saves it.

Compile using

tex4ebook -e mybuild.lua filename.tex


Sample TeX source:

\documentclass{article}
\begin{document}
diacritics: òíéú
\end{document}


Resulting HTML file:

<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns='http://www.w3.org/1999/xhtml'>
<meta content='text/html; charset=ISO 8859-1' http-equiv='Content-Type' />
<meta content='TeX4ht (https://tug.org/tex4ht/)' name='generator' />
<meta content='TeX4ht (https://tug.org/tex4ht/)' name='originator' />
<!--  xhtml,charset=ISO 8859-1,epub,uni-html4,html  -->
<meta content='sample.tex' name='src' />