Skip to content

Instantly share code, notes, and snippets.

@khaledhosny
Created September 3, 2010 12:45
Show Gist options
  • Save khaledhosny/563837 to your computer and use it in GitHub Desktop.
Save khaledhosny/563837 to your computer and use it in GitHub Desktop.
%% the following is needed for the stolen code to work
\newdimen\scratchdimen
\newdimen\zeropoint\zeropoint=0pt
\newbox\scratchbox
\def\fontexheight{\fontdimen5 }
\def\mathsubnormal{\fontdimen16 }
\def\mathsupnormal{\fontdimen14 }
\def\strutheight{0pt}
\def\strutdepth{0pt}
\long\def\firstofoneargument#1{#1}
\long\def\gobbleoneargument#1{}
\long\def\doif#1#2%
{\edef\stringa{#1}%
\edef\stringb{#2}%
\ifx\stringa\stringb
\expandafter\firstofoneargument
\else
\expandafter\gobbleoneargument
\fi}
\let\dontleavehmode\quitvmode
\let\unexpanded\relax
\let\dosingleempty\relax
\let\tx\sevenrm
%% below, almost, unchanged ConTeXt code
%D \macros
%D {low, high, lohi}
%D
%D Although \TEX\ is pretty well aware of super- and
%D subscripts, its mechanism is mainly tuned for math mode.
%D The next few commands take care of script texts both modes.
%D
%D \startbuffer
%D The higher\high{one goes} the lower\low{one drops}, or\lohi{yes}{no}?
%D \stopbuffer
%D
%D \typebuffer
%D
%D \getbuffer
%D
%D Note the different placement of \type {\lohi}, where we
%D need a bit more space. The implementation looks a bit
%D fuzzy, since some \type {\fontdimen}'s are involved to
%D determine the optimal placement.
\def\dodohighlow
{\ifx\fontsize\empty
\ifmmode
\ifnum\fam<0 \tx \else \holamathfont \fi
\else
\tx
\fi
\else
\tx
\fi}
\def\dohighlow#1#2#3#4#5% todo, named fontdimens
{\dontleavehmode
\bgroup
\scratchdimen\ifdim\fontexheight\textfont2=1ex #2\textfont2\else #3ex\fi
\advance\scratchdimen #4ex
\kern.1ex
\setbox\scratchbox\hbox{#1\scratchdimen\hbox{\dodohighlow#5}}%
\ht\scratchbox\strutheight
\dp\scratchbox\strutdepth
\box\scratchbox
\egroup}
\unexpanded\def\high{\dohighlow\raise\mathsupnormal{.86}{0}}
\unexpanded\def\low {\dohighlow\lower\mathsubnormal{.48}{0}}
% \unexpanded\def\lohi#1#2%
% {\dontleavehmode
% \hbox
% {\setbox4=\hbox{\dohighlow\lower\mathsubnormal{.48}{.1}{#1}}%
% \setbox6=\hbox{\dohighlow\raise\mathsupnormal{.86}{.1}{#2}}%
% \ifdim\wd4<\wd6
% \wd4=\zeropoint\box4\box6
% \else
% \wd6=\zeropoint\box6\box4
% \fi}}
\unexpanded\def\lohi
{\dosingleempty\dolohi}
\def\dolohi[#1]#2#3%
{\dontleavehmode
\hbox
{\setbox4\hbox{\dohighlow\lower\mathsubnormal{.48}{.1}{#2}}%
\setbox6\hbox{\dohighlow\raise\mathsupnormal{.86}{.1}{#3}}%
\doif{#1}{left}
{\ifdim\wd4<\wd6
\setbox4\hbox to \wd6{\hss\box4}%
\else
\setbox6\hbox to \wd4{\hss\box6}%
\fi}%
\ifdim\wd4<\wd6
\wd4=\zeropoint\box4\box6
\else
\wd6=\zeropoint\box6\box4
\fi}}
%D You can provide an optional keyword \type {left}, in which
%D case the super and subscripts will be aligned in a way that
%D permits placement at the left of a word (which means that
%D it will be right aligned).
%D
%D \startbuffer
%D \lohi{aha}{ah} test \lohi{aha}{ah} test
%D \lohi[left]{aha}{ah} test \lohi[left]{aha}{ah} test
%D \lohi{aha}{ah} test\lohi{aha}{ah} test
%D \lohi[left]{aha}{ah}test \lohi[left]{aha}{ah}test
%D \stopbuffer
%D
%D \typebuffer
%D
%D \getbuffer
%% test
A\lohi[]{xy}{abxx}
\lohi[left]{xy}{abxx}A
A\low{xy} A\high{xy}
\bye
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment