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.


Dec 23 2011

Bug in LyX for Mac – \Omega, \otimes are not being displayed on designer

If you are using LyX on mac you probably noticed that some symbols are missing while editing an equation.

One of the symbols missing is $latex \Omega$ which is really annoying when you learn probability.
When you try and type \Omega, instead of seeing the symbol you will just see a blank space.

I’ve dived into the code, and found out how the all designer works. For most of the symbols, the translation is quite simple. There are several font files, and a file named symbols (located in /Application/LyX.app/Contents/Resources) which maps every latex code to the relevant font and unicode value.

The problem with $latex \Omega$ and $latex \otimes$ is that their unicode value is 0x00ad. And as it seems, QT4 has bug with characters with that code. LyX source code mentioned this bug in GuiPainter.cpp and it have some code to try and fix it, but for some reason it doesn’t work.

A work around for the $latex \Omega$ is to make a small change in the symbols file, locate the Omega line (with capital O) and change the code from 173 to 937 (a code for different Omega character that is available in the font).

I didn’t find (didn’t really looked for) a way to fix the \otimes, because i don’t really use it. A way to fix it will be using a font editor and changing the font and map the wanted symbols from other unused code. Again, didn’t care enough to do it, If someone want me to just post here. But I believe that LyX developers will soon enough fix it.


Dec 18 2011

Compiling LyX on Mac

When you learn mathematics at the university, and you are a bit of computer’s freak, you will probably want to take lecture notes on the computer.

You are all probably familiar with $latex \LaTeX$ (if not, well… It’s the standard way to write type equations). The best editor for LaTeX is LyX (Well, maybe best is a strong word, The less of all evil is much better).

Recently I’ve bought a new MacBook Air (Mac OS X Lion version 10.7.2. i7 processor), And I’m using it in the university to take some lecture notes. Unfortunately, the LyX release version for mac is a little bit buggy… (for example, when you try to open a file when another file is open it fails most of the time, and if you insist LyX will probably crash. Some characters, such as $latex \Omega$, are not being displayed on editor mode). So I’ve decided to download the source code from their SVN and compile the latest development version.

To make a long story short, I’ve encountered a lot of errors, And I’ve decided to list them here so if you will get them you will be able to solve them much quicker than me.

First error – ICONV_CONST not defined

The first error I’ve encountered was the following:

docstream.cpp: In member function ‘std::codecvt_base::result<unnamed>::iconv_codecvt_facet::do_iconv(void*, const char**, size_t*, char**, size_t*) const’:
docstream.cpp:264: error: expected `>' before ‘ICONV_CONST’
docstream.cpp:264: error: expected `(' before ‘ICONV_CONST’
docstream.cpp:264: error: ‘ICONV_CONST’ was not declared in this scope
docstream.cpp:264: error: expected primary-expression before ‘>’ token

Here the solution is simple. The problem is that the configure doesn’t config the ICONV_CONST properly. The solution to edit the config.h file located in the lyx-devel directory, and add the following line:

#define ICONV_CONST

And now it will work wonderfully.

Second error – Undefined symbols for arh

The second error was much more annoying, and I’ve even destroyed my Mac by mistake while trying to solve it (Had to reinstall it). The solution is a bit more difficult, but no worries.

The error I got, was the following linking error:

Undefined symbols for architecture x86_64:
  "_libiconv_open", referenced from:
      lyx::IconvProcessor::init()     in liblyxsupport.a(unicode.o)
      (anonymous namespace)::iconv_codecvt_facet::iconv_codecvt_facet(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::_Ios_Openmode, unsigned long)in liblyxsupport.a(docstream.o)
  "_libiconv", referenced from:
      lyx::IconvProcessor::convert(char const*, unsigned long, char*, unsigned long)in liblyxsupport.a(unicode.o)
      (anonymous namespace)::iconv_codecvt_facet::do_out(__mbstate_t&, wchar_t const*, wchar_t const*, wchar_t const*&, char*, char*, char*&) constin liblyxsupport.a(docstream.o)
      (anonymous namespace)::iconv_codecvt_facet::do_in(__mbstate_t&, char const*, char const*, char const*&, wchar_t*, wchar_t*, wchar_t*&) constin liblyxsupport.a(docstream.o)
  "_libiconv_close", referenced from:
      lyx::IconvProcessor::convert(char const*, unsigned long, char*, unsigned long)in liblyxsupport.a(unicode.o)
      lyx::IconvProcessor::Impl::~Impl()in liblyxsupport.a(unicode.o)
      (anonymous namespace)::iconv_codecvt_facet::~iconv_codecvt_facet()in liblyxsupport.a(docstream.o)
      (anonymous namespace)::iconv_codecvt_facet::~iconv_codecvt_facet()in liblyxsupport.a(docstream.o)
ld: symbol(s) not found for architecture x86_64

There seems to be a problem with the built-in libiconv library that comes with the OS. The solution was to get the latest source code from GNU, and installing it.

The way to install it is as follows:

Download the archive file, open a terminal in the folder with the archive and type the following commands:

tar -xzvf libiconv-1.14.tar.gz
cd libiconv-1.14

./configure '--prefix=/usr/local' 'CC=gcc -arch i386 -arch x86_64' 'CXX=g++ -arch i386 -arch x86_64' 'CPP=gcc -E' 'CXXCPP=g++ -E'

make
sudo make install

Note: It is very important NOT to install the lib to “/usr/lib“. but to “/usr/local/lib” (That’s they way I’ve messed up my all OS, and it wouldn’t boot afterwards).

After installing the libiconv, you will need to configure and install LyX  using the following commands (assuming you are in the lyx-devel directory):

LDFLAGS="-L/usr/local/lib" ./configure --with-version-suffix=-2.0 --with-libiconv-prefix=/usr/local --with-x=no --disable-stdlib-debug

make
make install

Note: You will probably encounter again the first error I had, because running the configure recreate the config.h file. Just reedit the config.h file as mentioned in the last section.

And that’s it!

It seems like in the current development version the opening bug is fixed, but the missing symbols are still missing. Hopefully it will be fixed soon (I’m looking into it).