Skip to content

Instantly share code, notes, and snippets.

@zr-tex8r
Created January 31, 2016 06:38
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/73acfa8bd9c75bf2c169 to your computer and use it in GitHub Desktop.
Save zr-tex8r/73acfa8bd9c75bf2c169 to your computer and use it in GitHub Desktop.
LaTeX: To make all (generalized) fractions be prefixed with \Ustack, to enable \mathstyle to work right
% bxluaustack.sty
%% package declaration
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{bxluaustack}[2015/01/31 v0.2]
%% preparation
\def\bxluy@pkgname{bxluaustack}
\providecommand\bxDebug[1]{}
%--------------------------------------- environment check
%% engine check
\RequirePackage{ifluatex}
\ifluatex\else
\PackageError\bxluy@pkgname{%
This package requires LuaTeX}%
{Package loading is aborted.\MessageBreak\@ehc}
\expandafter\endinput\fi\relax
%--------------------------------------- settings
%%<+> \UstackifyDone
\let\UstackifyDone\@undefined
%% \bxluy@gfp@list
\def\bxluy@gfp@list{%
\do\over\@@over
\do\atop\@@atop
\do\above\@@above
\do\overwithdelims\@@overwithdelims
\do\atopwithdelims\@@atopwithdelims
\do\abovewithdelims\@@abovewithdelims
}
%% \bxluy@unknown@pkg@list
\def\bxluy@unknown@pkg@list{%
\do{cmathml}%
\do{tex4ht}%
\do{typehtml}%
}
%% \bxluy@incompat@pkg@list
\def\bxluy@incompat@pkg@list{%
\do{amstex}% obsolete
\do{codepage}% useless in LuaLaTeX
\do{easyeqn}%
\do{nath}% FIXME
}
%--------------------------------------- helpers
%% packages
\RequirePackage{etoolbox}[2011/01/03]
\RequirePackage{amsmath}
%% variables
\newbool{bxluy@ok}
%% unique tokens
\def\bxluy@end{\bxluy@end@}
%% \bxluy@initialize
% The begin-document hook.
\@onlypreamble\bxluy@initialize
\let\bxluy@initialize\@empty
\AtBeginDocument{\bxluy@initialize}
%% LuaTeX primitives
\directlua{
tex.enableprimitives('', {
'mathstyle', 'Ustack',
'crampeddisplaystyle', 'crampedtextstyle',
'crampedscriptstyle', 'crampedscriptscriptstyle'
})
}
%% \bxluy@Ustack
\let\bxluy@Ustack\Ustack
%--------------------------------------- compatibility stuffs
%% \bxluy@prohibit@gfp
\@onlypreamble\bxluy@prohibit@gfp
\begingroup\catcode`\#=12
\gdef\bxluy@prohibit@gfp{%
\bxDebug{\string\bxluy@prohibit@gfp}%
\expandafter\patchcmd\csname primfrac \endcsname
{\PackageWarning{amsmath}}%
{\bxluy@err@ivgfp{#1}\@gobble}%
{\bxluy@oktrue}{\bxluy@okfalse}%
\unless\ifbxluy@ok
\PackageError\bxluy@pkgname{%
Patch to 'amsmath' failed}\@ehc
\fi}
\endgroup
\def\bxluy@err@ivgfp#1{%
\PackageError\bxluy@pkgname{%
Foreign command '\@backslashchar#1' is prohibited}%
{You should use \noexpand\frac or \noexpand\genfrac
instead.\MessageBreak\@ehc}}
%% \bxluy@ustackify
\@onlypreamble\bxluy@ustackify
\def\bxluy@ustackify{%
\bxDebug{\string\bxluy@ustackify}%
\bxluy@ustackify@a
\ifbxluy@ok
\DeclareRobustCommand{\frac}[2]{%
\bxluy@Ustack{\begingroup##1\endgroup\@@over##2}}
\else
\PackageError\bxluy@pkgname{%
Patch to 'amsmath' failed}\@ehc
\fi}
\@onlypreamble\bxluy@ustackify@a
\begingroup\catcode`\#=12
\gdef\bxluy@ustackify@a{%
\patchcmd\@genfrac
{#1}%
{#1\bxluy@Ustack}%
{\bxluy@oktrue}{\bxluy@okfalse}}%
\endgroup
%%
\gappto\bxluy@initialize{%
\bxluy@oktrue
% check for incompatible packages
\def\do#1{%
\bxDebug{check for '#1'}%
\@ifpackageloaded{#1}{%
\PackageError\bxluy@pkgname{%
Incompatible package '#1' is loaded}\@ehc
\bxluy@okfalse
}{}}%
\bxluy@incompat@pkg@list
% check for 'unknown' packages
\ifbxluy@ok
\def\do#1{%
\bxDebug{check for '#1'}%
\@ifpackageloaded{#1}{%
\PackageWarning\bxluy@pkgname{%
Prohibition of Fraction primitives is suppressed\MessageBrfeak
for compatibility with package '#1';\MessageBrfeak
\noexpand\mathstyle might not work correctly\@gobble}%
\bxluy@okfalse
}{}}%
\ifbxluy@ok
\bxluy@prohibit@gfp
\else \bxluy@oktrue
\fi
\fi
\ifbxluy@ok
\bxluy@ustackify
\fi
\ifbxluy@ok
\let\UstackifyDone=t\relax
\fi}
%--------------------------------------- compatibility stuffs
%% variables
\newbool{bxluy@patchok}
%% \bxluy@restore@gfp
\def\bxluy@restore@gfp{%
\let\do\let \bxluy@gfprimes}
%% \bxluy@patched@pkg@list
\let\bxluy@patched@pkg@list\@empty
%% \bxluy@declare@patch
\@onlypreamble\bxluy@declare@patch
\def\bxluy@declare@patch#1{%
\gappto\bxluy@patched@pkg@list{\do{#1}}%
\expandafter\bxluy@declare@patch@a\csname bxluy@patch/#1\endcsname}
\@onlypreamble\bxluy@declare@patch@a
\def\bxluy@declare@patch@a#1{%
\@onlypreamble#1%
\begingroup \catcode`\#=12
\def\next{\xdef#1{\the\toks@}\endgroup}%
\afterassignment\next \toks@=}
%% 'schemata'
\bxluy@declare@patch{schemata}{%
\patchcmd\complexlbrace
{$\left}%
{\bxluy@restore@gfp$\left}%
{}{\bxluy@okfalse}%
\patchcmd\complexlbrace
{$\vbox}%
{\bxluy@restore@gfp$\vbox}%
{}{\bxluy@okfalse}}
%% 'oz'
\bxluy@declare@patch{oz}{%
\patchcmd\z@rename
{#1 \over #2}%
{\bxluy@Ustack{#1 \@@over #2}}%
{}{\bxluy@okfalse}}
%% 'permute'
\bxluy@declare@patch{permute}{%
\patchcmd\pmt@PrintVPmt@
{{\pmt@preimage\atop\pmt@image}}%
{\bxluy@Ustack{\pmt@preimage\@@atop\pmt@image}}%
{}{\bxluy@okfalse}}
%% 'mafr'
\bxluy@declare@patch{mafr}{%
\patchcmd\frc
{{#1\over#2}}%
{\bxluy@Ustack{#1\@@over#2}}%
{}{\bxluy@okfalse}}
%% 'slides-class'
\gappto\bxluy@initialize{%
\csletcs{ver@slides-class.sty}{ver@slides.cls}}
\bxluy@declare@patch{slides-class}{%
\patchcmd\frc
{{\begingroup#3\endgroup\over#4}}%
{\bxluy@Ustack{\begingroup#3\endgroup\@@over#4}}%
{}{\bxluy@okfalse}}
%%
\gappto\bxluy@initialize{%
\ifbxluy@ok
\bxluy@patchoktrue
\def\do#1{%
\bxDebug{check for '#1'}%
\@ifpackageloaded{#1}{%
\bxluy@oktrue
\csuse{bxluy@patch/#1}%
\ifbxluy@ok
\bxDebug{ok}%
\PackageInfo\bxluy@pkgname{%
Patch applied to package '#1'\@gobble}%
\else
\PackageWarning\bxluy@pkgname{%
Patch to package '#1' failed\@gobble}%
\bxluy@patchokfalse
\fi
}{}}%
\bxluy@patched@pkg@list
\unless\ifbxluy@patchok
\PackageError\bxluy@pkgname{%
Some necessary patches failed;\MessageBreak
\noexpand\mathstyle might not work correctly}\@ehc
\bxluy@okfalse
\fi
\fi
\ifbxluy@ok
\PackageInfo\bxluy@pkgname{%
Ustackifying process successfully finished\@gobble}%
\else\ifdefined\UstackifyDone
\PackageWarning\bxluy@pkgname{%
Ustackifying process finished but was incomplete;\MessageBreak
\noexpand\mathstyle might not work correctly\@gobble}%
\else
\PackageWarning\bxluy@pkgname{%
Ustackifying process failed\@gobble}%
\fi\fi}
%--------------------------------------- all done
\endinput
%% EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment