I am trying to typset chapter titles in my TOC uppercase, as in this answer. I do not want to do it in the \chapter{} command, since I use the hyperref package to refer to the titles throughout my document and don't want them uppercase in those instances. The above answer, for some reason, does not work with polyglossia and RTL languages. I have edited the MWE here to show this (Note: I changed it from sections to chapters and the report class):

\documentclass{report}
\usepackage{textcase}
\usepackage{tocloft}
\usepackage{hyperref}
\usepackage{polyglossia}
\setotherlanguage{hebrew}

\makeatletter
\let\oldcontentsline\contentsline
\def\contentsline#1#2{%
\expandafter\ifx\csname l@#1\endcsname\l@chapter
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{%
\oldcontentsline{#1}{\MakeTextUppercase{#2}}%
}{%
\oldcontentsline{#1}{#2}%
}%
}
\makeatother

\begin{document}
\tableofcontents
\chapter{A test section $a=b$}\label{sec:test}
\chapter{Another test section with a reference:~\ref{sec:test}}
\section{Third}
\end{document}


If you comment out the \setotherlanguage{hebrew} it works just fine. So it is not just polyglossia, but RTL languages (I tried with both Hebrew and Syriac).

I am not tied to the above solution, so if someone knows a different way to do this with RTL languages, I am all ears.

Delay the redefinition to \begin{document}.

I have no idea and didn't tested if hebrew and \MakeTextUppercase goes along, if you have hebrew chapter titles you will have to check this yourself:

\documentclass{report}
\usepackage{textcase}
\usepackage{tocloft}
\usepackage{hyperref}
\usepackage{polyglossia}
\setotherlanguage{hebrew}

\makeatletter
\AtBeginDocument{%
\let\oldcontentsline\contentsline
\def\contentsline#1#2{%
\expandafter\ifx\csname l@#1\endcsname\l@chapter
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{%
\oldcontentsline{#1}{\MakeTextUppercase{#2}}%
}{%
\oldcontentsline{#1}{#2}%
}%
}}
\makeatother

\begin{document}
\tableofcontents
\chapter{A test section $a=b$}\label{sec:test}
\chapter{Another test section with a reference:~\ref{sec:test}}
\section{Third}
\end{document}

• That works, thanks. I had tried the \AtBeginDocument{, but I had put it before \makeatletter. I guess \makeatletter only works in the preamble? – Paul Oct 30 '18 at 13:54
• No \makeatletter also works in the document. But inside \AtBeginDocument it is too late to change the catcodes of the following code - the argument has already been read. – Ulrike Fischer Oct 30 '18 at 13:56