# XeLaTeX produces gibberish with Devanagari document in TeXShop

I was able to process devanagari-documents into PDF successfully a couple of years ago on my Mac. I tried processing a simple document with xelatex but it doesn't work. My document is:

\documentclass{article}
\usepackage{fontspec}
\begin{document}
\setmainfont[Script=Devanagari]{Nakula}
नमस्ते रुद्र मन्यवे
\end{document}


Below is an image of all the fonts that are installed on my Mac (running Sierra 10.12.1)

Here's log file displayed in TeXshop's console:

This is XeTeX, Version 3.14159265-2.6-0.99992 (TeX Live 2015) (preloaded format=xelatex)
restricted \write18 enabled.
entering extended mode
(./Untitled.tex
LaTeX2e <2015/01/01>
Babel <3.9l> and hyphenation patterns for 79 languages loaded.
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/size10.clo))
(/usr/local/texlive/2015/texmf-dist/tex/latex/fontspec/fontspec.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/l3kernel/expl3.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/l3kernel/expl3-code.tex)
(/usr/local/texlive/2015/texmf-dist/tex/latex/l3kernel/l3unicode-data.def)
(/usr/local/texlive/2015/texmf-dist/tex/latex/l3kernel/l3xdvipdfmx.def))
(/usr/local/texlive/2015/texmf-dist/tex/latex/l3packages/xparse/xparse.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/fontspec/fontspec-patches.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/fontspec/fontspec-xetex.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/fontenc.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/euenc/eu1enc.def)
(/usr/local/texlive/2015/texmf-dist/tex/latex/euenc/eu1lmr.fd))
(/usr/local/texlive/2015/texmf-dist/tex/xelatex/xunicode/xunicode.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/tipa/t3enc.def
(/usr/local/texlive/2015/texmf-dist/tex/latex/euenc/eu1lmss.fd))
(/usr/local/texlive/2015/texmf-dist/tex/latex/graphics/graphicx.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/graphics/graphics.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/graphics/trig.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/latexconfig/graphics.cfg)
(/usr/local/texlive/2015/texmf-dist/tex/xelatex/xetex-def/xetex.def))))
(/usr/local/texlive/2015/texmf-dist/tex/latex/fontspec/fontspec.cfg)))
(./Untitled.aux) (/usr/local/texlive/2015/texmf-dist/tex/latex/tipa/t3cmr.fd)
[1] (./Untitled.aux) )
Output written on Untitled.pdf (1 page).
SyncTeX written on Untitled.synctex.gz.
Transcript written on Untitled.log.


I also tried setting the font type to "Devanagari MT" but that didn't work either.

Update:

I downloaded and installed texlive-2016 and re-ran my file through xelatex. Now it produces a meaningful error Font 'Nakula' does not contain script 'Devanagari'.

This is XeTeX, Version 3.14159265-2.6-0.99996 (TeX Live 2016) (preloaded format=xelatex)
restricted \write18 enabled.
entering extended mode
(./Untitled.tex
LaTeX2e <2016/03/31>
Babel <3.9r> and hyphenation patterns for 83 language(s) loaded.
(/usr/local/texlive/2016/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/local/texlive/2016/texmf-dist/tex/latex/base/size10.clo))
(/usr/local/texlive/2016/texmf-dist/tex/latex/fontspec/fontspec.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/l3kernel/expl3.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/l3kernel/expl3-code.tex)
(/usr/local/texlive/2016/texmf-dist/tex/latex/l3kernel/l3xdvipdfmx.def))
(/usr/local/texlive/2016/texmf-dist/tex/latex/l3packages/xparse/xparse.sty)
(/usr/local/texlive/2016/texmf-dist/tex/latex/fontspec/fontspec-xetex.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/base/fontenc.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/euenc/eu1enc.def)
(/usr/local/texlive/2016/texmf-dist/tex/latex/euenc/eu1lmr.fd))
(/usr/local/texlive/2016/texmf-dist/tex/xelatex/xunicode/xunicode.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/tipa/t3enc.def
(/usr/local/texlive/2016/texmf-dist/tex/latex/euenc/eu1lmss.fd))
(/usr/local/texlive/2016/texmf-dist/tex/latex/graphics/graphicx.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/local/texlive/2016/texmf-dist/tex/latex/graphics/graphics.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/graphics/trig.sty)
(/usr/local/texlive/2016/texmf-dist/tex/latex/graphics-cfg/graphics.cfg)
(/usr/local/texlive/2016/texmf-dist/tex/xelatex/xetex-def/xetex.def
(/usr/local/texlive/2016/texmf-dist/tex/generic/oberdiek/infwarerr.sty)
(/usr/local/texlive/2016/texmf-dist/tex/generic/oberdiek/ltxcmds.sty)))))
(/usr/local/texlive/2016/texmf-dist/tex/latex/fontspec/fontspec.cfg)))
(./Untitled.aux) (/usr/local/texlive/2016/texmf-dist/tex/latex/tipa/t3cmr.fd)
*************************************************
* fontspec warning: "script-not-exist"
*
* Font 'Nakula' does not contain script 'Devanagari'.
*************************************************
[1] (./Untitled.aux) )
Output written on Untitled.pdf (1 page).
SyncTeX written on Untitled.synctex.gz.
Transcript written on Untitled.log.


Output:

• Somehow it doesn't make sense that the output contains six question marks: the input has 19 Unicode codepoints (two of them spaces) or 8 glyphs (Devanagari aksharas) (plus two spaces). Where does 6 come from: are you sure you're looking at the correct output file? :-) (For example, "नमस्ते" has exactly 6 Unicode codepoints…). – ShreevatsaR Dec 21 '16 at 19:54
• Yes it does. Please see dropbox.com/s/d1fmeoitquxnj04/zoom_0.mp4?dl=0 – linuxfan Dec 21 '16 at 20:13
• Try clicking on "Trash Aux Files" to delete the aux files; also remove the \expandafter line (and/or hit Enter at the prompt in TeXShop's console to make the compilation complete). – ShreevatsaR Dec 21 '16 at 20:31
• Thanks for trying to help :=) dropbox.com/s/d1fmeoitquxnj04/after-trashing-aux.mp4?dl=0 – linuxfan Dec 21 '16 at 20:43
• Arrgghh. I found the solution. TexShop was converting my file to plain-text. When I cat the file, I saw a bunch of ??? instead of devanagari letters. I added a line at the top of my file (as per tex.stackexchange.com/questions/46199/…) and now I finally see devanagari in my pdf! Thanks @ShreevatsaR and all others for your help – linuxfan Dec 21 '16 at 21:05

The problem is that TeXShop apparently defaults to saving documents as latin1, rather than utf-8 as a modern editor would.

Although adding the !TS line in every document is an option, the best thing to do is to alter the default encoding for files in TeXShop's preferences so that utf-8 is used by default. A !TS line can then be used to override this in the unlikely event that it is ever necessary.

This happened to one of my students a couple of weeks ago and I was astonished that TeXShop does not default to utf-8. But it certainly doesn't - this was a brand new install and there it was: latin1 encoding for all documents as default.

• I did as you suggested and agree that it is more efficient than applying a workaround to every .tex file. Thanks – linuxfan Dec 22 '16 at 2:48

The real problem was with TexShop. It saved my file as ASCII and not unicode:

$file zz.tex zz.tex: LaTeX 2e document text, ASCII text$ cat zz.tex
\documentclass{article}
\usepackage{fontspec}
\newfontscript{Devanagari}{deva}
\setmainfont{Devanagari MT}

\begin{document}

?????? ????? ??????
\end{document}


This gave me a hint that the file-type was incorrect. Following TeXShop doesn't remember file encoding, I added the following as the first line of my .tex file:

% !TEX encoding = UTF-8 Unicode


and everything worked just fine.

\$ file zz.tex
zz.tex: LaTeX 2e document text, UTF-8 Unicode text

• This will work, but please see my answer below. This happened to one of my students a couple of weeks ago and I was astonished that TeXShop does not default to this. But it certainly doesn't - this was a brand new install and there it was: latin1 encoding for all documents as default. – cfr Dec 22 '16 at 2:29

Too long for a comment.

The following document

\documentclass{article}
\usepackage{fontspec}
\setmainfont[Path=./,Extension=.ttf,Script=Devanagari]{nakula}

\begin{document}

नमस्ते रुद्र मन्यवे

\expandafter\show\the\font

\end{document}


halts XeLaTeX (as expected) and displays the information

*************************************************
* fontspec warning: "script-not-exist"
*
* Font 'nakula' does not contain script 'Devanagari'.
*************************************************
(./nak.aux) (/usr/local/texlive/2016/texmf-dist/tex/latex/tipa/t3cmr.fd)
> \EU1/nakula(0)/m/n/10=select font "[./nakula.ttf]/OT:script=deva;language=DFL
T;mapping=tex-text;".
<inserted text> \EU1/nakula(0)/m/n/10

l.9 \expandafter\show\the\font


Thus you can see that the warning message is spurious, which seems to be a small bug in fontspec. Indeed, if I do

\documentclass{article}
\usepackage{fontspec}
\newfontscript{Devanagari}{deva}

\setmainfont[Path=./,Extension=.ttf,Script=Devanagari]{nakula}

\begin{document}

नमस्ते रुद्र मन्यवे

\expandafter\show\the\font

\end{document}


no warning is issued and the font information is exactly the same. As you can see, the script is correctly loaded.

The output on my system is

I can't read the Devanagari script, so I can't judge whether it's correct or not. However, other software shows

• Errors after applying changes that you suggested: kpathsea:make_tex: Invalid fontname [./Nakula.ttf]/OT', contains '[' ./Untitled.tex:5: fontspec error: "font-not-found" ! The font "Nakula" cannot be found. l.5 .../,Extension=.ttf,Script=Devanagari]{Nakula} kpathsea:make_tex: Invalid fontname [./Nakula.ttf]/OT', contains '[' ./Untitled.tex:5: Font EU1/Nakula(0)/m/n/10=[./Nakula.ttf]/OT:mapping=tex-text; at 10.0pt not loadable: Metric (TFM) file or installed font not found. <to be read again> relax l.5 .../,Extension=.ttf,Script=Devanagari]{Nakula} – linuxfan Dec 21 '16 at 18:40
• Your output looks perfect. Is there a way to get the correct names of all fonts installed on my system? – linuxfan Dec 21 '16 at 18:44
• Try with \setmainfont{Nakula} -- you probably don't have Nakula in your current folder. – Ulrike Fischer Dec 21 '16 at 18:51
• No luck even after making the change suggested by Ulrike. All the additional fonts (Chandas, Charis SIL, Nakula etc.) are under "User" in my fontbook. Even if that were a reason for this failure, "Devanagari MT", installed under "All Fonts" doesn't work. – linuxfan Dec 21 '16 at 18:55
• But it did work in your first example. So why should xelatex no longer find your font? – Ulrike Fischer Dec 21 '16 at 19:01

(This is just "could not reproduce", but too long for a comment.) I installed Nakula from its website and ran exactly the file in the question

\documentclass{article}
\usepackage{fontspec}
\begin{document}
\setmainfont[Script=Devanagari]{Nakula}
नमस्ते रुद्र मन्यवे
\end{document}


through xelatex (on Mac OS X Yosemite 10.10.5, texlive-2015) and got the expected output:

So I imagine it's something odd about your installation of Nakula that makes XeLaTeX not find it properly.

• I can probably find a mac running a different version of OSX and check if xelatex works in that environment. – linuxfan Dec 21 '16 at 19:35
• @linuxfan Also I strongly suggest uninstalling Nakula and trying with a fresh install from the website as above. – ShreevatsaR Dec 21 '16 at 19:40
• I already did a reinstall but it has no impact on the result. – linuxfan Dec 21 '16 at 19:48