Getting Hyperref to Work with Hebrew (in XeTeX)

The hyperref package is notoriously known to cause problem with RTL text, which unfortunately include Hebrew. In this post I present some preliminary workarounds that enable the user to use the hyperref package with Hebrew and possibly other RTL languages. The solution requires XeTeX which is available in TeXLive. I had no success, yet, to port the workaround to pdfTeX, which is more popular.

The problem is that because TeX doesn’t store information regarding the direction of text inside the dvi, RTL text is actually stored in reverse. When a link is inserted in such text, the command to start the link ends up after the command to end the link, which is problematic. When trying to compile such document with pdfTex it throws errors like:

! pdfTeX error (ext4): pdf_link_stack empty, \pdfendlink used without \pdfstart

On the otherhand XeTeX behaves a little nicer and compiles the document, but the wrong parts of the text are marked as links.

My workaround basically changes the order of the start and end link commands. Thus, in RTL text the commands get reversed and actually end up in the correct order. The workaround doesn’t support links spanning multiple lines and color links (although link border is supported). Further work needs to be done in order to support links in the table of contents. I could get it to work only in LTR mode (which isn’t good). You should insert the following code to your preamble in order to enable the workaround.

\makeatletter
\def\hyper@link#1#2#3{%
  \if@rl
    \setLR
      \begingroup
      \hyper@linkend
        #3
      \hyper@linkstart{#1}{#2}
    \setRL
  \else
    \hyper@linkstart{#1}{#2}
        #3
    \hyper@linkend
  \fi
}
\makeatother

I’ll also note that in order to generate correct pdf bookmarks (not in gibberish), you should set unicode=false in the hyperref‘s options.

You can find a working example bellow (and the corresponding PDF output here).

\documentclass{article}
\usepackage{fontspec}
\usepackage{xunicode}
\usepackage{bidi}

\usepackage[unicode=false,
 bookmarks=true,bookmarksnumbered=false,bookmarksopen=false,
 breaklinks=false,pdfborder={0 0 1},backref=false,colorlinks=false]
 {hyperref}

\makeatletter
\def\hyper@link#1#2#3{%
  \if@rl
    \setLR
      \begingroup
      \hyper@linkend
        #3
      \hyper@linkstart{#1}{#2}
    \setRL
  \else
    \hyper@linkstart{#1}{#2}
        #3
    \hyper@linkend
  \fi
}
\makeatother
\setromanfont{Times New Roman}

\begin{document}
\setRL
\title{בדיקה}
\author{גיא רוטנברג}
\date{}
\maketitle
\pagebreak{}

\section{סעיף ראשון}\label{testlabel}
\subsection{תת-סעיף ראשון}\label{testlabel2}

לה לה

לי לי

לו לו

\pagebreak{}
\section{סעיף שני}
\subsection{תת-סעיף ראשון}

לה לה

\subsection{תת-סעיף שני}

לי לי
חלק \ref{testlabel}
אבגד
\hyperref[testlabel2]{בדיקה ארוכה}

\setLR
This is a test for LTR links. See section \ref{testlabel} and \hyperref[testlabel2]{long label}.
\end{document} 

11 thoughts on “Getting Hyperref to Work with Hebrew (in XeTeX)”

  1. As I said, I couldn’t get it to work on pdfTeX, but maybe if you’ve more time than I did the research this issue you could make it work. If you succeed, it will be nice if you could drop a word.

  2. Looks like a nice solution. I haven’t tried it yet, but did you figure out why you need the ps2pdf step? Does it work with links in the content itself or only links in the TOC?

    It’s really nice to see that people are working to get LaTeX to properly support Hebrew.

  3. mainly because I’m using pstricks which is supported only by ps2pdf
    I didn’t really check for links in the content, It might be a little buggy.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.