I'm not sure if the following question has a solution (and if there is one if it is still in the scope of what one can achive with Latex) but I hope its alright to at least ask.

I'm currently dealing with texts of the following form

私は 東京に 住(す)んで います。東京は4月から 春(はる) です。
暖(あたた)かい です。桜(さくら)が きれい です。天気(てんき)も いい です。
6月から雨(あめ)の 季節(きせつ)が 始(はじ)まります。梅雨(つゆ)と 言(い)います。

and I've been wondering if it is possible to automatically convert this, using Latex, to

私は 東京に \ruby{住}{す}んで います。東京は4月から \ruby{春}{はる} です。
\ruby{暖}{あたた}かい です。\ruby{桜}{さくら}が きれい です。\ruby{天気}{てんき}も いい です。
6月から\ruby{雨}{あめ}の \ruby{季節}{きせつ}が \ruby{始}{はじ}まります。\ruby{梅雨}{つゆ}と \ruby{言}{い}います。

What is happening here is that you:

  1. take each "block" of Kanji that is followed by parenthesis and add a \ruby{} command around it,
  2. replace the parenthesis with {}.

This seems rather simple, but I see at least two possible problems:

  1. Since the parenthesis are coming after the "block" of Kanji one would probably need to go thorugh the text at least twice, once for determining where the parenthesis are and once for actually inserting the \ruby command.
  2. Its highly non trivial to determine how many Kanji before a parenthesis need to be placed inside the \ruby command. I honestly can't think of a direct solution for this, but as a work around one could say that we only take the last Kanji before the parenthesis and put it in the \ruby command...

As I said in the beginning, I'm not sure if this is possible, but I would appreciate your input on the matter anyways!

As an MWE I'll leave you with both versions (compile with XeTeX):


私は 東京に 住(す)んで います。東京は4月から 春(はる) です。暖(あたた)かい です。桜(さくら)が きれい です。天気(てんき)も いい です。6月から雨(あめ)の 季節(きせつ)が 始(はじ)まります。梅雨(つゆ)と 言(い)います。
私は 東京に \ruby{住}{す}んで います。東京は4月から \ruby{春}{はる} です。\ruby{暖}{あたた}かい です。\ruby{桜}{さくら}が きれい です。\ruby{天気}{てんき}も いい です。6月から\ruby{雨}{あめ}の \ruby{季節}{きせつ}が \ruby{始}{はじ}まります。\ruby{梅雨}{つゆ}と \ruby{言}{い}います。
  • It would probably be much easier to do this with find and replace in your editor, or with awk,sed,perl etc. if you want an automated solution. – Marijn May 6 at 19:24
  • 1
    you could do this in luatex by using lua to edit the text before passing to tex, in xetex, I would do it using an editor or perl etc before running xetex – David Carlisle May 6 at 19:33
  • Some LaTeX editors can be macro enhanced to do as you request however as it is done sans TeX any good programmers editor or programmable Find 'n Replace Tool should work just as well for pre-processing – user170109 May 6 at 19:33
  • @DavidCarlisle Switching to luatex wouldn‘t be a problem. The problem is that I don‘t know how to code in lua... Could you maybe suggest a solution? – Sito May 6 at 19:42
  • 2
    alternatively, in xetex, are the blocks always marked by a space so it is <space>kanji-sequence<space>(ruby) if so you may be able to define an active space to do the right thing? – David Carlisle May 6 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.