# problem with polyglossia and hyperref

Here a minimal example, I don't know where is the problem?

\documentclass[12pt,a4paper,openany]{book}

%%%%%%%%%%%%%%%%%%%
\usepackage{fancyhdr}
\pagestyle{fancy}

\usepackage{hyperref}
\hypersetup{
filecolor=magenta,
urlcolor=cyan,
citecolor=blue
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{polyglossia}
\setmainlanguage[numerals=maghrib]{arabic}
\setotherlanguage{english}
\newfontfamily\arabicfont[Script=Arabic,Scale=1.1,AutoFakeSlant=-0.03]{Amiri}
\setsansfont[Script=Arabic,Scale=1.1]{Amiri}

\makeatletter
\pretocmd{\@chapter}{\begingroup \renewcommand{\thechapter}{{\words{chapter}}}}{}{}
\apptocmd{\@chapter}{\endgroup}{}{}
\makeatother

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  MACRO FOR LITERAL NUMBERING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\words[1]{\expandafter\xwords\csname c@#1\endcsname}
\def\xwords#1{\ifcase#1\or
الأول\or
الثاني\or
الثالث\or
الرابع\or
\else
I need more words\fi}

\makeatletter
\makeatother

%%%%%%%%%%%%%%%%%% REDEFINES \THECHAPTER IN CONTENTS
\makeatletter
\pretocmd{\@chapter}{\begingroup \renewcommand{\thechapter}{{\words{chapter}}}}{}{}
\apptocmd{\@chapter}{\endgroup}{}{}
\makeatother

\usepackage[Glenn]{fncychap}
\ChNumVar{\Large}% Dimension of the number of the chapter in the first page... instead of the default...\ChNumVar{\Huge}

\begin{document}

\chapter{واحد}

\end{document}

• I would say you are out of luck. fncychap wants to be after bidi/polyglossia to redefine the look of a chapter, hyperref wants to be after fncychap to insert the destination code in a chapter, and bidi wants to be behind hyperref for whatever reason. A nice circle. – Ulrike Fischer Aug 20 '19 at 21:03
• @UlrikeFischer in any place I put them there is an error – Poline Sandra Aug 20 '19 at 21:15
• yes, as I wrote: A circle. – Ulrike Fischer Aug 20 '19 at 21:18
• there is no solutions ?@UlrikeFischer – Poline Sandra Aug 20 '19 at 21:26
• Could you switch to LuaLaTeX and babel instead of XeLaTeX and polyglossia? Then you don't have to use bidi. – David Purton Aug 20 '19 at 22:25

This may or may not be a suitable answer, but as far as I can see (as someone who can't understand Arabic) it gives the same output as your MWE without hyperref (including in the TOC and headers).

I've switched to using babel and LuaLaTeX to get around the load order problems with bidi. And patched \Hy@org@chapter. This is the original \@chapter macro which hyperref embeds into its own new \@chapter.

\documentclass[12pt,a4paper,openany]{book}
\usepackage{etoolbox}
\usepackage[nil,bidi=basic-r]{babel}
\babelprovide[import,main]{arabic}
\babelprovide[import]{english}
\babelfont[arabic]{rm}[Scale=1.1, AutoFakeSlant=-0.03]{Amiri}
\babelfont[arabic]{sf}[Scale=1.1]{Amiri}
\usepackage[Glenn]{fncychap}
\ChNumVar{\Large}
\usepackage{fancyhdr}
\pagestyle{fancy}
\usepackage{hyperref}
\hypersetup{
filecolor=magenta,
urlcolor=cyan,
citecolor=blue
}
\newcommand\words[1]{\expandafter\xwords\csname c@#1\endcsname}
\def\xwords#1{%
\ifcase#1\or
الأول\or
الثاني\or
الثالث\or
الرابع\or
\else
I need more words\fi}
\makeatletter
\pretocmd{\Hy@org@chapter}
{\begingroup \renewcommand{\thechapter}{{\words{chapter}}}}
{}
{}
\apptocmd{\Hy@org@chapter}
{\endgroup}
{}
{}
{\thechapter}
{\words{chapter}}
{}
{}
\makeatother
\begin{document}
\chapter{واحد}
\end{document}


• but I want to use "Amiri" to write , is there something to replace Hyperref? – Poline Sandra Aug 23 '19 at 10:01
• @PolineSandra, you can use any font you want – David Purton Aug 23 '19 at 10:26
• @PolineSandra, I don't really understand you comment actually. My MWE does use Amiri and hyperref. There is no other package that does the same as hyperref. – David Purton Aug 23 '19 at 14:00
• I don't see it I'm sorry @David Purton – Poline Sandra Aug 23 '19 at 16:41
• @PolineSandra the \babelfont lines set the font to Amiri. Did you try to compile my example? What happens? – David Purton Aug 23 '19 at 23:14