\fontfam[lm]

\newcount\secnum 
\newwrite\fileref \newread\filetest

\def\ifnextchar#1#2#3{\let\tznak=#1\def\bodyA{#2}\def\bodyB{#3}%
    \futurelet\znak\ifnextcharA}
\def\ifnextcharA{\ifx\znak\tznak\let\next=\bodyA\else\let\next=\bodyB\fi\next}
\def\softinput #1 {\let\next=\relax \openin\filetest=#1
  \ifeof\filetest
    \message{VAROVÁNÍ: soubor #1 nenalezen, TeXujte ještě jednou.}%
  \else \closein\filetest \def\next{\input #1 }\fi
  \next}  

\def\ref[#1]{\expandafter\ifx\csname l:#1\endcsname\relax
    ??\message{VAROVÁNÍ: nezámý label [#1]}%
    \else\csname l:#1\endcsname \fi}
\def\pgref[#1]{\expandafter\ifx\csname p:#1\endcsname\relax
    ??\message{VAROVÁNÍ: nezámý p-label [#1]}%
    \else\csname p:#1\endcsname \fi}

\def\lab[#1]{\gdef\ilabel{#1}}
\def\obr [#1] #2:{Obrázek #2:\saveref{#1}{#2}}
\def\saveref#1#2{\expanded{%
    \write\fileref {\string\Xref[#1]{#2}{\noexpand\the\pageno}}}}


\def\Xref[#1]#2#3{\expandafter\def\csname l:#1\endcsname{#2}
    \expandafter\def\csname p:#1\endcsname{#3}}

\softinput \jobname.rf
\immediate\openout\fileref=\jobname.rf

\def\sekce{\advance\secnum by1
   \indent \ifnextchar[{\labelsekce}{\tisksekce}}
\def\labelsekce[#1]{\saveref{#1}{\the\secnum}\tisksekce}
\def\tisksekce #1{%
  {\bf\llap{\the\secnum. }#1}\par\nobreak\medskip}

\hsize=6cm \vsize=5.5cm \raggedbottom
\pdfpagewidth=10cm \pdfpageheight=10cm

\def\bl{bla bla bla bla bla bla bla bal bla bla bla }
\def\bla{\bl\bl\bl\bl\bl\bl\bl\bl\bl}

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

\sekce{Úvod}
%%%%%%%%%%%%

\bl viz obrázek~\ref[obr chrousta] 
na straně~\pgref[obr chrousta], \bla

\bla viz též sekci~\ref[chrousti] na 
straně~\pgref[chrousti], \bl

\bla

\sekce[chrousti] {O~nesmrtelnosti chroustů}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\bl

\bla

\medskip
\centerline{\pdfximage width2cm {chroust.pdf}\pdfrefximage\pdflastximage }
\nobreak
\centerline{\obr[obr chrousta] AA: chroust}
\medskip

\bla

\end
