Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LaTeX: (u)pLaTeXでxspaceするためのやつ
%%
%% This is file 'pxxspace.sty'.
%%
%% Copyright (c) 2019 Takayuki YATO (aka. "ZR")
%% GitHub: https://github.com/zr-tex8r
%% Twitter: @zr_tex8r
%%
%% This package is distributed under the MIT License.
%%
%% package declaration
\NeedsTeXFormat{pLaTeX2e}
\ProvidesPackage{pxxspace}[2019/04/30 v0.2]
\def\pxxs@pkgname{pxxspace}
%--------------------------------------- preparations
%% e-TeX is required
\RequirePackage{etoolbox}[2011/01/03]% v2.1
\ifx\numdef\@undefined \endinput \fi
%% packages
\RequirePackage{xspace}[2009/10/20]% v1.13
%% check if patchable
\chardef\pxxs@tmpa=\@ne
\ifpatchable\@xspace{%
\@xspace@lettoken@if@letter@TF
}{}{\chardef\pxxs@tmpa=\z@}
\ifpatchable\@xspace@eTeX@setup{%
\begingroup
}{}{\chardef\pxxs@tmpa=\z@}
\ifnum\pxxs@tmpa=\z@
\PackageError\pxxs@pkgname
{Failure in patching}%
{Package loading is aborted.\MessageBreak\@ehc}%
\expandafter\endinput\fi\relax
%--------------------------------------- helpers
%% packages
\RequirePackage{pdftexcmds}
%% engine check
\newif\ifpxxs@uptex
\pdf@isprimitive\forcecjktoken\forcecjktoken{\pxxs@uptextrue}{}
%% \pxxs@forcecjktoken
\ifpxxs@uptex
\let\pxxs@forcecjktoken\forcecjktoken
\else
\let\pxxs@forcecjktoken\relax
\fi
%--------------------------------------- adjustments to xspace
%% \pxxs@jt@XXX : probe tokens
\begingroup
\def\do#1#2#3{%
\kcatcode#2=#3\relax \kansujichar1=#2\relax
\edef\next{%
\global\let\noexpand#1=\scantokens\expandafter{\kansuji1}%
}\next}
\pxxs@forcecjktoken
\everyeof{\noexpand}% disable EOF marker
\do\pxxs@jt@kanji{\jis"3441}{16}%
\do\pxxs@jt@kana{\jis"2422}{17}%
\do\pxxs@jt@other{\jis"2123}{18}%
\ifpxxs@uptex
\do\pxxs@jt@hangul{\ucs"D55C}{19}%
\fi
\endgroup
%% \pxxs@lettoken@if@jachar{<true>}{<false>}
% Checks if \@let@token is a ja-character token.
% NB: Here \@let@token might be an \outer-token.
\let\@let@token\relax
\def\pxxs@tmpa#1#2{\unexpanded{%
\ifcat\noexpand\@let@token#1\expandafter\@firstoftwo
\else \expandafter#2\fi}}
\edef\pxxs@lettoken@if@jachar{%
\pxxs@tmpa\pxxs@jt@kana\pxxs@lettoken@if@jachar@a}
\edef\pxxs@lettoken@if@jachar@a{%
\pxxs@tmpa\pxxs@jt@kanji\pxxs@lettoken@if@jachar@b}
\edef\pxxs@lettoken@if@jachar@b{%
\pxxs@tmpa\pxxs@jt@other\pxxs@lettoken@if@jachar@c}
\edef\pxxs@lettoken@if@jachar@c{%
\pxxs@tmpa\pxxs@jt@hangul\@secondoftwo}
\unless\ifpxxs@uptex
\let\pxxs@lettoken@if@jachar@c\@secondoftwo
\fi
%% \pxxs@precheck
% Placed just before \@xspace@lettoken@if@letter@TF to modify the
% behavior of \@xspace so that spaces are omitted if the following
% token is a ja-character.
\def\pxxs@precheck#1{%
\pxxs@lettoken@if@jachar{\@gobbletwo}{#1}%
}
%% patch \@xspace
\patchcmd\@xspace{%from
\@xspace@lettoken@if@letter@TF
}{%to
\pxxs@precheck
\@xspace@lettoken@if@letter@TF
}{}{}
%% patch \@xspace@eTeX@setup
\patchcmd\@xspace@eTeX@setup{%from
\begingroup
}{%to
\begingroup
\pxxs@forcecjktoken
}{}{}
%--------------------------------------- done
\endinput
%% EOF
%#!platex
\documentclass[a4paper]{jsarticle}
\usepackage{pxxspace}
\newcommand\myTeX{\TeX\xspace}
\begin{document}
%\noautoxspacing
\myTeX is great!\par
\myTeX, it's awesome!\par
\myTeX (not \LaTeX) is great!\par
\myTeX はアレ!\par
\myTeX、超絶アレ!\par
\myTeX\LaTeX でない)はアレ!\par
\myTeX 言語、ダメゼッタイ!\par
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.