4

I want to use Marathi font and English font in the same template but I don't want to specify Marathi or English in between. means I will write some random text which has Marathi or English both text anywhere in between. How can I manage this so that I don't need to write the Marathi or English explicitly.

For example:

स्वागत आहे!

Hello!

स्वागत आहे!
2
  • 2
    Does this answer your question? How to write Bengali in LaTeX?
    – codeR
    Commented 2 days ago
  • Try replacing font name with an appropriate one and see if it works for you.
    – codeR
    Commented yesterday

2 Answers 2

8

The most advanced way to do this is with Babel in LuaLaTeX. This can detect the language and switch between different serif, sans-serif and monospace fonts for each language. It also has the advantage of supporting bidirecional text and hyphenation rules for each language, if Babel has them.

You might need to surround some paragraphs and sections with commands like \begin{otherlanguage}{marathi} and \end[otherlanguage} if you need to switch between different layouts for the two languages.

Simplified template:

\documentclass{article}
\tracinglostchars=3
\usepackage[bidi=basic,layout=sectioning]{babel}
\usepackage{fontspec}

\babelprovide[import,main,onchar=fonts ids]{marathi}
\babelprovide[onchar=fonts ids]{english}
\defaultfontfeatures{Scale=MatchUppercase, Ligatures=TeX, Renderer=HarfBuzz}

%% These fonts are available at: https://www.google.com/get/noto/
\defaultfontfeatures[NotoSerifDevanagari]{
    UprightFont=*_SemiCondensed-Light, % Pick a width and weight to match the other fonts.
    BoldFont=*-SemiBold,
    AutoFakeSlant = 0.20, % Hack in case we need something Italic or slanted.
}

\babelfont{rm}
          [Ligatures=Common,Scale=1.0]{Latin Modern Roman}
\babelfont{sf}
          {Latin Modern Sans}
\babelfont{tt}
          {Latin Modern Mono}
\babelfont[marathi]{rm}
          {NotoSerifDevanagari}

\begin{document}
\section*{मराठी and English}

स्वागत आहे! Hello! स्वागत आहे!

\noindent\textit{स्वागत आहे! Hello! स्वागत आहे!}
\end{document}

Noto Serif Devanagari sample

Edit:

A comment requested a version that takes its punctuation from the Devanagari, not the English, font. Here you go:

\documentclass{article}
\tracinglostchars=3
\usepackage[bidi=basic,layout=sectioning]{babel}
\usepackage[svgnames]{xcolor}
\usepackage{fontspec}

\babelprovide[import,main,onchar=fonts ids letters]{marathi}
\babelprovide[import,onchar=fonts ids letters]{english}
\defaultfontfeatures{Scale=MatchUppercase, Ligatures=TeX, Renderer=HarfBuzz}

%% These fonts are available at: https://www.google.com/get/noto/
\defaultfontfeatures[NotoSerifDevanagari]{
    UprightFont=*_SemiCondensed-Light, % Pick a width and weight to match the other fonts.
    BoldFont=*-SemiBold,
    AutoFakeSlant = 0.20, % Hack in case we need something Italic or slanted.
}

\babelfont{rm}
          {NotoSerifDevanagari}
\babelfont[english]{rm}
          [Ligatures=Common,Scale=1.0,Color=DarkGreen]{Latin Modern Roman}

\begin{document}
\section*{मराठी and English}

\foreignlanguage{marathi}{स्वागत आहे!} Hello! स्वागत आहे!

\noindent\textit{\foreignlanguage{marathi}{स्वागत आहे!} Hello! स्वागत आहे!}
\end{document}

Noto Serif Devanagari sample

If you want the punctuation to follow the current language, there’s no automatic rule for this. You need to use language tags:

\documentclass{article}
\tracinglostchars=3
\usepackage[bidi=basic,layout=sectioning]{babel}
\usepackage[svgnames]{xcolor}
\usepackage{fontspec}

\babelprovide[import,main,onchar=fonts ids letters]{marathi}
\babelprovide[import,onchar=fonts ids letters]{english}
\defaultfontfeatures{Scale=MatchUppercase, Ligatures=TeX, Renderer=HarfBuzz}

%% These fonts are available at: https://www.google.com/get/noto/
\defaultfontfeatures[NotoSerifDevanagari]{
    UprightFont=*_SemiCondensed-Light, % Pick a width and weight to match the other fonts.
    BoldFont=*-SemiBold,
    AutoFakeSlant = 0.20, % Hack in case we need something Italic or slanted.
}

\babelfont[english]{rm}
          [Scale=1.0, Ligatures=Common, Color=DarkGreen]{Latin Modern Roman}
\babelfont[marathi]{rm}
          {NotoSerifDevanagari}

\begin{document}
\section*{मराठी and English}

\foreignlanguage{marathi}{स्वागत आहे!} \foreignlanguage{english}{Hello!}
स्वागत आहे! Hello!

\noindent\textit{%
\foreignlanguage{marathi}{स्वागत आहे!} \foreignlanguage{english}{Hello!}
स्वागत आहे! Hello!}

\end{document}

Noto Serif Devanagari sample

10
  • 1
    @ApoorvPotnis I tried that, but NewCM is broken with \babelfont.
    – Davislor
    Commented yesterday
  • 1
    @ApoorvPotnis The \babelfont command choked when I copied and pasted the commands from NewCM10-Regular.fontspec into the document. Babel seems to have trouble whenever \defaultfontfeatures overrides the family name, which happens here to select the optical size NewCM08-Regular. As I recall, this was the same bug that made them rename the file to NewCM10-Regular.fontspec to begin with.
    – Davislor
    Commented yesterday
  • 1
    @ApoorvPotnis Posted a new version with what i think you wanted.
    – Davislor
    Commented yesterday
  • 1
    @ApoorvPotnis Note that, for the reasons given on page 31 of the Babel manual, there’s no general rule to auto-detect the language for punctuation, and if you want it to switch between two different fonts, you must sometimes tag it manually.
    – Davislor
    Commented yesterday
  • 1
    Thank you so much for the detailed answer! Commented yesterday
6

I will assume you have access to a suitable Opentype or Truetype Marathi font. I happen to have Google's Tiro Devanagari Marathi font installed on my system; you may have access to a different font.

Load the fontspec package and activate the font via a \setmainfont directive. And, as @ApoorvPotnis has kindly noted in a comment, do specify the options [Script=Devanagari, Language=Marathi, Renderer=Harfbuzz]. The third option should only be used if you use LuaLaTeX to compile your document.

enter image description here

%% compile with LuaLaTeX or XeLaTeX   
\documentclass{article} % or some other suitable document class
\usepackage{fontspec}   % for '\setmainfont' macro
\setmainfont{Tiro Devanagari Marathi}[%
             Script=Devanagari, 
             Language=Marathi, 
             Renderer=Harfbuzz % <-- use only with LuaLaTeX
            ] 

\begin{document}
स्वागत आहे! Hello! स्वागत आहे!

\emph{स्वागत आहे! Hello! स्वागत आहे!}
\end{document}  
4
  • 1
    I recommend adding [Script=Devanagari, Language=Marathi, Renderer=Harfbuzz] as an optional argument to \setmainfont{Tiro Devanagari Marathi}. This shall give you the correct ligatures (conjuncts), which are quite necessary in Devanagari. The output of the word स्वागत changes as well. Commented yesterday
  • 1
    I guess one can remove Renderer=Harfbuzz while using XeLaTeX. Commented yesterday
  • 1
    @ApoorvPotnis - Many thanks for your comments and suggestions! I've gladly incorporated them in my answer.
    – Mico
    Commented yesterday
  • Thanks you for the answers. Commented yesterday

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .