# RTL theorem numbering problem - mdframed and xelatex

I have a problem with wrong numbering order when using mdframed theorem style. As in the MWE, the second theorem number should 2.1.1 (reading from right to left, 1.1 for the section and 2 for the theorem number) but the counter appears in the middle between chapter and section numbers.

I have tried to redefine the counter using

\renewcommand{\thetheorem}{\arabic{theorem}.\thesection}


but it didn't work.

\documentclass{book}

\usepackage[framemethod=tikz]{mdframed}
\usepackage{amsmath, amssymb}

\usepackage{polyglossia}
\setdefaultlanguage[locale=morocco]{arabic}
\setmainfont{Times New Roman}

\mdtheorem{theorem}{Theorem}[section]

\renewcommand{\thetheorem}{\arabic{theorem}.\thesection}

\begin{document}

\chapter{A chapter}
\section{A section}

\begin{theorem}
There exists some $x$ such that $x^2=2$.
\end{theorem}

\begin{theorem}[A second theorem]
For all $x, y \in \mathbb{R}$:
\begin{itemize}
\item $x(-y) = (-x)y = -(xy)$:
\item $(-x)(-y) = xy$.
\end{itemize}
\end{theorem}

\end{document}

• Does the problem really disappear if you remove any remaining element of that example? If not, it is not yet a Minimal Working Example. Keep going until you find the problem. For example, what if you stick to the default font rather than loading TNR? What if you don't set the linecolor of the theorems to ocre? – cfr Mar 4 '14 at 23:38
• The problem is the arabic option given to \setdefaultlanguage; presumably this is doing something to block the effect of \arabic{theorem} when you redefine the counter. Not sure how to fix it (yet). – alexwlchan Mar 5 '14 at 1:01
• Okay, next find: putting a space on either side of the period in \arabic{theorem}.\thesection cures the bug. No idea why. (Also, amused that I found a reference to egreg in the polyglossia source. :P) – alexwlchan Mar 5 '14 at 1:09
• @alexwlchan What if you redefine the counter first? – cfr Mar 5 '14 at 1:18
• @ctr: no joy, but I think I have a fix. – alexwlchan Mar 5 '14 at 1:30

This one is subtle, and I don’t really understand why it happens yet. Unfortunately I’m tired and still have a lot to do before tomorrow morning, so I don’t have time to work out it out tonight, but I do have a fix.

If you remove the lines setting the document text to Arabic:

\usepackage{polyglossia}
\setdefaultlanguage[locale=morocco]{arabic}
\setmainfont{Times New Roman}


then the problem goes away. This lead me to think that maybe polyglossia was causing a conflict, until I started reading the polyglossia documentation:

Note that for Arabic one cannot use the environment arabic, as \arabic is defined internally by LATEX. In this case we need to use the environment Arabic instead.

Although this switches off Arabic text, it also turns off RTL, which turns out to be the source of the problem.

If you use this command:

\renewcommand{\thetheorem}{\arabic{theorem}.\thesection}


then theorem numbers get printed as "section.theorem.chapter". I assume this is something to do with the RTL converter getting a little confused, and when it expands \thesection, gets things out of order.

If we don't rely on \thesection to print the section number, and instead build it ourselves with the \arabic macro and by accessing the counter directly, it seems to work fine:

\renewcommand{\thetheorem}{\arabic{theorem}.\arabic{section}.\arabic{chapter}}


Somebody who knows how \thesection expands can probably explain why this happens; if not, I’ll try to work it out in the morning.

Here's an MWE of what I was using:

\documentclass{book}

\usepackage[framemethod=tikz]{mdframed}

\usepackage{amsmath, amssymb}

\usepackage{polyglossia}
\setdefaultlanguage[locale=morocco]{arabic}
\setmainfont{Times New Roman}

\mdtheorem{theorem}{Theorem}[section]
%\renewcommand{\thetheorem}{\arabic{theorem}.\thesection}
\renewcommand{\thetheorem}{\arabic{theorem}.\arabic{section}.\arabic{chapter}}

\newcommand*{\exampletheorems}{
\begin{theorem}
There exists some $x$ such that $x^2=2$.
\end{theorem}
\begin{theorem}[A second theorem]
For all $x, y \in \mathbb{R}$:
\begin{itemize}
\item $x(-y) = (-x)y = -(xy)$;
\item $(-x)(-y) = xy$.
\end{itemize}
\end{theorem}
}

\begin{document}

\chapter{}
\section{}

\exampletheorems

\section{}

\exampletheorems

\chapter{}
\section{}

\exampletheorems

\end{document}


and here's a screenshot:

• (I apologise for converting all the text to English, but I was too tired to try to parse Arabic as well.) – alexwlchan Mar 5 '14 at 1:44
• Thanks alexwlchan.. This seems to work.. How about the empty space to the right of the theorem head as you can see below. – user46994 Mar 5 '14 at 8:50
• What are you compiling it with? I can’t reproduce this on my setup. (OS X, TeX Live 2013, XeLaTeX) – alexwlchan Mar 5 '14 at 10:44
• I have the same here.. OS X, TEX Live 2013 and XeLateX. – user46994 Mar 5 '14 at 11:32
• I think cfr got the same below. – user46994 Mar 5 '14 at 11:32

This seems to work but will presumably have unwanted side-effects so I doubt this is really a solution. I would wait to see alexwlchan's solution which will likely be better:

\documentclass{book}

\usepackage[framemethod=tikz]{mdframed}
\usepackage{amsmath, amssymb}

\usepackage{polyglossia}
\setdefaultlanguage[locale=morocco]{arabic}
\setmainfont{Times New Roman}

\mdtheorem{theorem}{Theorem}[section]

\renewcommand{\thetheorem}{\arabic{theorem}.\thesection}
\renewcommand{\thesection}{\arabic{section}.\thechapter}

\begin{document}

\chapter{A chapter}
\section{A section}

\begin{theorem}
There exists some $x$ such that $x^2=2$.
\end{theorem}

\begin{theorem}[A second theorem]
For all $x, y \in \mathbb{R}$:
\begin{itemize}
\item $x(-y) = (-x)y = -(xy)$:
\item $(-x)(-y) = xy$.
\end{itemize}
\end{theorem}

\end{document}


I got the same (space to the right of the theorem head only when using RTL: \setdefaultlanguage[locale=morocco]{arabic}). However, I think this problem is related to mdframed.. because the problem disappears when using framed and ntheorem.

\documentclass[12pt,a4paper]{book}
\usepackage[x11names]{xcolor}
\usepackage{amssymb}
\usepackage{framed}
\usepackage[framed]{ntheorem}

\newframedtheorem{theorem}{مبرهنة}[section]

\renewcommand{\thetheorem}{\arabic{theorem}.\arabic{section}.\arabic{chapter}}

% LANGUAGES AND FONTS SPECS --------------------------------------------

\usepackage{xltxtra}
\usepackage{polyglossia}
\setdefaultlanguage[locale=morocco]{arabic}
\defaultfontfeatures{Ligatures=TeX,%
Mapping=tex-text,%
AutoFakeSlant=0.4%Simulate Italic
}
\setmainfont{Times New Roman}

\begin{document}

\chapter{}

\section{}

\begin{theorem}
لا يوجد عدد جذري حلا للمعادلة:
$x^2=2$.
\end{theorem}

\begin{theorem}[قاعدة الإشارة]
لكل  $x$ و $y$ من
$\mathbb{R}$
لدينا،
\begin{itemize}
\item $x(-y) = (-x)y = -(xy)$،
\item $(-x)(-y) = xy$.
\end{itemize}
\end{theorem}
\end{document}


Here is a screenshot:

When using mdframed instead of framed the problem appears (only for RTL doc):

\documentclass[12pt,a4paper]{book}
\usepackage[x11names]{xcolor}
\usepackage{amssymb}
\usepackage[framemethod=tikz]{mdframed}

\mdtheorem{theorem}{مبرهنة}[section]

\newmdtheoremenv{theorem1}{مبرهنة}[section]

\renewcommand{\thetheorem}{\arabic{theorem}.\arabic{section}.\arabic{chapter}}

% LANGUAGES AND FONTS SPECS --------------------------------------------

\usepackage{xltxtra}
\usepackage{polyglossia}
\setdefaultlanguage[calendar=gregorian,hijricorrection=1,locale=morocco]{arabic}
\defaultfontfeatures{Ligatures=TeX,%
Mapping=tex-text,%
AutoFakeSlant=0.4%Simulate Italic
}
\setmainfont{Times New Roman}

\begin{document}

\chapter{}

\section{}

\begin{theorem}
لا يوجد عدد جذري حلا للمعادلة:
$x^2=2$.
\end{theorem}

\begin{theorem}[قاعدة الإشارة]
لكل  $x$ و $y$ من
$\mathbb{R}$
لدينا
\begin{itemize}
\item $x(-y) = (-x)y = -(xy)$،
\item $(-x)(-y) = xy$.
\end{itemize}
\end{theorem}

\begin{theorem1}
لا يوجد عدد جذري حلا للمعادلة:
$x^2=2$.
\end{theorem1}

\begin{theorem1}[قاعدة الإشارة]
لكل  $x$ و $y$ من
$\mathbb{R}$،
لدينا
\begin{itemize}
\item $x(-y) = (-x)y = -(xy)$،
\item $(-x)(-y) = xy$.
\end{itemize}
\end{theorem1}
\end{document}


which gives now:

Check the bidi package documentation on page 12 (section 1.13); you will need to use \@SepMark instead of ..