Jul 16 2012

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:

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


Jan 23 2012

Small tip for Hebrew LyX users – Stop the Gibberish

Are you using LyX with Hebrew?

Isn’t it annoying that when CAPSLOCK is on the entire text goes Gibberish?

The problem lays in the fact that by default some of the capital letters are mapped to the hebrew Niqqud characters. There for, when you type ‘S’ for example you will get a Dagesh.

How to solve it? Just edit: “C:\Program Files\LyX20\Resources\kbd\hebrew.kmap” (replace “C:\Program Files\LyX20\” with your installation folder) and comment all the lines (using # as comment) below the line:

# Hebrew points (nikud)

Restart LyX and we are done!


Jan 18 2012

Gaussian Integral

[latexpage]

A Gaussian function (named after one of the greatest mathematician, Carl Friedrich Gauss) is a function of the form: \[ f\left(x\right)=ae^{-\frac{\left(x-b\right)^{2}}{2c^{2}}} \]

We would have like to examine the following integral: \[ I=\intop_{-\infty}^{\infty}e^{-x^{2}}\mathrm{d} x\]

But we are facing a problem, what is the antiderivative function of $e^{-x^{2}}$?

Well, You won’t be able to find one, at least not an elementary one. The integral of a Gaussian is the Gauss error function, but in this special case, there is a cool way to calculate this integral.

At first, Instead of looking at $I$, lets take a look at $I^{2}$. Meaning:
\[ I^{2}=\intop_{-\infty}^{\infty}e^{-x^{2}}\mathrm{d} x\intop_{-\infty}^{\infty}e^{-y^{2}}\mathrm{d} y \]

Note: In the second integral, I’ve changed the variable of integration to y. I can do that, because it’s a completely separated integral.

Now, notice that the following integral is equivalent to the following:\[
\intop_{-\infty}^{\infty}\intop_{-\infty}^{\infty}e^{-x^{2}-y^{2}}\mathrm{d} x\mathrm{d} y
\]
Well, That’s a bit weird. Now we have a double integral! We’ve made the problem much more complex! Or did we?

Well, yes, now we have a double integral, but there is a good reason for that. Now we can change our coordinate system to the polar coordinate system (Meaning: $x=r\cos\theta, y=r\sin\theta$) and remember to multiply by the Jacobian determinant! In our case, the Jacobian determinant is $r$. So, we got:
\[ \intop_{0}^{2\pi}\intop_{0}^{\infty}e^{-r^{2}}r\mathrm{d}r\mathrm{d}\theta \]
But wait a minute, this looks familiar! Notice that:
\[ =-\frac{1}{2}\intop_{0}^{2\pi}\intop_{0}^{\infty}\frac{\mathrm{d}}{\mathrm{d}r}\left(e^{-r^{2}}\right)\mathrm{d}r\mathrm{d}\theta=-\frac{1}{2}2\pi\intop_{0}^{\infty}\frac{\mathrm{d}}{\mathrm{d}r}\left(e^{-r^{2}}\right)\mathrm{d}r=\] \[ =- \pi \left[e^{-r^{2}}\right]_{0}^{\infty}= -\pi\left[0-1\right]=\pi\]
Therefore:
\[ I^{2}=\pi\Rightarrow I=\intop_{-\infty}^{\infty}e^{-x^{2}}\mathrm{d} x = \sqrt{\pi} \]