Skip to content

Instantly share code, notes, and snippets.

@zr-tex8r
Created October 31, 2019 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zr-tex8r/0f971049992a1b03f2a1a0fc6aa10f10 to your computer and use it in GitHub Desktop.
Save zr-tex8r/0f971049992a1b03f2a1a0fc6aa10f10 to your computer and use it in GitHub Desktop.
TeX:和文文字トークンの和文カテゴリコードの値を展開限定文脈で取得する
%% package declaration
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{tckcatvalue}[2019/10/31 v0.1]
\def\xx@pkgname{tckcatvalue}
%% engine
\chardef\xx@uptex=\ifx\enablecjktoken\@undefined 0 \else 1 \fi
%% inspect the type and set it to \xx@type
% 1 = upTeX, old pTeX
% 0 = new pTeX
\begingroup
\ifnum\xx@uptex=1 \forcecjktoken \fi
\kcatcode`☆=16
\let\xx@tok=☆
\kcatcode`☆=18 \kcatcode`漢=16
\global\chardef\xx@type=\ifcat 漢\xx@tok 1 \else 0 \fi
\endgroup
\PackageWarningNoLine\xx@pkgname
{type = \number\xx@type}
%%<*> \kcatvalue{<ja-char>}
\newcommand*\kcatvalue[1]{%
\number\xx@kcat@value#1\space}
\ifnum\xx@type=\@ne %-----<*type=1>
\begingroup
\ifnum\xx@uptex=1 \forcecjktoken \fi
\kcatcode`漢=16 \global\let\xx@A=漢
\kcatcode`あ=17 \global\let\xx@B=あ
\kcatcode`☆=18 \global\let\xx@C=☆
\ifnum\xx@uptex=1
\kcatcode`#=19 \global\let\xx@D=# % hangul
\else \global\let\xx@D\xx@A % never-matching
\fi
%% \xx@kcat@value{<ja-char>}
\gdef\xx@kcat@value#1{%
% comapre with prepared ja-char tokens
\ifcat \noexpand#1\xx@A 16%
\else\ifcat\noexpand#1\xx@B 17%
\else\ifcat\noexpand#1\xx@C 18%
\else\ifcat\noexpand#1\xx@D 19%
\else 0%
\fi\fi\fi\fi}
\endgroup
\else %-----<*type=0>
\begingroup
\escapechar\m@ne
%% \xx@kcat@value{<ja-char>}
\edef\xx@args{{\string\kanj}{\string\ter}}
\def\xx@next#1#2{%
\gdef\xx@kcat@value##1{%
\ifcat\relax\noexpand##10% avoid complex things
\else \expandafter\xx@kcat@value@a\meaning##1#1@%
\fi}%
% force the ja-char to be explicit through \meaning,
% then inspect the \kcatcode parameter value for the ja-char
\gdef\xx@kcat@value@a##1#1##2@{%
\ifcat @##1@\xx@kcat@value@b##2@%
\else 0\fi}%
\gdef\xx@kcat@value@b##1#2 ##2##3@{%
\kcatcode`##2\space}%
}\expandafter\xx@next\xx@args
\endgroup
\fi %-----</type>
%% done
\endinput
\documentclass{article}
\usepackage{tckcatvalue}
\makeatletter %!!!!!!!!!!!!!!!!!!!!!!!!! TeX code BEGIN
\ifx\ucs\@undefined\else \enablecjktoken \fi
\kcatcode`◎=17
\kcatcode`Я=17
\let\◎=◎
\def\Я{Я}
\kcatcode`字=16
\kcatcode`ア=17
\kcatcode`◎=18
\kcatcode`Я=18
\kcatcode`あ=18 % wow! up new-p old-p
\tracingmacros=1 \tracingonline=1
\typeout{1:字:\kcatvalue 字.} % 16 16 16
\tracingmacros=0 \tracingonline=0
\typeout{2:ア:\kcatvalue ア.} % 17 17 17
\typeout{3:◎:\kcatvalue ◎.} % 18 18 18
\typeout{4:Я:\kcatvalue Я.} % 18 18 18
\typeout{5:\string\◎:\kcatvalue\◎.} % 17 18 17
\typeout{6:\string\Я:\expandafter\kcatvalue\Я.} % 17 18 18
\stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment