Guy Rutenberg

Keeping track of what I do

Getting Hyperref to Work with Hebrew (in XeTeX)

without comments

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}
Share and Enjoy:
  • del.icio.us
  • StumbleUpon
  • Digg
  • Facebook
  • Mixx
  • Google Bookmarks
  • Simpy

Written by Guy

June 27th, 2009 at 1:18 pm

Posted in LaTeX

Tagged with , ,

Leave a Reply