Skip to content

Instantly share code, notes, and snippets.

@z-rui
Created July 21, 2019 04:50
Show Gist options
  • Save z-rui/7bef781a35c3025952d47f88645dd95d to your computer and use it in GitHub Desktop.
Save z-rui/7bef781a35c3025952d47f88645dd95d to your computer and use it in GitHub Desktop.
TeX verbatim typesetting
% Verbatim typesetting
\catcode`@=11
\def\uncatcodespecials{\begingroup
\def\do##1{\catcode`\noexpand##1=12 }%
\edef\next{\endgroup\dospecials}\next}
\let\verbfont=\tentt
\def\nohyphenation{\hyphenchar\font\m@ne}
{\obeyspaces\gdef\verbspaces{\obeyspaces\let =\ }}
% in-line verbatim typesetting
\newtoks\everyverb
\def\verb{\bgroup \uncatcodespecials
\verbfont \nohyphenation \verbspaces \verb@}
\def\verb@#1{\catcode`#1=\tw@ \the\everyverb}
% multi-line verbatim typesetting
\let\verbatimfont=\tentt
\newtoks\everyverbatim
\newdimen\verbatimindent \verbatimindent=0pt
\newcount\interverbatimpenalty \interverbatimpenalty=100
\def\setupverbatim{\uncatcodespecials \verbatimfont \nohyphenation \verbspaces
\parindent\verbatimindent
\def\par{\leavevmode\endgraf % allow empty lines
\penalty\interverbatimpenalty}\obeylines}
\def\verbatim{\bgroup \setupverbatim \verbatim@}
{\lccode`\|=`\\ \catcode`\^^M=\active \lowercase{\gdef\verbatim@#1
{\long\def\next##1|endverbatim#1{\the\everyverbatim##1\egroup}\next}}}
\def\listing#1{\bgroup \setupverbatim
\expandafter\the\expandafter\everyverbatim \input#1 \egroup}
\catcode`@=12
% vim: ts=2:sw=2:et
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment