Saturday, 20 May 2017

How to use a language (Hindi, Bangla, Punjabi, Tamil, etc) other than English in Latex?

In order to use any language other than English in LaTex, following things are required:


1. Font of that language should be installed on the Machine.

2. Document should be compiled with XeLaTex or LuaLaTex. 
There are multiple ways to do this: (i) use commands in the document itself, e.g., as done in following example (ii) Change the default complier (like in TexStudio) (iii) Use menu options to compile the document with desired compiler, e.g.,  in TexStudio follow this: Tools>>Commands>>XeLaTex.

3. Declare a command for using the font.
Notice the 6 and 7 line number of the following example. After \newfontfamily, a name (\punjabifont) is given to the font (which is specified as 'Saab' in the braces). Then in the line number 7 one command is declared using the font name. In this case  \textpunjabi is the command name which is then used in the document for inserting the Punjabi text.

Example: Suppose we want to use Punjabi in Latex. First install the font "Saab" from the link and then copy paste the following program in LaTex and compile normally:

% !TeX program = xelatex
% !TeX encoding = utf8


\textpunjabi{ ਵਾਹਿਗੁਰੂ ਜੀ ਕਾ ਖਾਲਸਾ !}\\
\textpunjabi{ਵਾਹਿਗੁਰੂ ਜੀ ਕੀ ਫਤਹਿ !!}

Similarly, we can work with Hindi, Tamil, Bangla etc. For working with Hindi install the font 'Devanagari MT' on your machine and use the following program:

Hindi/Sanskrit Example:
% !TeX program = xelatex
% !TeX encoding = utf8

\newfontfamily\devanagarifont{Devanagari MT}

\texthindi{कर्मण्येवाधिकारस्ते मा फलेषु कदाचन।}\\
\texthindi{मा कर्मफलहेतुर्भूर्मा ते सङ्गोऽस्त्वकर्मणि॥ २-४७}

Possible errors:

1. Font related problems:

fontspec error: "font-not-found" The font "Devanagari1 MT" cannot be found. For immediate help type H <return>. \newfontfamily\devanagarifont{Devanagari1 MT}

This error can appear due to following reasons:

(a) Font which you are trying to use is not available on your machine, i.e., font is not installed.

(b) Font is installed but still we get this error because font might not be compatible with LaTex. I faced this issue while working with Hindi on Windows 10. So you need to use that font for your language which is compatible with LaTex. For Hindi, Nakula is compatible with LaTex and you can it use from here.

2. Compilation Errors:

Fatal fontspec error: "cannot-use-pdftex" The fontspec package requires either XeTeX or LuaTeX. You must change your typesetting engine to, e.g., "xelatex" or "lualatex"instead of plain "latex" or "pdflatex". For immediate help type H <return>.

This error occurs when you don't use compile the document properly. Refer to step 2, above.

3. 'fontspec' package issue:

Undefined control sequence. \emfontdeclare{ \emshape, \eminnershape }

This error appears when your fontspec package has some issue and need to be updated. So update this package to solve this error.

4. Installation Issues:

If you still get some error then that means there is some installation error. I faced this error twice while working on two different machines. The solution to this problem is to first uninstall miktex engine and editor (like TexStudio) from machine and then install them again and try these steps.

NOTE: I don't know much about LaTex but yes I have learnt to fix some problems and I am sharing the same here.


Dedicated to my parents 🙏🙏🙏...