% Makro pro "TeXbook naruby"
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Zpracovat form�tem csplain              Petr Ol��k

% Toto makro nen� p��li� reprezentativn� vyvedeno. Vznikalo toti� postupn�
% spole�n� s textem knihy. P�itom po�adavky na typografii a makro se p�i
% p��prav� textu postupn� m�nily. Nap��klad �pln� naposled byla do makra
% p�id�na mo�nost generov�n� hypertextov�ch odkaz�. Proto�e jsem p�i
% p�id�v�n� dal��ch vlastnost� postupn� "nalepoval" nov� ��sti k�du a star�
% u� necht�l p�episovat, nep�sob� celek vyrovnan�m dojmem. P�esto jsem se
% rozhodl makro ve�ejn� prezentovat, aby se zv�dav� �ten�� mohl pod�vat
% "jak to ten Ol��k ud�lal". Upozor�uji takov�ho �ten��e, �e se mu v k�du
% bude orientovat velmi �patn�.

% \let\pdfoutput=\undefined  %% Pokud nechceme v�stup do PDF ale do DVI

% D�len� slov:
\chyph
\hyphenation{plainu ASCII}

% Fonty:
\font\bigbf=csbx12 scaled\magstep1
\font\bbf=csbx12
\font\btt=cstt12
\font\sltt=cssltt10
\font\mflogo=logo10
\font\bbtext=bbold10
\font\bbex=cspex10

% Definice zrcadla:
\raggedbottom
\bigskipamount=12pt \medskipamount=6pt
\parindent=0pt  \parskip=\bigskipamount
\hsize=30pc \vsize=45pc \emergencystretch=2em \hbadness=2000
\exhyphenpenalty=10000
\lineskiplimit=-5pt \lineskip=0pt
\def\indent{\hskip\indentskip}
\abovedisplayshortskip=-\bigskipamount % Zahajuji $$ v�dy jako nov� odstavec
\abovedisplayskip=-\bigskipamount
\belowdisplayskip=0pt % a chci m�t nad a pod rovnic� pouze \parskip.
\belowdisplayshortskip=0pt 

%% Hypertextov� odkazy pro PDF
\ifx\pdfoutput\undefined  %%% Pro b�n� form�tov�n� pou��v�m Knuth�v TeX
\def\beglink#1{}
\def\endlink{}
\def\aimlink#1{}
\def\outlink#1#2#3{}
\def\pglink#1{#1}
\def\urllink#1{{\tt#1}}

\else                     %%% Pro generov�n� hyperlink� pou�iji Thanh�v TeX
\pdfoutput=1
\voffset=-2cm \hoffset=-1.5cm
\pdfcompresslevel=9
\pdfpagewidth=\hsize \advance\pdfpagewidth by 20mm
\pdfpageheight=\vsize \advance\pdfpageheight by 6\baselineskip
\pdfinfo{/Author (Petr Olsak)
         /CreationDate (/10/1996) 
         /ModDate (\the\day. \the\month. \the\year)
         /Creator (TeX)
         /Producer (pdfTeX)
         /Title (TeXbook naruby)
         /Subject (kniha)
         /Keywords (programovani, makrojazyk TeXu)
}
\def\pdfsetcmykcolor#1{\special{PDF:#1 k}}
%\def\CadetBlue{\pdfsetcmykcolor{0.62 0.57 0.23 0}}
\def\Blue{\pdfsetcmykcolor{0.9 0.9 0.1 0}}
\def\Black{\pdfsetcmykcolor{0 0 0 1}}
                          % Barva pro hyperlinky
\def\beglink#1{%          % Za��tek textu odkazu, #1 je kl�� odkazu
   \convertkey{#1}\Blue \pdfannotlink height8pt depth3pt 
     attr{/Border [0 0 0]} goto num\numlink \relax}
\def\endlink{\pdfendlink\Black}   % Konec textu odkazu
\def\aimlink#1{%          % M�sto c�le odkazu, #1 je kl�� odkazu
   \convertkey{#1}%
   \ifvmode \pdfdest num\numlink fith\relax 
   \else \vbox to0pt{\vss\hbox{\pdfdest num\numlink fith}\kern13pt}\fi}
\def\outlink#1#2#3{{%     % #1 je kl��, #2 je po�et potomk� a #3 je text
   \def\tt{}\def~{ }\def\char`{\string}\def\TeX##1{TeX}%
   \convertkey{#1}% kl�� c�le
   \pdfoutline goto num\numlink count -#2 % z�lo�ka je zav�en�
       {#3}}} % vlastn� text z�lo�ky
\def\pglink#1{%           % Vytiskne ��slo strany #1 ve form� odkazu
   \hbox{\beglink{pg/#1}#1\endlink}}
\def\urllink#1{\pdfannotlink height 10pt depth 3pt 
   user{/Border[0 0 0]/Subtype/Link/A << /Type/Action/S/URI/URI(#1)>>}\relax
   \Blue{\tt #1}\endlink}
\let\orishipout=\shipout
\def\shipout#1#2{\orishipout#1{\aimlink{pg/\the\pageno}#2}}
\def\convertkey#1{{%      % Konvertuje textov� kl�� na ��slo do \numlink
   \def\ { }\def~{ }%
   \expandafter\ifx \csname #1\endcsname \relax
      \global\advance\sumlink by1
      \expandafter\xdef \csname #1\endcsname{\the\sumlink}%
      \global\numlink=\sumlink
   \else \global\numlink=\csname #1\endcsname\relax
   \fi}}

%% N�hradn� �e�en� nekter�ch font�
%% O doby, kdy jsem p�evedl CSfonty do Type1, nen� toto �e�en� pot�eba
%\let\mflogo=\rm
%\font\cmtenrm=cmr10  \textfont0=\cmtenrm
%\font\cmsevenrm=cmr7 \scriptfont0=\cmsevenrm
%\font\bbtext=cmmib10
%\let\bbex=\tenex
\pdfcatalog{/PageMode /UseOutlines}\relax

\fi  %  Konec maker pro hyperlinky

% Deklarace:
\newskip\indentskip \indentskip=2em  % Budeme nulovat \parindent
\newwrite\reffile                    % Pro soubor s k���ov�mi odkazy
\newwrite\toc                        % Soubor pro sestaven� obsahu
\newwrite\mac                        % V�stupn� soubor s uk�zkami
\newread\testfile                    % Pracovn� prom�nn� pro soubor
\newtoks\buffer                      % Pracovn� prom�nn� typu \toks
\def\addbuf #1{\edef\act{\buffer={\the\buffer#1}}\act} % P�id�n� do \buffer
\def\nullbuf{\buffer={}}
\newtoks\pagenotoks                  % Pro pozdr�en� \the\pageno ve \write
\newtoks\nadpis                      % Pro pozdr�en� expanze nadpisu
\pagenotoks={\the\pageno}
\newtoks\noreftoks                   % Kter� slova se nemaj� ukl�dat  do .ref
\newif\ifnoref                       % Test, zda neulo�it do .ref
\newif\iflink                        % Zda tisknout sekvenci jako link
\newif\ifcarka  \carkatrue           % Zda se m� tisknout ��rka v \nextcs
\newcount\numlink \newcount\sumlink  % ��slo PDF odkazu
\newcount\linenum                    % ��slo ��dky v uk�zce
\newcount\chapnum                    % ��slo kapitoly
\newcount\secnum                     % ��slo sekce
\newcount\tempnum                    % Pracovn� prom�nn�
\newcount\bibnum                     % Bibliografick� reference
\newcount\expnum \newcount\basenum   % Pro kalkulace ��sel str�nek
\newif\ifdashbase \newif\ifdashexp
\newif\iffirst \newif\iffirstbase    % Pro kalkulaci ��sel str�nek
\newif\ifindexy  \indexytrue         % Maji se sazet indexy?

% Pom�cky:
% \krab[t]2. vysad� obd�ln��ek obsahuj�c� t s indexem 2
\def\smallstrut{\vrule height8.5pt depth2.5pt width0pt }
\def\krab[#1]#2.{\leavevmode\kern1pt\lower2.5pt\vbox{\hrule\kern-.4pt\hbox{%
  \vrule\tt\smallstrut
  \kern2pt#1\kern2pt\vrule}\kern-.4pt\hrule}$_{#2}$%
  \nobreak\hskip1pt minus.8pt\relax}
% \kraball[abc]2. je tot� jako \krab[a]2.\krab[b]2.\krab[c]2.
\def\kraball[#1]#2.{\def\temp{#2}\let\next=\cyklkrab \next #1]}
\def\cyklkrab #1{\if #1]\let\next=\relax \else\krab[#1]\temp.\fi\next}
% \pod[slovo] vysad� slovo podtr�eno
\def\pod[#1]{\leavevmode\lower2.5pt\vbox{\hbox{\tt\smallstrut
  \kern2pt#1\kern2pt}\kern-.4pt\hrule}}
% \endpar ud�l� �tvere�ek na konci my�lenky
\def\endpar{\unskip~\hfill\lower1pt\vbox{\hrule
  \hbox to 7pt{\vrule height7pt\hfil\vrule}\hrule}}
\def\.{\kern.1em } \def\,{\thinspace} \def\;{\kern.4em }
% \noi zabr�n� tisku referenc� s indexy.
\def\noi{\indexyfalse}
\def\+{\tabalign} % nechceme, aby to bylo \outer

% Uvozovky:
\def\uv{\bgroup\aftergroup\closequotes\leavevmode\clqq\let\next=}
\def\closequotes{\unskip\crqq\relax}
\def\clqq{\char254 }
\def\crqq{{\edef\iSF{\spacefactor\the\spacefactor}\char255 \iSF\relax}}

%% Prost�ed� s punt�kat�mi polo�kami %%
\def\begitems{\bigskip\bgroup\parskip=0pt \catcode`\*=13 }
\def\enditems{\par\egroup}
{\catcode`\*=13
 \gdef*{\par\leavevmode \hangindent=\indentskip \hbox to\indentskip
   {\hfil$\bullet$\kern8pt}\ignorespaces}}

% Loga:
\def\LaTeX{L\kern-.36em\raise.5ex\hbox{\sevenrm A}\kern-.12em\TeX}
\def\CS{$\cal C\kern-.1667em\lower.5ex\hbox{$\cal S$}\kern-.075em $}
\def\mf{{\mflogo META}\-{\mflogo FONT}}
\def\AMS{$\cal A\kern-.166em\lower.5ex\hbox{$\cal M$}\kern-.075em S$}
\def\itLaTeX{{\it L\kern-.24em\raise.5ex\hbox{\seveni A}\kern-.24em\TeX}}

% Kapitoly a sekce:
\def\sub #1 [#2]{\par\goodbreak\bigskip
   \advance\secnum by1
   \hbox{\aimlink{sc/#2}\outlink{sc/#2}0{#1}}\nobreak \vskip-\parskip
   \noindent{\bbf\let\tt=\btt \the\chapnum.\the\secnum. #1\par\nobreak
   \write\reffile{\string\secref{#2}\the\chapnum.\the\secnum}
   \nadpis={#1}
   \edef\act{\write\toc{\nono\subtocline
      {\the\chapnum.\the\secnum}{\the\nadpis}{\the\pagenotoks}{#2}}}\act
   \let\tt=\relax \let\\=\relax %\def\uv##1{\char254 ##1\char255 }
   \mark{\the\chapnum.\the\secnum. #1}}}
\def\kap#1 \par{\vfill\break \linenum=0 \secnum=0
   \global\advance\chapnum by1 \xdef\chapmark{\the\chapnum. #1}
   \headline={\hfil\starthead}
   \hbox{}\nadpis={#1}
   \aimlink{kp/\the\chapnum}
   \outlink{kp/\the\chapnum}{\expnumber{kap:\the\chapnum}}{#1}
   \edef\act{\write\toc{%
      \nono\kaptocline{\the\chapnum}{\the\nadpis}{\the\pagenotoks}}}\act
   {\lineskiplimit=-20pt {\bigbf \the\chapnum. #1}\par}}
\def\app#1 \par{\vfill\break 
   \global\advance\chapnum by1 \xdef\chapmark{\the\chapnum. #1}
   \headline={\hfil\starthead}
   \hbox{}\nadpis={#1}
   \aimlink{app/\the\chapnum}
   \outlink{app/\the\chapnum}{\expnumber{app:\the\chapnum}}{#1}
   \edef\act{\write\toc{%
      \nono\apptocline{\the\chapnum}{\the\nadpis}{\the\pagenotoks}}}\act
   {\lineskiplimit=-20pt {\bigbf \the\chapnum. #1}\par}}
\def\subpar{\leavevmode$\bullet$\bf\space}
\def\subnapis #1\par{\par\goodbreak\bigskip
   \noindent{\bbf\let\tt=\btt #1\par\nobreak
   \let\tt=\relax \let\\=\relax }}
\def\napis#1\par{\vfill\break
   \hbox{} {\lineskiplimit=-20pt {\bigbf #1}\par}}

% V�po�et stromu z�lo�ek pro PDF
\def\expnumber#1{\expandafter \ifx\csname#1\endcsname \relax 0%
    \else \csname#1\endcsname \fi}
\def\advancenumber#1{\tempnum=\expnumber{#1}\relax
    \advance\tempnum by1
    \expandafter\xdef\csname#1\endcsname{\the\tempnum}}
\def\extractkapnum#1.#2:{#1}

% Obsah
\def\nono{\noexpand\noexpand\noexpand}
\def\maketoc{\napis Obsah\par
   \aimlink{/obsah}\outlink{/obsah}0{OBSAH}
   \bigskip   \dimen0=9pt
   \softinput \jobname.toc
   \immediate\openout\toc=\jobname.toc
   }
\def\cvak{\ifdim\dimen0<6pt \dimen0=9pt \else \dimen0=3pt \fi}
\def\puntiky{\leaders\hbox to12pt{\kern\dimen0.\hss}\hfil}
\def\kaptocline #1#2#3{\medskip\line{#1.\enskip #2\hfil \pglink{#3}}\medskip}
\def\apptocline #1#2#3{\cvak \line{#1.\enskip #2\puntiky\ \pglink{#3}}}
\def\subtocline #1#2#3#4{
   \advancenumber{kap:\extractkapnum#1:}
   \line{\qquad #1\quad 
       \hbox{\beglink{sc/#4}#2\endlink}%
       \puntiky\hbox to2em{\hfil\pglink{#3}}}\cvak}
\def\partline #1#2#3{\cvak 
   \bigskip\line{\bf ��st #1: #2 \puntiky\ #3}\cvak}
\output{\def~{ } \let\TeX=\relax \let\\=\relax \let\tt=\relax \let\uv=\relax
   \plainoutput}

% Form�t ��sti B
\def\\#1 {\ifx\veskupine\undefined \else \endgroup \fi
    \par\mark{\normalbackslash #1}
    \begingroup \leftskip=\indentskip \noindent \kern-\indentskip
    \def\currcs{#1}\noref{#1}\def\veskupine{}%
    \saverefinfo{#1}%
    \aimlink{cs/#1}\outlink{cs/#1}0{\string\\#1}%
   {\tt \char`\\ #1 }}
\def\[#1] {\hfil [#1]\break}
\def\kn#1.{\par \emergencystretch=50pt {\bf Kn:~}#1,\hskip5pt plus10pt \ol}
\def\ol{{\bf Ol:~}\expandafter\printref\expandafter{\currcs}.%
   \par\endgroup}
\def\saverefinfo#1{\immed\write\reffile{\string\csref{#1}}}

\def\nextcs#1#2{\def\param{#2}\let\next=\nextcontrolsequence \next #1,,}
\def\nextcontrolsequence #1,{\if X#1X\let\next=\relax
   \else \handlecs{#1}\fi \next}
\def\handlecs#1{\unskip\ifcarka, \fi\hbox{\tt\char`\\ #1\param}%
   \saverefinfo{#1}\noref{#1}\carkatrue
   \aimlink{cs/#1}\outlink{cs/#1}0{\string\\#1}}

{\catcode`\<=\active
\gdef\Sp<#1> {\egroup \par \mark{{$\langle$\it#1$\rangle$}}
   \immed\write\reffile{\string\gqref{#1}}
   \bgroup \leftskip=\indentskip \noindent
   \kern-\indentskip 
   \aimlink{g/#1}\outlink{g/#1}0{\<#1>}{$\langle$\it#1$\rangle$}\enspace}
}

% �ten� v�ech c�l� hypertextov�ch odkaz� v ��sti B
\def\csref#1{\advancenumber{app:3}
   \expandafter\ifx\csname r:#1\endcsname \relax
   \expandafter\def\csname r:#1\endcsname{ }\fi}
\def\gqref#1{\advancenumber{app:1}
   \expandafter\def\csname g:#1\endcsname{ }}

% Plovouc� z�hlav� 
\advance\voffset by 2\baselineskip
\def\headrule{\leaders\hrule height3pt depth-2.6pt\hfil}
\def\starthead{\global\headline=
  {\ifodd\pageno \pravezahlavi \else \levezahlavi \fi}}
\nopagenumbers

% Implicitn� z�hlav� (m�n�no v ��sti B)
\def\pravezahlavi{\headrule\hskip3pt {\let\tt=\sltt\it \firstmark\/}}
\def\levezahlavi{{\it Kapitola \chapmark\/}\hskip3pt \headrule}

\def\startA {\vfill\break
  {\nopagenumbers \hbox{}\vskip5\baselineskip
  \write\toc{\noexpand\partline{A}{Algoritmy}{\the\pageno}}
  \aimlink{/castA}\outlink{/castA}0{== A ==}
  \centerline{\bigbf ��st A}\vskip2\baselineskip
  \centerline{\bigbf Algoritmy}\vfil\break}
  \footline={\ifodd\pageno \hfill \fi \bf\the\pageno \hfil}}

\def\startB {\vfill\break \linenum=0 \chapnum=0
  \ifodd\pageno \else \shipout\hbox{} \advancepageno \fi
  {\nopagenumbers \headline={\hfil} \hbox{}\vskip5\baselineskip
  \write\toc{\noexpand\partline{B}{Reference}{\the\pageno}\bigskip}
  \aimlink{/castB}\outlink{/castB}0{== B ==}
  \centerline{\bigbf ��st B}\vskip2\baselineskip
  \centerline{\bigbf Reference}\vfil\break}
  \def\pravezahlavi{\headrule\hskip3pt {\it \chapmark\/}}
  \def\levezahlavi{{\it \chapmark\/}\hskip3pt \headrule}}

% Z�znam do .ref z uk�zek (v uk�zce je catcode`\\=13 let\=\printsequence)
\def\printsequence{\leavevmode\buffer={}%
  \let\next=\printcycle \firsttrue \next} % Projdi n�sleduj�c� (catcode 11)
\def\printcycle#1{\ifcat \noexpand#1A\addbuf{#1}%
  \else \let\nextchar=#1\let\next=\savestring
      \iffirst % prvn� znak je nep�smeno
         \ifcat \noexpand#1@\addbuf{#1}\let\nextchar=\relax\fi \fi
  \fi \firstfalse \next}
\def\savestring {\edef\param{\the\buffer}\linkfalse% Zapi� string do .ref
  \ifx\param\empty \else
      \noreffalse \let\:=\dvojtecka \the \noreftoks
      \ifnoref \else
         {\def\noexpand{}\expandafter \ifx \csname r:\param\endcsname \relax
                                      \else \global\linktrue \fi}%
         \iflink
         \edef\iwrite {\write\reffile % do .ref: \refitem{string}strana.��dek
           {\string\refitem{\the\buffer}\the\pagenotoks.\the\linenum}}\iwrite
      \fi \fi
  \fi
  \iflink \hbox{\edef\act{\noexpand\beglink{cs/\param}}\act
      \char`\\\the\buffer\endlink}%
  \else \char`\\\the\buffer  % Vytiskni sekvenci
  \fi
  \nextchar}

\def\printtie{%  Vytiskne vlnku v uk�zce jako hypertextov� odkaz
  \edef\iwrite {\write\reffile %
     {\string\refitem{-vlnka}\the\pagenotoks.\the\linenum}}\iwrite
  \hbox{\beglink{cs/-vlnka}\normaltie\endlink}}

\def\refitem#1#2 {%  P�i �ten� z .ref p�id�vej do \r:string strana.��slo,
  \expandafter \ifx\csname r:#1\endcsname \relax 
     \expandafter \edef\csname r:#1\endcsname {#2,}%
  \else
     \expandafter \ifx\csname r:#1\endcsname \space
         \expandafter \edef\csname r:#1\endcsname {#2,}%
      \else
         \expandafter \edef\csname r:#1\endcsname{\csname r:#1\endcsname #2,}%
   \fi \fi}
% Pr�ce se seznamem \noreftoks
\def\noref #1{\let\:=\relax
   \edef\act{\noexpand\noreftoks={\the\noreftoks\:{#1}}}\act}
\def\dvojtecka#1{\def\tmp{#1}\ifx\tmp\param \noreftrue \fi}

% Z�znam do .ref prost�ednictv�m lnl[label] pro pou�it� v lnr[label]
\def\lnl[#1]{\edef\iwrite % do .ref: \lnref{label}��dek
  {\write\reffile{\string\lnref{#1}\the\linenum}}\iwrite\aimlink{ln/#1}}
\def\lnref#1#2 {\expandafter\ifx \csname ln:#1\endcsname \relax
      \expandafter\def\csname ln:#1\endcsname{#2}%
   \else \message{Warning: The lnl[#1] is used twice !!}%
   \fi}
\def\lnr[#1]{\expandafter\ifx \csname ln:#1\endcsname \relax
     \message{Warning: ref "#1" is not defined}??%
   \else \hbox{\beglink{ln/#1}\csname ln:#1\endcsname\endlink}%
   \fi}

% Z�znam do .ref prost�ednictv�m \sec N�zev [label] pro pou�it� v \rsec
\def\secref#1#2 {\expandafter\def\csname sc:#1\endcsname{#2}}
\def\rsec[#1]{\expandafter\ifx \csname sc:#1\endcsname \relax
     \message{Warning: sec-ref "#1" is not defined}??%
   \else \hbox{\beglink{sc/#1}\csname sc:#1\endcsname\endlink}%
   \fi}

% Z�znam do .ref prost�ednictv�m pgl[label] pro pou�it� v pgr[label]
\def\pgl[#1]{%              do .ref: \pgref{label}str�nka
  \write\reffile{\string\pgref{#1}\the\pageno}}
\def\pgref#1#2 {\expandafter\ifx \csname pg:#1\endcsname \relax
      \expandafter\def\csname pg:#1\endcsname{#2}%
   \else \message{Warning: The pgl[#1] is used twice !!}%
   \fi}
\def\pgr[#1]{\expandafter\ifx \csname pg:#1\endcsname \relax
     \message{Warning: ref "#1" is not defined}??%
   \else \expandafter\pglink\expandafter{\csname pg:#1\endcsname}%
   \fi}

% Z�znam do .ref prost�ednictv�m inl[label] pro pou�it� v inr[label]
\def\inl[#1]{%              do .ref: \inref{label}str�nka
  \write\reffile{\string\inref{#1}\the\pageno}}
\def\inref#1#2 {%
   \expandafter\ifx \csname in:#1\endcsname \relax
      \expandafter\def\csname in:#1\endcsname{\Pglink{#2}}%
   \else
      \expandafter\edef\csname in:#1\endcsname
         {\csname in:#1\endcsname, \Pglink{#2}}%
   \fi}
\def\inr[#1]{\expandafter\ifx \csname in:#1\endcsname \relax
     \message{Warning: inl[#1] is not defined}??%
   \else \csname in:#1\endcsname
   \fi}

% Bibliografick� reference
\def\bib[#1]{\par \advance\bibnum by1
  \noindent \hbox to\indentskip{[\the\bibnum]\hss}%
  \hangindent=\indentskip \hangafter=1 \aimlink{bib/#1}%
  \edef\act{\write\reffile{\noexpand\string\noexpand\bibcite{#1}\the\bibnum}}%
  \act \ignorespaces}
\def\bibcite#1#2 {\expandafter\def\csname bib:#1\endcsname{#2}}
\def\cite[#1]{\inl[#1]%
  \expandafter\ifx\csname bib:#1\endcsname \relax
      \message{Warning: The bib[#1] is not defined}[??]%
  \else \hbox{\beglink{bib/#1}[\csname bib:#1\endcsname]\endlink}\fi}

% Makro na zpracov�n� referenc� str�nek, 1,2,3 -> 1--3
% \printref{string} provede tisk referenc� pro {string}
\def\printref #1{%
  \expandafter \ifx\csname in:#1\endcsname \relax
  \else {\it \csname in:#1\endcsname},
     \def\Pglink##1{##1}%
     \edef\nopage{\csname in:#1\endcsname.0}%
     \let\Pglink=\pglink
  \fi
  \ifx \RefText \undefined
  \else {\it \RefText},
  \fi
  \expandafter \ifx\csname r:#1\endcsname \relax \else
    \expandafter \ifx\csname r:#1\endcsname \space
       nen� nikdy pou�ito%\message{The "#1" is never used}%
    \else
       \edef\act{\noexpand\setref \csname r:#1\endcsname 0.0,}\act
  \fi \fi }
\def\reftext #1:{\edef\RefText{#1}}

% \setref 1.1,1.2,1.0,1.3,1.5,2.0,3.0,3.0,4.0,5.0,5.1,5.1,0.0 vlo�� do \bufer:
% \,1(1,2,3,5,)\,2()\,3()\,4()\,5(1) tj. neopakuje �daje o ��dc�ch a str�nk�ch
\def\setref{\begingroup
   \let\,=\relax \nullbuf \tempnum=0 \firsttrue
   \let\next=\cycleref \next}
\def\cycleref #1.#2,{\ifnum #1=0 \let\next=\finalref
  \else \ifnum #1=\tempnum
               \ifnum #2=0 \else \ifindexy
                  \ifnum\expnum=#2 \else \addbuf{#2,}\expnum=#2 \fi
               \fi \fi
        \else  \def\temp{#1.#2}\ifindexy\else \def\temp{#1.0}\fi
          \ifx \temp\nopage \else % nechceme odkaz na stranu \nopage tisknout
               \iffirst \firstfalse \else \addbuf{)}\fi
               \addbuf{\,#1(}\tempnum=#1
               \ifnum #2=0 \else \ifindexy \addbuf{#2,}\expnum=#2 \fi \fi
        \fi \fi
  \fi \next}
% Z�v�re�n� tisk \the\buffer v math re�imu. Nejprve se ale expanduje \,
% (tj. \cref) v \buffer, co� zm�n� obsah \buffer
% \,1(1,2,3,5,)\,2()\,3()\,4()\,5(1) --> 1_{1-3,5},2-4,5_{1}
\def\finalref {\addbuf{)}%
   \let\,=\cref \firstbasetrue \dashbasefalse \basenum=-1
   \let\Pglink=\relax
   \expandafter\nullbuf \the\buffer
   \ifdashbase \addbuf{\the\basenum}\dashbasefalse \fi
   \mathcode`\-="007B \thinmuskip=5mu plus 3mu minus 2mu
   \let\Pglink=\pglink
   $\displaystyle\the\buffer$\endgroup}
% \cref 1(1,2,4,) zap��e zp�t do \buffer: 1_{1-2,4}
\def\cref #1(#2){%
   \if :#2:\tempnum=#1 \advance\tempnum by-1
           \ifnum \tempnum=\basenum % postoupili jsme o jednicku
                  \ifdashbase % nedelej nic, uz je zapsane -
                  \else \addbuf{-}\dashbasetrue
                  \fi 
           \else  \addnewbase{#1}% Je treba zapsat novou basi
           \fi
           \basenum=#1
   \else   \addnewbase{#1}% nova base bude mit exponent
           \addbuf{_\bgroup}\convertexp #20,\addbuf{\egroup}%
           \basenum=#1 \advance\basenum by-1 % aby nenasledovalo -
   \fi }
\def\addnewbase #1{%
   \ifdashbase \addbuf{\Pglink{\the\basenum}}\dashbasefalse \fi
   \iffirstbase \firstbasefalse \else \addbuf{,\penalty0 }\fi
   \addbuf{\Pglink{#1}}}
\def\convertexp {\let\next=\cycexp \dashexpfalse \firsttrue \expnum=-1 \next}
\def\cycexp #1,{%
   \ifnum #1=0 \let\next=\relax
               \ifdashexp \addbuf{\the\expnum}\dashexpfalse \fi
   \else \tempnum=#1 \advance\tempnum by-1
         \ifnum \tempnum=\expnum % postoupili jsme jen o jednicku
               \ifdashexp %nedelej nic, uz mame otevrene -
               \else  \addbuf{-}\dashexptrue
               \fi
         \else % Je treba zapsat nove cislo
               \ifdashexp \addbuf{\the\expnum}\dashexpfalse \fi
               \iffirst \firstfalse \else \addbuf{,\mkern3mu}\fi
               \addbuf{#1}%
        \fi
        \expnum=#1
   \fi \next}

% Na�ten� souboru, jen kdy� existuje
\def\softinput #1 {\immediate\openin\testfile=#1
  \ifeof\testfile\message{Warning: the file "#1" not found; run me twice.}%
  \else\immediate\closein\testfile\input #1 \fi}

% \beguk ... \enduk je skoro verbatim prost�ed�, zapisuje do .ref a .mac
\def\beguk{\par
  \bgroup\parskip=0pt\parindent=\indentskip\bigskip\setverb
  \everypar{\vadjust{\penalty10}\global\advance\linenum by 1
    \hbox to0pt{\hss\sevenrm\the\linenum\kern8pt}}
  \catcode`\"=12 \catcode`\|=12  % Ru�en� speci�ln�ch aktivn�ch znak�
  \catcode`\!=0     % Aby bylo mo�no ps�t ��dic� sekvence !takto
  \def\!{!}         % Aby bylo mo�no ps�t vyk�i�n�k jako !!
  \catcode`\~=13    % Aby se vlnka netiskla
  \def\~{\printtie} % Aby se cht�n� vlnka tiskla
  \catcode`\\=13    % Aby \cokoli ulo�ilo {cokoli} do .ref
  \obeylines \hfuzz=1.3pt \startuk}
{\catcode`\^^M=13 \catcode`\|=0 \catcode`\\=13  %
  |gdef|startuk#1^^M#2\enduk{%
      |let\=|normalbackslash |writetomac{#1}{#2}%
      |def\{|printsequence}|tt#2|egroup|par|penalty0 }
  |catcode`|\=12
  |gdef|normalbackslash{\}}
{\catcode`\~=12 \gdef\normaltie{~}}

\newlinechar=`\^^J
\def\openfiles{\let\write=\oriwrite \let\immed=\immediate
  \immediate\openout\reffile=\jobname.ref
  \immediate\openout\mac=\jobname.mac
  \immediate\write\mac{%
  *** Soubor v�ech ��slovan�ch uk�zek z kn��ky TeXbook naruby ***^^J%
  ***************************************************************^^J%
  *** (C) Petr Ol��k, \the\day. \the\month. \the\year
  \space\space K�dov�n� souboru: UNIX ISO8859-2^^J%
  ^^J%
  Soubor vznikl p�i zpracov�n� knihy "TeXbook naruby" automaticky.^^J%
  Jednotliv� uk�zky jsou uvozeny t�emi procenty^^J%
  n�sledovan�mi ��slem strany v knize [v z�vorce].^^J%
  \string\Pozor! Soubor nen� ur�en k prost�mu na�ten� do TeXu.^^J%
  Vyberte si jen to, co se v�m l�b�.^^J%
  Algoritmy z tohoto souboru je mo�n� pou��vat bez omezen�.^^J%
  Nen� ale dovoleno je elektronicky ���it �i publikovat bez uveden� p�vodu.^^J}
}
\let\oriwrite=\write \def\write #1#2{}
\let\immed=\relax

% Pomocn� makro pro z�pis do .mac
{\catcode`\%=12 \catcode`\|=14 \catcode`\^^M=13 \catcode`\<=13 |
\gdef\writetomac #1#2{{\def^^M{^^J}\def\ { }|
  \def\krab[##1]##2.{[##1]}\def\kraball[##1]##2.{[##1]}|
  \def\lnl[##1]{}\def\pgl[##1]{}\def\pod[##1]{!##1}|
  \def\tt{}\def\char`##1{\string##1}\def\indent{}\def\relax{}\def\it{}|
  \def\s##1.{}\def<{\normalangle}\def~{ }\def\inl[##1]{}|
  \let\~=\normaltie\def\endpar{}\def\dots{...}\let\kraball=\krab|
  \def\lnr[##1]{\noexpand\tisknumera{ln:##1}}|
  \def\pgr[##1]{\noexpand\tisknumera{pg:##1}}|
  \edef\act{\write\mac{%%% [\the\pagenotoks]#1^^J^^J#2}}\act}}}
\def\tisknumera#1{\expandafter\ifx\csname#1\endcsname\relax ??\else
  \csname#1\endcsname\fi}

% \begmulti ... \endmulti (pro v�cesloupcovou sazbu)
\splittopskip=\baselineskip
\def\begmulti #1 {\bigskip\penalty0 \def\Ncols{#1}
   \setbox0=\vbox\bgroup\penalty0 }
\def\endmulti{\vfil\egroup \setbox1=\vsplit0 to0pt
   \ifdim\pagegoal=\maxdimen
     \dimen1=\vsize \corrsize{\dimen1}
   \else \dimen1=\pagegoal \advance\dimen1 by-\pagetotal \fi
   \dimen0=\Ncols\baselineskip \advance\dimen0 by-\baselineskip
   \advance\dimen0 by \ht0 \divide\dimen0 by\Ncols
   \roundtolines{\dimen0}
   % \dimen0 je celkov� v��ka n sloupcov� sazby
   % \dimen1 je zbyl� m�sto na str�nce
   \ifdim \dimen0>\dimen1 \splitpart
   \else \makecolumns{\dimen0} \fi
   \ifvoid0 \else \errmessage{ztracen� informace v tabulce} \fi}
\def\makecolumns#1{\setbox1=\hbox{}\tempnum=0
   \loop \ifnum\Ncols>\tempnum
      \setbox1=\hbox{\unhbox1 \vsplit0 to#1 \hfil}
      \advance\tempnum by1
   \repeat
   \hbox{}\nobreak\vskip-\baselineskip \nointerlineskip
   \line{\unhbox1\unskip}}
\def\splitpart{\advance\dimen1 by 1pt
   \roundtolines{\dimen1} \makecolumns{\dimen1} \break
   \advance\dimen0 by-\dimen1
   \dimen1=\vsize \corrsize{\dimen1}
   \ifvoid0 \else
      \ifdim \dimen0>\dimen1 \splitpart
      \else \makecolumns{\dimen0} \fi \fi}
\def\roundtolines #1{\divide #1 by\baselineskip
   \multiply #1 by\baselineskip}
\def\corrsize #1{\advance #1 by \splittopskip \advance #1 by-\topskip}

% "..." je verbatim v ��dku, \begtt ... \endtt je display verbatim
% |\sekvence| vytiskne "\sekvence" a ulo�� {sekvence} do .ref
\def\"{"} \catcode`\"=13
\def"{\leavevmode\hbox\bgroup\let"=\egroup\setverb\tt}
\def\setverb{\def\do##1{\catcode`##1=12}\dospecials\obeyspaces}
\def\begtt{\bgroup\parskip=0pt\parindent=\indentskip\bigskip\setverb
  \catcode`\"=12 \catcode`\|=12 \catcode`\<=12 \obeylines \startverb}
{\catcode`\|=0 \catcode`\\=12
  |gdef|startverb#1\endtt{|tt#1|egroup}}
{\obeyspaces \gdef {\ }}
\def\oribar{|}
\catcode`\|=13
\def|{\bgroup\setverb \catcode`\ =12 \sequencetoref}
{\catcode`\"=0 \catcode`\\=12 % do .ref: \refitem{sekvence}strana.0
 "gdef"sequencetoref\#1|{"Storef{#1}"printlink\{#1}}}
\def\printlink#1#2{\leavevmode\hbox{\tt\beglink{cs/#2}#1#2\endlink}\egroup}
\def\Storef#1{\write\reffile{\string\refitem{#1}\the\pageno.0}}

% <text> vys�z� text v lomen�ch z�vork�ch (i v \beguk .. \enduk)
\def\<{<}  % Aby bylo mo�no tisknout symbol "je men��"
\let\normalangle=\<
\catcode`\<=13
\def<#1>{{\def\/{}\def\char`##1{}%
   \def\ { }\def~{ }\def\tt{++}\def\printsequence{}\def\lnr[##1]{}\def\.{}%
   \expandafter\ifx\csname g:#1\endcsname \relax
      \global\linkfalse \else \global\linktrue\fi}%
   \leavevmode\hbox{\iflink\beglink{g/#1}\fi$\langle$\it#1\/$\rangle$%
                    \iflink\endlink\fi}}

\def\refinput{
  \let\Pglink=\relax
  \catcode`\%=12 \catcode`\&=12 \catcode`\#=12 \catcode`\$=12
  \catcode`\_=12 \catcode`\^=12 \catcode`\~=12 \catcode`\"=12
  \softinput \jobname.ref
  \catcode`\%=14 \catcode`\&=4  \catcode`\#=6  \catcode`\$=3
  \catcode`\_=8  \catcode`\^=7  \catcode`\~=13 \catcode`\"=13
  \let\Pglink=\pglink
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\refinput
\openfiles

\input title

\input uvod

\maketoc

\startA

\input vstup   % Vstupn� ��sti TeXu
\input expand  % Expand procesor
\input hlavni  % Z�klady hlavn�ho procesoru
\input tabulky % Tvorba tabulek
\input matika  % Matematick� sazba
\input zalom   % Zalamov�n�
\input ruzne   % R�zn�

\startB

\input s
\input zkratky

\input b
\input priklady
\input lit
\input rejstrik

\input tiraz

\tracingstats=1

\end

