1

Org-table works as expected when it comes to English characters.

| a | b | c | d | e |
| f | g | h | i | j |
| k | l | m | n | o |
| p | q | r | s | t |
| u | v | w | x | y |
| z |   |   |   |   |

However, if I try to do the same with Devanagari script (itrans inscript), table columns don't get aligned properly.

| क | ख | ग | घ | ङ |
| च | छ | ज | झ | ञ |
| ट | ठ | ड | ढ | ण |
| त | थ | द | ध | न |
| प | फ | ब | भ | म |

Is there a way to align columns where cell values are in Devanagari font? I tried (set-face-attribute 'org-table nil :inherit 'fixed-pitch) which seems to have no effect.

5 Answers 5

2

However, if I try to do the same with Devanagari script (itrans inscript), table columns don't get aligned properly.

Use valign from GNU ELPA.

From the blurb

This package provides visual alignment for Org Mode, Markdown and table.el tables on GUI Emacs. It can properly align tables containing variable-pitch font, CJK characters and images. Meanwhile, the text-based alignment generated by Org mode (or Markdown mode) is left untouched.

For the sake of demonstration, this is what I did

  1. Install valign with M-x pacakge-refresh-contents and M-x package-install RET valign RET

  2. Add the following to your init file (or eval it)

(add-hook 'org-mode-hook #'valign-mode)
  1. Copy your devanagari table to an org file.
| क | ख | ग | घ | ङ |
| च | छ | ज | झ | ञ |
| ट | ठ | ड | ढ | ण |
| त | थ | द | ध | न |
| प | फ | ब | भ | म |
  1. Do M-x revert-buffer when in above org buffer

  2. Mission accomplished.

What you see below is the screenshot of before and after valign is activated.

An org buffer with indic script without and with valign-mode

valign is disabled when the table size exceeds a specific threshold. So, if you happen to notice that some of your tables are mis-aligned, even when valign-mode is ON, then you may have to configure valign-max-table-size to a higher value.

For the sake of record, and if you haven't already noticed ... You DO NOT need to do all the jugglery with fonts and their widths suggested in the other responses. This is because the valign library measures pixel-width when doing alignment.

2
  • 1
    Yeah your answers are probably better than mine. Upvoted. Unfortunately like so much that's new in emacs it completely undocumented 😑
    – Rusi
    Commented Apr 29, 2022 at 8:34
  • this works like a charm, thank you!
    – jimmu
    Commented Apr 30, 2022 at 23:16
2

You need to find a fixed–pitch Devanagari font where the Devanagari glyphs are exactly the same width as the glyphs for ASCII characters.

2
  • Now I remember I tried this approach some time ago and had to abandon because it did not work. Perhaps the fixed-pitch font I chose was incorrect. Would appreciate font recommendation if you have worked with it.
    – jimmu
    Commented Apr 28, 2022 at 21:12
  • 1
    No, I don’t write in Devanagari so I don’t have any fonts to recommend.
    – db48x
    Commented Apr 29, 2022 at 6:42
2

This is not a answer to the original question, but a response to the comment https://emacs.stackexchange.com/a/71562/31220.

FWIW, the answer is in https://emacs.stackexchange.com/a/71569/31220.

PS: I don't have 50+ reputation to Add a comment, so I will go with Post your answer as an expedient.


FWIW, the elisp code there does transliteration from English-letters to Devanagari-letters. The code that does the transliteration part is this

(execute-kbd-macro inp)

Instead of using execute-kbd-macro, it would be much cleaner to use indian-translate-region.

Let me clarify what I mean ...

  1. Copy the table below to a temp buffer
| ka  | kha  | ga | gha | ~Na |
| cha | chha | ja | jha | ~na |
| Ta  | Tha  | Da | Dha | Na  |
| ta  | tha  | da | dha | na  |
| pa  | pha  | ba | bha | ma  |
  1. Mark the whole table with C-SPC etc etc

  2. Then do

M-: (indian-dev-itrans-v5-decode-region (region-beginning) (region-end))
  1. You will get transliterate-d text as below.

Trasliterating from English to Devanagari

For the sake of record,

  1. In the above example, one moved from English to Devanagari. You can also transliterate from Devanagari to English using indian-dev-itrans-v5-encode-region.

  2. Transliteration works NOT just for Devanagari, but for any indic script, say Tamil. See the above encode, decode functions or dive in to indian-translate-region for more details.

1

Here's one recipe for getting fixed-width Devanagari glyphs (with limitations):

  • download a fixed-width Devanagari font: I found one here.

  • assuming you are on Linux, create a /usr/share/fonts/monotty directory and copy the downloaded file there. You will need to use sudo (or otherwise be root) for this step. This is needed if you want the font to be available to every user of the system. Alternatively, you can install in ~/.fonts: no need to be root, but only you get to use it.

  • Refresh the font cache [1]: fc-cache -v

  • Add this to your init file [2]: (set-fontset-font t 'devanagari "Monotty")

  • Restart emacs to pick up the new font.

Now press TAB on your table above and you should get proper alignment.

If that does not work, check all the steps carefully and use C-u C-x = on a Devanagari glyph and check the font that it reports. It should read: ftcrhb:- OS -Monotty-regular-normal-normal-*-13-*-*-*-m-0-iso10646-1 (#x2D) or something similar. Without the monotty font, I was getting this instead: ftcrhb:-GNU -Denemo-regular-normal-normal-*-15-*-*-*-*-0-iso10646-1 (#x64E) although it may be different in individual cases, depending on what fonts are installed.

Limitations:

  • Although the Devanagari glyphs are fixed-width, they do NOT have the same width as other glyphs (Roman glyphs e.g.), so you cannot mix Devanagari glyphs and Roman glyphs in the same column, although having some columns consisting exlusively of Roman glyphs and some other columns exclusively of Devanagari glyphs should work.

  • The Devanagari strings in a column have to consist of the same number of glyphs, otherwise some columns have more spaces than others and the space glyph has a different width, destroying the alignment. It would be nice to find a monospaced Devanagari font whose glyphs have the same width as the glyphs of the other fonts that make up a table as @db48x recommends in his answer, but I don't know of one.

  • Maybe others... I have only tested the table you provide plus simple extensions like those described in the previous bullet.


[1] @db48x in his comment states that font caches are per-user. The Fedora docs say to run sudo fc-cache -v for system font installation. I haven't tested enough to make sure that @db48x is correct, but I'm taking him at his word and assuming that the docs are not quite correct.

[2] I found this in this Emacs SE question, but with a different font that AFAIK is not monospaced.

7
  • 1
    You need not be root to install a font. Just copy the font file into ~/.fonts instead of /usr/share/fonts. You won’t need to be root to run fc-cache either, as the caches are stored per–user.
    – db48x
    Commented Apr 28, 2022 at 5:33
  • 1
    OK - fixed it up a bit. Thanks!
    – NickD
    Commented Apr 28, 2022 at 5:58
  • 1
    You can check man fc-cache for a hint: “%cachedir%/*-%arch%.cache-%version%: These files are generated by fc-cache and contain maps from file names to font properties” along with a pointer to /usr/share/doc/fontconfig/fontconfig-user.html which explains that “The default directory is ``$XDG_CACHE_HOME/fontconfig''”.
    – db48x
    Commented Apr 28, 2022 at 6:39
  • thanks, will give this font a try. ideally I would want column width to adjust according to the largest string on that column. will report back (I am using mac btw) how it goes.
    – jimmu
    Commented Apr 28, 2022 at 21:17
  • 1
    I don't think you are going to get that with this font: see the second limitation I mention.
    – NickD
    Commented Apr 28, 2022 at 23:03
1

This may not be exactly an answer. And its an acquired taste.

I write my files in roman thus

| ka  | kha  | ga | gha | ~Na |
| cha | chha | ja | jha | ~na |
| Ta  | Tha  | Da | Dha | Na  |
| ta  | tha  | da | dha | na  |
| pa  | pha  | ba | bha | ma  |

And generate

| क  | ख  | ग | घ | ङ |
| च | छ | ज | झ | ञ |
| ट  | ठ  | ड | ढ | ण  |
| त  | थ  | द | ध | न  |
| प  | फ  | ब | भ | म  |

The generating code is

(setq rpm-input-method "devanagari-itrans")

(defun rpm-apply-input-method ()
  "buffer to buffer apply input method with buffering windowing stuff mixed up"
  (interactive)
  (let* ((inp (buffer-substring-no-properties (point-min) (point-max)))
     (filename (file-name-nondirectory (buffer-file-name)))
     (outname (concat (file-name-sans-extension filename)
                   "-hi"
                   (file-name-extension filename t)))
     (p))
    (switch-to-buffer-other-frame outname)
    (save-excursion
      (save-window-excursion
    (setq buffer-file-coding-system 'utf-8)
    (set-input-method rpm-input-method t)
    (setq p (point))
    (erase-buffer)
    (execute-kbd-macro inp)))
    (goto-char p)
    (other-frame 1)))

(global-set-key [f4] 'rpm-apply-input-method)

1
  • hmm...generated output still seems misaligned tho. My problem is not with the typing Devanagari part but that ugly looking table :)
    – jimmu
    Commented Apr 28, 2022 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.