I used a tool from Word to study Japanese, which enabled me to colour only certain characters of a lyric while listening to the music, the effectiveness of that was ridiculously powerful and I wish to mirror that with LaTeX so,

Instead of colouring each character あ I look for a way of doing all あ's in a document, which will be kinda big...

  • 1
    Please tell us which TeX engine you use -- pdfLaTeX, XeLaTeX, or LuaLaTeX? Please also edit your posting to post a sample text block that could be used to test potential solutions. (Not everyone who can program up LaTeX has ready access to Japanese text...) – Mico Apr 6 at 5:55

Assuming you use LuaLaTeX to typeset your document, you may achieve your formatting objective by setting up a Lua function that applies the desired per-character colorizing operations "on the fly". The crucial work is performed by the unicode.utf8.gsub function calls. Just place the characters you wish to colorize into the square-bracket parts ("character classes" in Lua terminology) in the second arguments of unicode.utf8.gsub.

The solution also provides two LaTeX macros, called \ColorizeOn and \ColorizeOff; they activate and deactivate the Lua function.

enter image description here

\setmainfont{Noto Serif CJK JP} % some suitable font

\usepackage{xcolor}  % for '\textcolor' macro
\usepackage{luacode} % for 'luacode' environment 

%% set up the main Lua function
function colorize_selected_letters ( s )
  s = unicode.utf8.gsub ( s , "[めリ脚]" , "\\textcolor{red}{%0}" )
  s = unicode.utf8.gsub ( s , "[すの義]" , "\\textcolor{blue}{%0}" )
  return s 

%% set up two LaTeX utility macros
   luatexbase.add_to_callback( "process_input_buffer", 
   colorize_selected_letters, "csl" )}}
   "process_input_buffer", "csl" )}}

%% (the following text is "borrowed" [!] from  https://tex.stackexchange.com/q/437081/5001)





| improve this answer | |
  • 1
    Thank you a lot hehe it worked just amazingly <3 – Bruno Stühler Apr 6 at 12:51

Your Answer

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

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