2

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
2

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

\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{%
   luatexbase.add_to_callback( "process_input_buffer", 
   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}
| 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.