Jesteś w: Start / Przedsięwzięcia/Projects / TeX Pearls / 2007 Pearls / grzegorz-murzynowski / bachotex2007-grzegorz-murzynowski-pearl1.tex

bachotex2007-grzegorz-murzynowski-pearl1.tex

TeX document icon bachotex2007-grzegorz-murzynowski-pearl1.tex — TeX document, 4 KB (4603 bytes)

Zawartość pliku

%&--translate-file=cp1250pl -*- coding: cp1250 -*-.\CodeDelim\%
%%% Hacking verbatim (LaTeX)
\documentclass[preprint, fleqn]{ltugproc}

\usepackage{graphicx}
\usepackage{ifpdf}
\ifpdf
  \usepackage[breaklinks,colorlinks,linkcolor=black,citecolor=black,
              pagecolor=black,urlcolor=black,
              hyperindex=false]{hyperref}      % for live url's.
\else
  \usepackage{url}
\fi

\usepackage{wasysym}
\usepackage{gmutils, gmverb}

\hypersetup{linkcolor=black,citecolor=black,
  pagecolor=black,urlcolor=black}

\makeatletter
\smartunder \dekclubs \edverbs

\author{Natror (Grzegorz Murzynowski)}
\title{Is there a~pearl?}
\address{Sulej\'owek\\Poland}
\netaddress{natror at o2.pl}

\begin{document}

\begin{abstract}
 \TeX\ maybe-pearls diven from the \pk{gmdoc} bundle: italics or
 \cs{show} inside a~verbatim and verbatim input of selected lines of
 a~file.

\end{abstract}

\maketitle

\show\cs
\expandafter\show\csname cs \endcsname
\section{\texttt{How do you get {\it italics} inside a~verbatim?}}

By `a~verbatim' I~mean a~\LaTeX's environment that changes the
catcodes of special chars and thus allows typesetting them verbatim.
\LaTeX's \cs{begin\{verbatim\}} expands mostly to
\[|\begingroup\csname verbatim\endcsname|\]
and \cs{verbatim} acts mostly like DEK's \cs{ttverbatim} and
\cs{end\{verbatim\}} is needed to delimit \cs{verbatim}'s argument.


Let's remind that the chars of of codes 1--32 (except the line end
etc.) are catcoded as `invalid' in \LaTeX. Therefore I~dare to assume
there are not used neither present in decent \LaTeXpar\ files.

The verbatim environments do not recatcode them.

So \emph{I} can use them for my wicked purpose: fig.~\ref{figEE}
and \ref{figFF}.

\begin{figure}
\catcode`\\active \def{\<char5>}
\catcode`\\active \def{\<char6>}

\begin{verbatim}
\catcode`\^^E\active
\def^^E{\bgroup\it}
\catcode^^F2 %
\begin{verbatim*}
How do you get italics inside a~verbatim?
\end{verbatim*}
\end{verbatim}

\caption{Italic source\dots}\label{figEE}

\catcode`\^^E\active
\def^^E{\bgroup\it}
\catcode`\^^F\active \let^^F\egroup %
\begin{verbatim*}
How do you get italics inside a~verbatim?
\end{verbatim*}

\caption{\dots\ and output.}\label{figFF}
\end{figure}

Note that we should use  explicit \<char5> and \<char6>
since verbatims recatcode |^| to `other' so |^^E| would produce
just |^^E|.

This trick I~used first for debugging: to |\show| something from
inside a~verbatim.

\section{How to input lines 2 and 24--27 of a~file verbatim?}

\begin{figure}
\catcode`\^^E\active \def^^E{}
\catcode`\\active \def{\vskip\stanzaskip}
\stanzaskip\medskipamount
\begin{verbatim}
\long\def\firstofone#1{#1}
\makeatletter
\newread\my@file
\openin\my@file=pearlsEBT.tex\relax

\def\my@reading#1 #2{%
  \loop\ifnum\count\z@<#1%
    \advance\count\z@\@ne\read\my@file to\@tempa
  \ifx.#2\@tempa\endgraf\fi\repeat}%

\firstofone{%
  \begin{verbatim}%
  \count\z@\z@
  \my@reading1 -%
  \my@reading2 .%
  \my@reading22 -%
  \my@reading26 .%
}\end{verbatim}
\end{verbatim}
\caption{A~verbatim input of selected lines of file\Dash the
  source\dots}\label{figVerInS}
\end{figure}

The code given if fig.~\ref{figVerInS} results in the output presented
in fig.~\ref{figVerInO}. What is the most fundamental trick? The
|\firstofone| macro (I~learnt it from my \TeX\ Guru who did'n invent
it either). Apparently it doesn't do anything: it has one parameter
and expands exactly to it. But there is one very important thing it
does: it `freezes' the catcodes in the argument. Therefore all the
commands and their arguments cannot be recatcoded by |\verbatim| and
they are expanded and executed.

% Another nice thing is that the file is read with the catcodes set by
% |\verbatim|
%  and therefore is typeset verbatim. The |\loop...\repeat|
% macro(s) don't need an explanation before this audience.



\begin{figure}
\catcode`\^^E9 %
\makeatletter
\newread\my@file
\openin\my@file=pearlsEBT.tex\relax
\def\my@reading#1 #2{%
  \loop\ifnum\count\z@<#1%
    \advance\count\z@\@ne\read\my@file to\@tempa
  \ifx.#2\@tempa\endgraf\fi\repeat}%

\firstofone{%
  \begin{verbatim}%
  \count\z@\z@
  \my@reading1 -%
  \my@reading2 .%
  \my@reading22 -%
  \my@reading26 .%
}\end{verbatim}

\caption{\dots\ and output.}\label{figVerInO}
\end{figure}
% Subsequent text



\end{document}

% The following is just my GNU Emacs stuff.
%
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End: