2

I'm using overleaf to write my document and I need to list some python code in minted environment that contains Farsi words as follows:

\begin{minted}[bgcolor=bg]{python}
>>> farsi_tagged_sents = nltk.corpus.multext_east.tagged_sents("oana-fa.xml", tagset='universal')
>>> farsi_tagged_sents[0]
[('یکی', 'NUM'),
 ('از', 'ADP'),
 ('روزهايِ', 'NOUN'),
 ('بسيار', 'ADV'),
 ('سردِ', 'ADJ'),
 ('ماهِ', 'NOUN'),
 ('آوريل', 'NOUN'),
 ('بود', 'VERB'),
 ('و', 'CONJ'),
 ('ساعتها', 'NOUN'),
 ('سيزده', 'NUM'),
 ('ضربه', 'NOUN'),
 ('مينواختند', 'VERB'),
 ('.', '')]
\end{minted}

However, nothing is displayed between quotes when I compile the above code. I tried babel package and \FR{} command but no luck. Is there any way I could highlight my code and at the same time include Farsi in it?

1
  • Please add a complete code starting with \documentclass and ending with \end{document}
    – Salim Bou
    Sep 7 '19 at 8:16
2

I think the only realistic way to do this is to use babel and lualatex.

The bad news is that you say you are using Overleaf. Almost certainly, the version of babel on Overleaf is too old for this to work, so you will need to run an up to date TeXLive install locally.

MWE

Compile this with: lualatex --shell-escape filename.

The mapfont=direction option allows babel to automatically switch to a Persian font for RTL characters. This means there is no need to mark up the text in the minted environment.

\documentclass{article}
\usepackage[bidi=basic]{babel}
\babelprovide[import,main,language=Default]{english}
\babelprovide[import,mapfont=direction,language=Default]{persian}
\babelfont{rm}{Latin Modern Roman}
\babelfont{sf}{Latin Modern Sans}
\babelfont{tt}{Latin Modern Mono}
\babelfont[persian]{tt}{FreeMono}
\usepackage{xcolor}
\usepackage{minted}
\begin{document}
\begin{minted}[fontsize=\footnotesize]{python}
>>> farsi_tagged_sents = nltk.corpus.multext_east.tagged_sents("oana-fa.xml", tagset='universal')
>>> farsi_tagged_sents[0]
[('یکی', 'NUM'),
 ('از', 'ADP'),
 ('روزهايِ', 'NOUN'),
 ('بسيار', 'ADV'),
 ('سردِ', 'ADJ'),
 ('ماهِ', 'NOUN'),
 ('آوريل', 'NOUN'),
 ('بود', 'VERB'),
 ('و', 'CONJ'),
 ('ساعتها', 'NOUN'),
 ('سيزده', 'NUM'),
 ('ضربه', 'NOUN'),
 ('مينواختند', 'VERB'),
 ('.', '')]
\end{minted}
\end{document}

MWE output

1
  • Awesome, worked like a charm. Thank you!
    – Pymal
    Sep 11 '19 at 5:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.