Skip to content

Instantly share code, notes, and snippets.

@zr-tex8r
Last active June 23, 2019 02:39
Show Gist options
  • Save zr-tex8r/261ab73c06368e03e4ff to your computer and use it in GitHub Desktop.
Save zr-tex8r/261ab73c06368e03e4ff to your computer and use it in GitHub Desktop.
LaTeX: ヤバくない \text の実装
%% bxamstext.sty
%% package declaration
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{bxamstext}[2015/12/19 v0.2]
%% preparation
\def\bxtt@pkgname{bxamstext}
%% load packages
\RequirePackage{calc}
\RequirePackage{zref}
%--------------------------------------- amstext
%% load amsmath, so taht:
% - patches to some LaTeX commands are reverted
% - switch 'firstchoice@' is annihilated
\def\bxtt@tmpa{% commands to be reverted
\do\stepcounter \do\addtocounter
\do\GenericInfo \do\GenericWarning \do\GenericError}
\@ifpackageloaded{amstext}{}{%else
\def\do#1{\expandafter\let\csname$\string#1\endcsname#1%
}\bxtt@tmpa
\RequirePackage{amstext}% load
% revert the patch
\def\do#1{\expandafter\let\expandafter#1\csname$\string#1\endcsname%
\expandafter\let\csname$\string#1\endcsname\@undefined
}\bxtt@tmpa
}
\let\bxtt@tmpa\relax
% 'firstchoice@' is always true
\firstchoice@true
\let\firstchoice@true\relax
\let\firstchoice@false\relax
%--------------------------------------- new version of \text
%% variables
\newcount\bxtt@id
%% zref settings
\zref@newlist{bxtt}
\zref@newprop{bxtt@ms}[0]{0}
\zref@addprop{bxtt}{bxtt@ms}
%% redeine \text@
% (this is the main process '\text' in math mdoe.)
\def\text@#1{%{<text>}
\begingroup
\bxtt@gen@label
\bxtt@text@out{\zref@extract{\bxtt@label}{bxtt@ms}}{#1}%
\bxtt@inspect@ms
\endgroup}
\def\bxtt@text@out#1#2{%
%\message{<\bxtt@label:#1>}%
\ifcase#1\relax
\setbox\z@\hbox{#2}%
\@tempdimb\fontdimen6 \textfont2\relax
\setlength\@tempdima{\wd\z@*\ratio{18\p@}{\@tempdimb}}%
\edef\bxtt@tmpa{\strip@pt\@tempdima}%
\hb@xt@\z@{\box\z@\hss}\mskip\bxtt@tmpa mu\relax
\or \textdef@\displaystyle\f@size{#2}%
\or \textdef@\textstyle\f@size{\firstchoice@false #2}%
\or \textdef@\textstyle\sf@size{\firstchoice@false #2}%
\or \textdef@\textstyle \ssf@size{\firstchoice@false #2}%
\fi}
%% \bxtt@gen@label
% Sets \bxtt@label.
\def\bxtt@gen@label{%
\global\advance\bxtt@id\@ne
\edef\bxtt@label{bxtt:\the\bxtt@id}}
%% \bxtt@inspect@ms
\def\bxtt@inspect@ms{\mathchoice
{\bxtt@inspect@ms@a{1}}%
{\bxtt@inspect@ms@a{2}}%
{\bxtt@inspect@ms@a{3}}%
{\bxtt@inspect@ms@a{4}}}
\def\bxtt@inspect@ms@a#1{%
\zref@setcurrent{bxtt@ms}{#1}%
\zref@labelbylist{\bxtt@label}{bxtt}}
%--------------------------------------- all done
\endinput
%% EOF
% XeLaTeX/LuaLaTeX document
\documentclass[a4paper]{article}
\usepackage{fontspec}
\newfontfamily\fIpxm{IPAexMincho}
\usepackage{bxamstext}% little noise
%\usepackage{amstext}% much noise!
\newcommand\☃{\text{{\fIpxm ☃}%
\typeout{Snowman's here!}}}% noise
\begin{document}
We have: $S = \Bigl\{\☃^{\☃^{\☃}}\Bigr\}$.
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment