## 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:

1. clicking Documet > Settings…
2. Go to the Language tab.
3. 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:

\usepackage[unicode=true] {hyperref}

\def\contentsline#1#2#3#4{%
\ifx\\#4\\%
\csname l@#1\endcsname{#2}{#3}%
\else
\csname l@#1\endcsname{#2}{#3}%
\or % section
\csname l@#1\endcsname{%
}{#3}%
\or % page
\csname l@#1\endcsname{{#2}}{%
}%
\else % all
\csname l@#1\endcsname{%
}{%
}%
\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.

This site is hosted by:

#### 16 Responses to “LaTeX – Using hyperref with RTL languages (such as Hebrew)”

• Latex - Fixing the brackets in hyperref bookmarks for RTL languages (such as Hebrew) | DiGMi Says:

[…] – Fixing the brackets in hyperref bookmarks for RTL languages (such as Hebrew) In my last post, I've shown a way to fix the hyperref package so it would work with RTL […]

• Hebrew Support in Hyperref – Situation Review by Guy Rutenberg Says:

• Guy Says:

I’m trying to play around with hyperref and Hebrew, but for some reason I’m unable to use your fix. It’s probably something small I’m missing. Can you please post (or email me) an example of a complete LaTeX document with Hebrew TOC? Thanks

Hi, I have no idea where to put this code… help a beginner?

• Yaroslav Says:

Hi,

I had tried to compile my course notes in Hebrew (in LyX) using hyperref with pdfLaTeX, however I’ve got the following error:

“!pdfTeX error: pdflatex: 1 unmatched \pdfsave after page shipout”

Yaroslav

P.S.,
I use a two-sided two-column document with fancy headings. A complete set of the packages is:

\documentclass[twoside,twocolumn,english,hebrew,leqno]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8x]{inputenc}
\usepackage{fancyhdr}
\usepackage{color}
\usepackage{babel}
\usepackage{array}
\usepackage{booktabs}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathdots}
\usepackage{graphicx}
\usepackage{setspace}
\usepackage{esint}
\usepackage{xargs}[2008/03/08]
\PassOptionsToPackage{version=3}{mhchem}
\usepackage{theorem}
\usepackage{color}
\usepackage[samesize]{cancel}
\usepackage{graphicx}
\usepackage{textcomp}

Is it possible to change this code so that the references will not be inside a red square in the PDF?

• DiGMi Says:

You can change the color setting just like any other links. Just add the relevant code to the preamble. If you are having problems let me know.

• נחי Says:

היי אור, אני מקווה שזה בסדר לכתוב בעברית.

קודם – תודה על העזרה!

בעיה שמתעוררת אצלי: עשיתי צעד אחר צעד כפי שהראית והסתדר לי יפה. יש רק בעיה משונה אחת: ביצוא של הקובץ, משום מה מתווספת האות “L” בסוף של כל נוסחה מתמטית.

• Alex Goldvard Says:

Hi.

I am trying to use your code but I get the link itself in the wrong place, not on the word. See my LaTeX file:
========================================
\documentclass[10pt]{article}
\usepackage[utf8x]{inputenc}
\usepackage[english,hebrew]{babel}

\usepackage[unicode=true] {hyperref}

\def\contentsline#1#2#3#4{%
\ifx\\#4\\%
\csname l@#1\endcsname{#2}{#3}%
\else
\csname l@#1\endcsname{#2}{#3}%
\or % section
\csname l@#1\endcsname{%
}{#3}%
\or % page
\csname l@#1\endcsname{{#2}}{%
}%
\else % all
\csname l@#1\endcsname{%