4

I am writing an Arabic book in LaTeX where I have to put much C code in it. I am compiling it using XeLaTeX. To have a proper Arabic support I use polyglossia package. For source code, I use listings, and to control the direction of the text I use bidi. The problem is that I can't make the source code looks really LTR. This screenshot shows all what I could do :

Source code screenshot

As you can see, the symbols <> () {} are inverted and written in RTL mode. This is some of the code I am currently using :

\documentclass[11pt, a4paper]{book}
\usepackage{polyglossia}
\usepackage{listings}
\usepackage{bidi}
% And other packages ...
\setmainlanguage[locale=algeria]{arabic}
\setotherlanguage{english}
\defaultfontfeatures{Ligatures=TeX}
\setmainfont[Script=Arabic, Scale=1.2]{Amiri}
\setmonofont{Courier New}
\newfontfamily\englishfont[Script=Latin]{Arial}
% ...
\lstset{language=C, showstringspaces=false, frame=single, numbers=left,
    breaklines=true, keywordstyle=\bfseries\color{Blue}, commentstyle=\color{LightGray},
    numberstyle=\color{Gray}, stringstyle=\color{Yellow}, basicstyle=\ttfamily, morecomment=[l][\color{magenta}]{\#}}
\lstnewenvironment{Csource}{\setLTR}{\unsetLTR}
% ...
\begin{Csource}
#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Hello world!\n");
    return 0;
}

\end{Csource}
4

In your case better use of

\newfontfamily\arabicfont[Script=Arabic, Scale=1.2]{Amiri}
\newfontfamily\arabicfonttt{Courier New}

Rather then

\setmainfont[Script=Arabic, Scale=1.2]{Amiri}
\setmonofont{Courier New}

And you can remove bidi pachage which is loaded by default in case of RTL languages with polyglossia

Code

\documentclass[11pt, a4paper]{book}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{polyglossia}
% And other packages ...
\setmainlanguage[locale=algeria]{arabic}
\setotherlanguage{english}
\defaultfontfeatures{Ligatures=TeX}
\newfontfamily\arabicfont[Script=Arabic, Scale=1.2]{Amiri}
\newfontfamily\arabicfonttt{Courier New}
\newfontfamily\englishfont[Script=Latin]{Arial}
% ...
\lstset{language=C, showstringspaces=false, frame=single, numbers=left,
    breaklines=true, keywordstyle=\bfseries\color{blue},basicstyle=\ttfamily, commentstyle=\color{LightGray},
    numberstyle=\color{gray}, stringstyle=\color{yellow},  morecomment=[l][\color{magenta}]{\#}}
\lstnewenvironment{Csource}{\setLTR}{\unsetLTR}
% ...

\begin{document}


\begin{Csource}
#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Hello world!\n");
    return 0;
}

\end{Csource}


\end{document}

Output

enter image description here

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.