# Colouring only certain letters as a native property of the document

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

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

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Noto Serif CJK JP} % some suitable font

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

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

%% set up two LaTeX utility macros
\newcommand\ColorizeOn{\directlua{%
colorize_selected_letters, "csl" )}}
\newcommand\ColorizeOff{\directlua{%
luatexbase.remove_from_callback(
"process_input_buffer", "csl" )}}

\begin{document}
%% (the following text is "borrowed" [!] from  https://tex.stackexchange.com/q/437081/5001)
\ColorizeOn
\section{解析モデル概要}

この例題ではに示す橋脚のソリッドモデルを作成します。.

\subsection{基礎と橋脚のジオメトリ作成}

\subsection{梁のジオメトリ作成}

\ColorizeOff
\section{線形解析の実行}

\end{document}

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