Skip to content

Instantly share code, notes, and snippets.

@zr-tex8r
Created August 19, 2012 13:57
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/3394992 to your computer and use it in GitHub Desktop.
Save zr-tex8r/3394992 to your computer and use it in GitHub Desktop.
To count the maximum length of \expandafter-chains
%
% bxcountxac.sty
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{bxcountxac}
%% variables
\newread\xx@file
\newcount\xx@count
\newcount\xx@maxcount
%
%% \xx@scan@line
\def\xx@scan@line{%
\expandafter\xx@scan@line@a\xx@line_%
}
\long\def\xx@scan@line@a#1{%
\ifx#1_\else
\ifx#1\expandafter
\advance\xx@count\@ne
\ifnum\xx@maxcount<\xx@count
\xx@maxcount\xx@count
\fi
\else
\xx@count\z@
\fi
\expandafter\xx@scan@line@a
\fi
}
%
%% \xx@set@verbatim
\def\xx@set@verbatim{%
\def\do##1{\catcode`##1=12\relax}%
\do\{\do\}\do\#\do\_%
}
%
%% \xx@inspect@maxcount
\def\xx@inspect@maxcount{%
\begingroup
\xx@set@verbatim
\endlinechar=13\relax
\xx@count\z@ \xx@maxcount\z@
\xx@inspect@maxcount@a
}
\def\xx@inspect@maxcount@a{%
\read\xx@file to\xx@line
\ifeof\xx@file
\expandafter\xx@inspect@maxcount@b
\else
\xx@scan@line
\expandafter\xx@inspect@maxcount@a
\fi
}
\def\xx@inspect@maxcount@b{%
\xdef\xx@gtempa{\xx@count\the\xx@maxcount\relax}%
\endgroup
\xx@gtempa
}
%
%%<*> \getxaccount{<filename>}
\newcommand\getxaccount[1]{%
\IfFileExists{#1}{%
\openin\xx@file=#1\relax
\xx@inspect@maxcount
\closein\xx@file
\edef\xaccount{\the\xx@count}%
}{}%
}
%
%%<*> \showxaccount{<filename>}
\newcommand\showxaccount[1]{%
\getxaccount{#1}%
\typeout{#1:\xaccount}%
}
%
%%-------- all done
\endinput
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment