# Mapping Devanagari numerals with LuaLaTeX

I have released a package named marathi which aims for typing Marathi language with great efficiency. This package requires XeLaTeX or LuaLaTeX. XeLaTeX with package fontspec provides a way to map arabic numerals with Devanagari ones. Try the following code -

% !TEX TS-Program = xelatex
\documentclass{article}
\usepackage{fontspec}
\setmainfont[Script=Devanagari,Mapping=devanagarinumerals]{Shobhika}

\begin{document}
\section{एक}
\section{दोन}
\end{document}


This gives correct mapping of Devanagari numerals. Unfortunately this function is not available with LuaLaTeX. Try the following code.

% !TEX TS-Program = lualatex
\documentclass{article}
\usepackage{fontspec}
\setmainfont[Renderer=Harfbuzz,Script=Devanagari,Mapping=devanagarinumerals]{Shobhika}

\begin{document}
\section{एक}
\section{दोन}
\end{document}


This gives the following warning -

Input mapping not supported in LuaTeX.


Why is it not possible in LuaLaTeX is my first question.

I have tried to generate Devanagari numerals with LuaLaTeX using a solution given in this answer. It works almost fine unless a package using definition \@arabic is used. Try this code (you need to install package marathi and as per the package requirement Shobhika font which is mostly included in the TeX-distribution) -

\documentclass{article}
\usepackage{marathi}

\begin{document}
\section{एक}
\section{दोन}
\end{document}


This shall produce good results with both XeLaTeX and LuaLaTeX, but I got an error with package when I used a package which treats \@arabic differently. There might be many such packages. The answer which I have referred also warns us about its effects, though I would like to use a softer way which will not produce un-predicted errors but will give me Devanagari numerals where necessary. Is there any way to make the package ignore the redefinition of \@arabic when it is already used by a package, but mapping the numerals when there is no such package loaded. Concerned definitions can be found on line no. 66 and 67 of the package.

Package babel does not give local enumeration. Try this code.

\tracinglostchars = 2 % Print a warning message if a character is missing.
\documentclass{article}
\usepackage[paperwidth=10cm]{geometry} % To format the MWE for TeX.SX
\usepackage[english, bidi=basic, layout=sectioning.counters]{babel}
\usepackage{fontspec}
\babelprovide[import, main, mapdigits]{marathi}
\defaultfontfeatures{Scale = MatchLowercase, Ligatures=TeX}
\babelfont{rm}[Scale=1.0,Ligatures={Common,Discretionary},Numbers=OldStyle]{Shobhika}
\babelfont{sf}[Ligatures={Common, Discretionary}]{Shobhika}
\babelfont[marathi]{rm}{Shobhika}
\babelfont[marathi]{sf}{Shobhika}

\begin{document}
\begin{enumerate}
\item अबक
\begin{enumerate}
\item अबक
\begin{enumerate}
\item अबक
\end{enumerate}
\end{enumerate}
\end{enumerate}
\end{document}

• You’ve edited a new question into your answer (which more people are likely to notice if you post a new one than if you put it at the end of a question that already has an accepted answer). I believe the answer to it is here. Jun 8 '20 at 6:55
• Thanks @Davislor! This is exactly how polyglossia renews the enumeration counters in gloss-marathi.ldf. babel-mr.ini is lacking these definitions. I have asked this question here on the GitHub repo. Jun 8 '20 at 7:02
• There is an alphabetic counter for Hindi in babel (which you can use with \babelprovide[import, maparabic, alph=alphabetic]{hindi}), but it is not defined for Marathi. You can, however, create it yourself with enumitem. Jun 8 '20 at 7:04
• I’m glad that someone is motivated to improve support for the language. I hope I was able to help in some way, but I don’t speak Marathi and would be completely unqualified to do the work myself. Jun 8 '20 at 7:16
• I updated the MWE. Jun 8 '20 at 7:52

The babel package, running on LuaHBTeX (that is, LuaLaTeX from 2020 or later), supports this through the mapdigits feature. This example will change the digits to Devanagari in page numbers, chapter numbers, the table of contents, etc., and in Marathi text, but not in English text or math mode.

%% This MWE requires LuaHBTeX 1.12.0 or later.
\tracinglostchars = 2 % Print a warning message if a character is missing.
\documentclass{article}
\usepackage[paperwidth=10cm]{geometry} % To format the MWE for TeX.SX
\usepackage[english, bidi=basic, layout=sectioning.counters]{babel}
\usepackage{fontspec}

\babelprovide[import, main, mapdigits]{marathi}

\defaultfontfeatures{Scale = MatchLowercase, Ligatures=TeX, Renderer = HarfBuzz}
\babelfont{rm}
[Scale = 1.0]{Shobhika}

\newcommand\textenglish[1]{\foreignlanguage{english}{#1}}
\newenvironment{english}{\begin{otherlanguage}{english}}{\end{otherlanguage}}

\begin{document}
\section{एक}
\textenglish{The number 42 in English. In math mode:} $$42$$.

\section{दोन}
\textenglish{In Marathi:} 42.
\textenglish{This is Section} \arabic{section}.

\end{document}


If you are compiling with Babel and XeLaTeX, the sectioning= package option will work for counters such as page and chapter numbers, and you can pass maparabic instead of mapdigits to \babelprovide to make \arabic counters such as numbered lists use different numerals. It also provides the command \marathidigits{42}. Or you can just type in Marathi in your editor the way you normally do.

If the localized strings are unsatisfactory, you can override them by redefining \marathicontentsname, \marathichaptername, \marathiabstractname, etc.

The polyglossia package provides a similar command to map digits, \devanagaridigits.

• Please see this log file. I saved your MWE with test.tex name and compiled it with luahbtex (v1.12.0). Jun 7 '20 at 9:09
• @Niranjan you should use lualatex to compile the example not luahbtex (which will use plain format). Jun 7 '20 at 9:20
• @Niranjan babel is well maintained and updated frequently. If you find errors in the ini open an issue or a pull request at the github. Jun 7 '20 at 9:35
• @Niranjan Yes, I saw it. But you should create a real pull request by clicking on the "open pull request" button. Then Javier can simply merge. Jun 7 '20 at 10:38
• @Niranjan Thanks! Both babel and polyglossia could use some attention from native speakers. Jun 7 '20 at 17:50

I used my first answer to provide a solution that works with the toolchain as it exists today. But I’ll also try to answer the several questions you literally asked.

Only XeTeX supports the Mapping= feature of fontspec. If you have one solution that works in XeLaTeX and another solution that works in LuaLaTeX, you can use the macros in the iftex package to select the right one at compile time.

If there’s some other package that redefines \@arabic in a way that conflicts with yours, you have a few options. You can make your definition come last by wrapping it in \AtEndPreamble or \AtBeginDocument. You can document that your package must be loaded in a particular order. You can detect the other package with \@ifpackageloaded (possibly deferred until \AtEndPreamble so that it works whether you load your package first or last) and attempt to monkey-patch it.

• Thanks for this clarification. My package already uses iftex for conditionally using Mapping= function. Is there any way to decide the compiler if a certain package is loaded? Jun 8 '20 at 4:24
• I am thinking of using babel ifluatex and polyglossia ifxetex :) Jun 8 '20 at 4:26
• Also it would be great if you can explain the use of the parameters used in your MWE. Jun 8 '20 at 4:27
• @Niranjan \@ifpackageloaded. You can also check for options the other package was loaded with. Jun 8 '20 at 4:41
• @Niranjan Which parameters did you have in mind? Jun 8 '20 at 4:43