\newif\ifsimboli
\newif\ifriferimenti

\newwrite\filesimboli
\def\simboli{
    \immediate\write16{ !!! Genera il file \jobname.SMB }
    \simbolitrue\immediate\openout\filesimboli=\jobname.smb}

\newwrite\fileausiliario
\def\riferimentifuturi{
    \immediate\write16{ !!! Genera il file \jobname.AUX }
    \riferimentitrue\openin1 \jobname.aux
    \ifeof1\relax\else\closein1\relax\input\jobname.aux\fi
    \immediate\openout\fileausiliario=\jobname.aux}

\newcount\sectnum\global\sectnum=0
\newcount\eqnum\global\eqnum=0
\newcount\citnum\global\citnum=0

\newif\ifndoppia
\def\numerazionedoppia{\ndoppiatrue\gdef\lasezionecorrente{\the\sectnum}}

\def\seindefinito#1{\expandafter\ifx\csname#1\endcsname\relax}
\def\spoglia#1>{}

\def\cref#1{\seindefinito{@c@#1}\immediate\write16{ !!! \string\cref{#1}
    non definita !!!}
    \expandafter\xdef\csname@c@#1\endcsname{??}\fi\csname@c@#1\endcsname}

\def\eqref#1{\seindefinito{@eq@#1}\immediate\write16{ !!! \string\eqref{#1}
    non definita !!!}
    \expandafter\xdef\csname@eq@#1\endcsname{??}\fi\csname@eq@#1\endcsname}

\def\sref#1{\seindefinito{@s@#1}\immediate\write16{ !!! \string\sref{#1}
    non definita !!!}
    \expandafter\xdef\csname@s@#1\endcsname{??}\fi\csname@s@#1\endcsname}

\def\section#1\par{\immediate\write16{#1}\goodbreak\vskip 48pt plus 60pt
    \noindent{\bf#1}\nobreak\vskip 15pt\nobreak\noindent}

\def\autosection#1#2\par{
    \global\advance\sectnum by 1\ifndoppia\global\eqnum=0\fi
    \xdef\lasezionecorrente{\the\sectnum}
    \def\usaegetta{1}\seindefinito{@s@#1}\def\usaegetta{2}\fi
    \expandafter\ifx\csname@s@#1\endcsname\lasezionecorrente\def\usaegetta{2}\fi
    \ifodd\usaegetta\immediate\write16
      { !!! possibili riferimenti errati a \string\sref{#1} }\fi
    \expandafter\xdef\csname@s@#1\endcsname{\lasezionecorrente}
    \immediate\write16{\lasezionecorrente. #2}
    \ifsimboli
      \immediate\write\filesimboli{ }\immediate\write\filesimboli{ }
      \immediate\write\filesimboli{  Sezione \lasezionecorrente :   sref.   #1}
      \immediate\write\filesimboli{ } \fi
    \ifriferimenti
      \immediate\write\fileausiliario{\string\expandafter\string\edef
      \string\csname@s@#1\string\endcsname{\lasezionecorrente}}\fi
    \goodbreak\vskip 48pt plus 60pt
    \noindent\hbox{\bf\the\sectnum.\quad{}}{\bf#2}\nobreak\vskip 15pt
    \nobreak\noindent}

\def\semiautosection#1#2\par{
    \gdef\lasezionecorrente{#1}\ifndoppia\global\eqnum=0\fi
    \ifsimboli
      \immediate\write\filesimboli{ }\immediate\write\filesimboli{ }
      \immediate\write\filesimboli{  Sezione ** : sref.
          \expandafter\spoglia\meaning\lasezionecorrente}
      \immediate\write\filesimboli{ }\fi
    \section#2\par}

\def\eqlabel#1{\global\advance\eqnum by 1
    \ifndoppia\xdef\ilnumero{\lasezionecorrente.\the\eqnum}
       \else\xdef\ilnumero{\the\eqnum}\fi
    \def\usaegetta{1}\seindefinito{@eq@#1}\def\usaegetta{2}\fi
    \expandafter\ifx\csname@eq@#1\endcsname\ilnumero\def\usaegetta{2}\fi
    \ifodd\usaegetta\immediate\write16
       { !!! possibili riferimenti errati a \string\eqref{#1} }\fi
    \expandafter\xdef\csname@eq@#1\endcsname{\ilnumero}
    \ifndoppia
       \def\usaegetta{\expandafter\spoglia\meaning\lasezionecorrente.\the\eqnum}
       \else\def\usaegetta{\the\eqnum}\fi
    \ifsimboli
       \immediate\write\filesimboli{  Equazione \usaegetta :  eqref.   #1}\fi
    \ifriferimenti
       \immediate\write\fileausiliario{\string\expandafter\string\edef
       \string\csname@eq@#1\string\endcsname{\usaegetta}}\fi}

\def\autoeqno#1{\eqlabel{#1}\eqno(\csname@eq@#1\endcsname)}
\def\autoleqno#1{\eqlabel{#1}\leqno(\csname@eq@#1\endcsname)}

\def\clabel#1{\global\advance\citnum by 1%
\xdef\lacitazione{\the\citnum}%
\def\usaegetta{1}\seindefinito{@c@#1}\def\usaegetta{2}\fi%
\expandafter\ifx\csname@c@#1\endcsname\lacitazione\def\usaegetta{2}\fi%
\ifodd\usaegetta\immediate\write16%
{ !!! possibili riferimenti errati a \string\cref{#1} }\fi%
\expandafter\xdef\csname@c@#1\endcsname{\lacitazione}%
\ifsimboli%
\immediate\write\filesimboli{  Citazione \lacitazione :   cref.   #1}\fi%
\ifriferimenti\immediate\write\fileausiliario{\string\expandafter\string\edef%
\string\csname@c@#1\string\endcsname{\lacitazione}}\fi}%

\def\cite#1{$^{[\cref{#1}]}$}
\def\firstcite#1{\clabel{#1}\cite{#1}}

