LaTeX – Using hyperref with RTL languages (such as Hebrew)
Long ago I’ve tried to use hyperref(which allowing links inside the documents, and bookmarks in pdf output) package in latex while writing in Hebrew. Sadly I’ve failed and didn’t have time to check why.
Today, After encountering this post by a guy asking for help in this situation I’ve managed to work around most of the problems.
At first I’ve found Guy Rutenberg’s post about “Getting Hyperref to Work with Hebrew (in XeTeX)“.
Which made me understand why pdfLaTeX failed miserably.
Guy used XeTeX, which might be fine for you, but I’m using a lot of packages and some of them just doesn’t support XeTeX. So, I thought, maybe ps2pdf will work?
Now it is a good time to say, I’m using LyX. I’m not writing the latex code directly, so that’s why my next choice was ps2pdf, because it’s one of LyX defaults.
I’ve tried to compile a simple article with a table of contents and sections. I had a small smile on my face when it managed to compile, but when I’ve opened the PDF it was one big mass. All the bookmarks were blank. After playing around with it, I figured out that the problem was the encoding of the document. For some reason LyX doesn’t like Unicode so it doesn’t use it by default and ps2pdf doesn’t like other Hebrew encoding at all.
So, first of all, you will have to change the encoding. In LyX You can do that by:
- clicking Documet > Settings…
- Go to the Language tab.
- In the Encoding section choose Other and then Unicode (ucs-extended) (utf8x).
If you are writing a latex code, just write:
\usepackage[utf8x]{inputenc}
In the preamble (and remove the other inputenc importing if exists).
Now when I’ve compiled the document the bookmark were OK, but the links in the table of content weren’t in the right place. The problem as it seems to be, was the very same problem Guy mentioned, parts of document are being processed in a reversed order because of the RTL. The solution for that, was to find the specific buildup of the TOC and change the RTL to LTR before setting up the link and then back for the text itself. So, when I’ve added the following to the preamble everything worked just fine:
\PreloadUnicodePage{5} \usepackage[unicode=true] {hyperref} \def\contentsline#1#2#3#4{% \ifx\\#4\\% \csname l@#1\endcsname{#2}{#3}% \else \ifcase\Hy@linktoc % none \csname l@#1\endcsname{#2}{#3}% \or % section \csname l@#1\endcsname{% \beginL\hyper@linkstart{link}{#4}{\R{#2}}\hyper@linkend\endL }{#3}% \or % page \csname l@#1\endcsname{{#2}}{% \hyper@linkstart{link}{#4}{#3}\hyper@linkend }% \else % all \csname l@#1\endcsname{% \hyper@linkstart{link}{#4}{#2}\hyper@linkend }{% \hyper@linkstart{link}{#4}{#3}\hyper@linkend }% \fi \fi }
By adding it, you are replacing the hyperref‘s contentsline function. The only difference between the original function and the new one is at line 13. the \beginR, \endR and \L{…}.
I’m not really sure if it’s 100% bulletproof, so let me know if you encounter any problems.