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.
\AtBeginDocument{, but I had put it before\makeatletter. I guess\makeatletteronly works in the preamble? – Paul Oct 30 '18 at 13:54