5

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}
Davislor
  • 44,045
Niranjan
  • 3,435
  • 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. – Davislor Jun 08 '20 at 06: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. – Niranjan Jun 08 '20 at 07: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. – Davislor Jun 08 '20 at 07:04
  • I wish that the process becomes easier on the users' side. I will see babel-hin.ini and if the counters are good enough for the requirements of Marathi, I'll open up a new pull request. I personally manage to get everything right in my documents. I opened this question to ease the process for other new Marathi users with the help of my package. I want to give my users the best that LaTeX has. – Niranjan Jun 08 '20 at 07:10
  • 1
    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. – Davislor Jun 08 '20 at 07:16
  • Your help is very much appreciated. Thanks :) – Niranjan Jun 08 '20 at 07:21
  • One other minor thing. If you’re using SHobhika, you can simplify the font commands to \babelfont{rm}[Scale = 1.0]{Shobhika}. Even the Scale= parameter is a very minor tweak that only matters if you’re scaling every other font you use to the height of the main font. This font doesn’t have any of the OpenType features I used for Libertinus in my example, it’s not sans-serif, and it supports both scripts I wanted to use. A \babelfont will automatically be called with the currently-selected language and its associated script. So you don’t need to copy all that code. :) – Davislor Jun 08 '20 at 07:33
  • Yes. That is much easier. Also one more very important font-feature. Kindly add Renderer=Harfbuzz to the \defaultfontfeatures command. It is very much needed to get Devanagari ligatures right. – Niranjan Jun 08 '20 at 07:40
  • 1
    I updated the MWE. – Davislor Jun 08 '20 at 07:52

2 Answers2

8

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}

Shobhika font sample

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.

Davislor
  • 44,045
  • Please see this log file. I saved your MWE with test.tex name and compiled it with luahbtex (v1.12.0). – Niranjan Jun 07 '20 at 09:09
  • 1
    @Niranjan you should use lualatex to compile the example not luahbtex (which will use plain format). – Ulrike Fischer Jun 07 '20 at 09:20
  • Thanks @UlrikeFischer . Now it gives good output. This answer definitely gives a good solution with babel, but now I as I experimented more with it I realized that translations for polyglossia are much more updated and maintained. I'll look into babel-mr.ini now, but isn't there any good solution with polyglossia as of now? – Niranjan Jun 07 '20 at 09:28
  • 2
    @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. – Ulrike Fischer Jun 07 '20 at 09:35
  • I didn't say babel is not maintained. The translations are not updated. They are old and needs to be updated. I am opening a pull request for the same :) – Niranjan Jun 07 '20 at 09:52
  • 2
    @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. – Ulrike Fischer Jun 07 '20 at 10:38
  • Oops! I just changed the file. Forgot to create the pull request. Now done here. – Niranjan Jun 07 '20 at 10:45
  • @Niranjan I see how my wording was confusing about which command to use. I’ve made a small edit. – Davislor Jun 07 '20 at 17:45
  • 1
    @Niranjan Thanks! Both babel and polyglossia could use some attention from native speakers. – Davislor Jun 07 '20 at 17:50
1

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.

Davislor
  • 44,045