Guy Rutenberg

Keeping track of what I do

Getting Hyperref to Work with Hebrew (in XeTeX)

with 4 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 , ,

4 Responses to 'Getting Hyperref to Work with Hebrew (in XeTeX)'

Subscribe to comments with RSS or TrackBack to 'Getting Hyperref to Work with Hebrew (in XeTeX)'.

  1. Is there _any_ way I can make something like that work with pdf(e)tex?…

    Eyal Rozenberg

    23 Oct 11 at 09:16

  2. 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.

    Guy

    11 Nov 11 at 15:45

  3. [...] Basically, you can’t get links with right-to-left text. It’s about the direction rather than the non-Latin language – somehow the link-start command is placed at the end due to some sort of reversal. The problem is described in Guy Rutenberg’s blog, here. [...]

  4. [...] Basically, you can’t get back links with right-to-left text. It’s concerning the path as opposed to the non-Latin phrases – somehow the link-start command is positioned in the complete on account of some sort of reversal. The problems is described in man Rutenberg’s blog, here. [...]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">