Skip to content

Instantly share code, notes, and snippets.

@zr-tex8r
Last active July 21, 2016 13:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zr-tex8r/67c5b77f4729772d92da3dfb473e5e14 to your computer and use it in GitHub Desktop.
Save zr-tex8r/67c5b77f4729772d92da3dfb473e5e14 to your computer and use it in GitHub Desktop.
LaTeX: Much ado about the old font commands
% bxoldfontcmd.sty
%% package declaration
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{bxoldfontcmd}
\def\bxof@pkgname{bxoldfontcmd}
%% code guards
\@onlypreamble\bxof@restore@codes
\edef\bxof@restore@codes{%
\catcode33=\the\catcode33%
\catcode39=\the\catcode39%
\catcode45=\the\catcode45%
\catcode46=\the\catcode46%
\catcode47=\the\catcode47%
\catcode58=\the\catcode58%
\catcode59=\the\catcode59%
\catcode60=\the\catcode60%
\catcode62=\the\catcode62%
\catcode94=\the\catcode94%
\endlinechar=\the\endlinechar%
\relax}
\catcode33=12 % <!>
\catcode39=12 % <'>
\catcode45=12 % <->
\catcode46=12 % <.>
\catcode47=12 % </>
\catcode58=12 % <:>
\catcode59=12 % <;>
\catcode60=12 % <<>
\catcode62=12 % <>>
\catcode94=7 % <^>
\endlinechar=13 %
\AtEndOfPackage{\bxof@restore@codes}
\providecommand\bxDebug[1]{}
%--------------------------------------- helpers
%% packages
\RequirePackage{keyval}
%% unique tokens
\def\bxof@end{\bxof@end@}
%% switches
\newif\ifbxof@ok
%% \bxof@set@number\CS{<number>}
\def\bxof@set@number#1#2{%
#1=#2\relax}
%--------------------------------------- options
%% \ifbxof@aho
\newif\ifbxof@aho
%% \ifbxof@total
\newif\ifbxof@total
%% \ifbxof@stats
\newif\ifbxof@stats
%% \bxof@mode
\chardef\bxof@mode@banned=0
\chardef\bxof@mode@deprecated=1
\chardef\bxof@mode@allowed=2
\chardef\bxof@mode@praised=3
\let\bxof@mode\bxof@mode@deprecated
%% \bxof@freq
\newcount\bxof@freq \bxof@freq=1000000
%% 'noaho'/'aho'
\DeclareOption{noaho}{%
\bxof@ahofalse}
\DeclareOption{aho}{%
\bxof@ahotrue}
%% 'nostats'/'stats'
\DeclareOption{nostats}{%
\bxof@statstfalse}
\DeclareOption{stats}{%
\bxof@statstrue}
%% 'deprecated' etc.
\DeclareOption{banned}{%
\let\bxof@mode\bxof@mode@banned}
\DeclareOption{deprecated}{%
\let\bxof@mode\bxof@mode@deprecated}
\DeclareOption{praised}{%
\let\bxof@mode\bxof@mode@praised}
\DeclareOption{allowed}{%
\let\bxof@mode\bxof@mode@allowed}
%% keyval options
\DeclareOption*{%
\edef\bxof@tmpa{\noexpand\setkeys{bxof}{\CurrentOption}%
}\bxof@tmpa}
\define@key{bxof}{freq}{%
\bxof@set@number\bxof@freq{#1}}
%% process
\ProcessOptions\relax
%% postprocess
\ifnum\bxof@freq<\@ne
\PackageError\bxof@pkgname
{The 'freq' value must be positive\MessageBreak
(value=\the\bxof@freq)}
{That value is changed to 1.\MessageBreak\@ehc}
\bxof@freq=1
\fi
\ifnum\bxof@mode=\bxof@mode@banned
\bxof@freq=1
\fi
%---------------------------------------
%% variables
\let\bxof@name\relax
\let\bxof@cseq\relax
%\bxof@g@C/<name>
\def\bxof@g@total{0}
%% \bxof@g@name@list
\let\bxof@g@name@list\@empty
%% \bxof@get@name\CS
\def\bxof@get@name#1{%
\begingroup\escapechar\m@ne
\xdef\bxof@g@tmpa{\def\noexpand\bxof@name{\string#1}}%
\endgroup \bxof@g@tmpa
\edef\bxof@cseq{\@backslashchar\bxof@name}}
%% \bxof@cmd@declared\FCS
\@onlypreamble\bxof@cmd@declared
\def\bxof@cmd@declared#1{%
\begingroup
\bxof@get@name#1%
\bxDebug{DECLARED: \bxof@name}%
\expandafter\bxof@cmd@declared@a\csname bxof@g@C/\bxof@name\endcsname
\endgroup}
\@onlypreamble\bxof@cmd@declared@a
\def\bxof@cmd@declared@a#1{%
\ifx#1\relax
\let\do\relax
\xdef\bxof@g@name@list{\bxof@g@name@list\noexpand\do{\bxof@name}}%
\gdef#1{0}%
\fi}
%% \bxof@cmd@used\FCS
\def\bxof@cmd@used#1{%
\begingroup
\bxof@get@name#1%
\bxof@countup
\ifbxof@ok
\ifnum\bxof@mode=\bxof@mode@banned
\bxof@issue@error
\else\ifnum\bxof@mode=\bxof@mode@deprecated
\bxof@issue@warning
\else\ifnum\bxof@mode=\bxof@mode@praised
\bxof@issue@praise
\fi\fi\fi
\fi
\endgroup}
%% \bxof@countup
\def\bxof@countup{%
\expandafter\bxof@countup@a\csname bxof@g@C/\bxof@name\endcsname}
\def\bxof@countup@a#1{%
% check the phase
\bxof@okfalse
\@tempcnta=\ifbxof@total\bxof@g@total\else#1\fi\relax
\@tempcntb\@tempcnta \divide\@tempcntb\bxof@freq
\multiply\@tempcntb-\bxof@freq \advance\@tempcntb\@tempcnta
\ifnum\@tempcntb=\z@ \bxof@oktrue \fi
% command count
\@tempcnta=#1\relax \advance\@tempcnta\@ne
\xdef#1{\the\@tempcnta}%
% total count
\@tempcnta=\bxof@g@total\relax \advance\@tempcnta\@ne
\xdef\bxof@g@total{\the\@tempcnta}%
\bxDebug{\bxof@name=#1; total=\bxof@g@total}}
%% end-document hook
\AtEndDocument{%
\ifnum\bxof@mode=\bxof@mode@deprecated
\bxof@issue@warning@last
\fi
\ifbxof@stats
\bxof@show@stats
\fi}
%--------------------------------------- aho font
%% if that one really exists....
\IfFileExists{bxahofont.sty}{%
\RequirePackage{bxahofont}[]%
}{}
\ifx\textaho\@undefined
%% declare the aho font in private
\def\bxof@aho@fam{bxof-aho}\let\bxof@tmpa\bxof@aho@fam
\DeclareFontFamily{OT1}{\bxof@tmpa}{}
\DeclareFontShape{OT1}{\bxof@tmpa}{m}{n}{<->cmfi10}{}
\def\do#1#2{%
\DeclareFontShape{OT1}{\bxof@tmpa}{#1}{#2}{<->ssub*\bxof@tmpa/m/n}{}}
\do{m}{it}\do{m}{sl}\do{m}{sc}
\do{bx}{n}\do{bx}{it}\do{bx}{sl}\do{bx}{sc}
\DeclareRobustCommand\ahofont{%
\not@math@alphabet\ahofont\mathaho
\usefont{OT1}{\bxof@aho@fam}{m}{it}}
\DeclareTextFontCommand{\textaho}{\ahofont}
\DeclareMathAlphabet{\mathaho}{OT1}{\bxof@aho@fam}{m}{it}
%%
\fi
%--------------------------------------- messages
%% \bxof@issue@error
\def\bxof@issue@error{%
\PackageError\bxof@pkgname
{The old font comamnd '\bxof@cseq' is banned}
{Don't even think of writing the command!\MessageBreak
And throw away all the obsolete stuffs that depend on it!}}
%% \bxof@issue@warning
\def\bxof@issue@warning{%
\PackageWarning\bxof@pkgname
{Alas, the old font comamnd '\bxof@cseq' is used\MessageBreak
on input line \the\inputlineno.\MessageBreak
(The later occurrence of similar warnings may\MessageBreak
be suppressed.)\@gobbletwo}}
%% \bxof@issue@warning@last
\def\bxof@issue@warning@last{%
\ifnum\bxof@g@total>\z@
\let\bxof@tmpa\@empty
\def\do##1{%
\expandafter\ifnum\csname bxof@g@C/##1\endcsname>\z@
\edef\bxof@tmpa{\bxof@tmpa\space\@backslashchar##1}\fi}%
\bxof@g@name@list
\PackageWarningNoLine\bxof@pkgname
{Some of the old font comamnds were used:\MessageBreak
\space\bxof@tmpa\MessageBreak
All those commands are deprecated, because you\MessageBreak
have declared so. If that is not what you want,\MessageBreak
try \string\usepackage[allowed]{\bxof@pkgname}}
\fi}
%% \bxof@show@stats
\begingroup
\catcode10=12
\gdef\bxof@show@stats{%
\let\bxof@tmpa\@empty
\def\do##1{%
\expandafter\ifnum\csname bxof@g@C/##1\endcsname>\z@
\edef\bxof@tmpa{\bxof@tmpa\space\@backslashchar##1: %
\csname bxof@g@C/##1\endcsname^^J}%
\fi}%
\bxof@g@name@list
\typeout
{Here is how many times old font commands are invoked:^^J%
\bxof@tmpa\space Total: \bxof@g@total}%
}
\endgroup
%--------------------------------------- the big praise
%% \bxof@praise@message
\begingroup
\catcode`\^^A=14 \catcode`\#=12
\catcode`\^^\=0 \catcode`\^^[=1 \catcode`\^^]=2 \catcode`\^^X=6
\endlinechar=10 \catcode10=12 \catcode96=7 \let\do\@makeother
\dospecials``\gdef``\bxof@praise@message``X1``X2``[``A
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! ! ! ! ! ! ! ! ! ! E X C E L L E N T ! ! ! ! ! ! ! ! ! !
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
____
___HHHH _____ You are using the command ``X1
/ . . \ |NICE!| on input line ``X2.
\ --- / |~~~~~
V :#######: Y I'll greatly praise you for your use
\/ o \/ of such old font commands.
{ o }
\_______/ HURRAY!!
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%``]``A
``\endgroup%
%% \bxof@issue@praise
\begingroup
\catcode10=12
\gdef\bxof@issue@praise{%
\typeout{^^J%
\bxof@praise@message{\bxof@cseq}{\the\inputlineno}}}
\endgroup
%--------------------------------------- define the comamnds
%% \DeclareOldFontCommand redefined
\def\DeclareOldFontCommand#1#2#3{%
\bxof@cmd@declared#1%
\let#1\@undefined
\ifbxof@aho
\DeclareRobustCommand#1{%
\bxof@cmd@used#1%
\@fontswitch{\ahofont}{\mathaho}}
\else
\DeclareRobustCommand#1{%
\bxof@cmd@used#1%
\@fontswitch{#2}{#3}}
\fi}
%% redefine the commands
\DeclareOldFontCommand{\rm}{\normalfont\rmfamily}{\mathrm}
\DeclareOldFontCommand{\sf}{\normalfont\sffamily}{\mathsf}
\DeclareOldFontCommand{\tt}{\normalfont\ttfamily}{\mathtt}
\DeclareOldFontCommand{\bf}{\normalfont\bfseries}{\mathbf}
\DeclareOldFontCommand{\it}{\normalfont\itshape}{\mathit}
\DeclareOldFontCommand{\sl}{\normalfont\slshape}{\@nomath\sl}
\DeclareOldFontCommand{\sc}{\normalfont\scshape}{\@nomath\sc}
% pTeX commands
\ifx\mcfamily\@undefined\else
\DeclareOldFontCommand{\mc}{\normalfont\mcfamily}{\mathmc}
\fi
\ifx\gtfamily\@undefined\else
\DeclareOldFontCommand{\gt}{\normalfont\gtfamily}{\mathgt}
\fi
%--------------------------------------- all done
\endinput
%% EOF
\documentclass[a4paper]{article}
\usepackage{bxoldfontcmd}% '\bf' is deprecated
%\usepackage[freq=5]{bxoldfontcmd}% more warnings
%\usepackage[stats]{bxoldfontcmd}% show statistics
%\usepackage[banned]{bxoldfontcmd}% '\bf' is banned
%\usepackage[praised]{bxoldfontcmd}% '\bf' is praised! hurray!
\begin{document}
Once upon a time, there {\it were} some {\bf old} font commands.
\end{document}
@zr-tex8r
Copy link
Author

Usage

You can laod the package as follows:

\usepackage[<option>,...]{bxoldfontcmd}

The available options are:

  • banned/deprecated/allowed/praised: Specifies how the use of old font commands is treated.
  • stats: Shows the usage statistics of old commands at the end of the process.
  • nostats: Negation of stats.
  • freq=<number>: Frequency that specifies how often the (error/warning/praise) message is shown on the terminal.
  • aho: Tells that the old font comamnd should ignore its definition and always change to the 'CM Funny Italic' font.
  • noaho: Negation of aho.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment