Skip to content

Instantly share code, notes, and snippets.

@dohyunkim
Created June 14, 2016 11:45
Show Gist options
  • Select an option

  • Save dohyunkim/57390c95052fdecbea8b1d32e0be6429 to your computer and use it in GitHub Desktop.

Select an option

Save dohyunkim/57390c95052fdecbea8b1d32e0be6429 to your computer and use it in GitHub Desktop.
tex macro, gsub
\documentclass{article}
\makeatletter
\def\gsub#1#2#3{%
\in@{#2}{#3}\ifin@
\errhelp{`#3' contains `#2', which is not allowed.}%
\errmessage{Nested Substitution}%
\fi
\in@{#2}{#1}\ifin@
\def\gsubresult##1#2##2\par{##1#3##2}%
\def\next{\expandafter\gsub\expandafter{\gsubresult#1\par}{#2}{#3}}%
\else
\def\next{\def\gsubresult{#1}}%
\fi
\next
}
\protected\def\segment#1{%
\gsub{#1}{.}{dot}%
\expandafter\gsub\expandafter{\gsubresult}{?}{QM}%
% \expandafter\gsub\expandafter{\gsubresult}{O}{Oh!}%
\expandafter\gsub\expandafter{\gsubresult}{!}{EXCL}%
\gsubresult
}
\makeatother
\usepackage{trace}
\begin{document}
|\segment{OO \TeX20.9 ?}|
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment