%
% Move contents page to the front
%
% Code from knuth.drv in https://github.com/oberdiek/latex-tds
% Works with pdftex and xetex.
%
\ifx\detokenize\undefined\endinput\fi
\newread\testread
\openin\testread=\contentsfile\relax
\ifeof\testread % First run
\else % Second run
  \let\ORGcon\con
  \def\con{%
    \ifnum\pageno=1 \global\titletrue\fi
    \begingroup
      \let\end\relax
      \ORGcon
    \endgroup
    \let\con\end
  }%
  \expandafter\let\csname ORGN\expandafter\endcsname
                  \csname N\endcsname
  \expandafter\outer\expandafter\def\csname N\endcsname{%
    \con
    \advance\pageno by -1\relax
    \expandafter\let\csname N\expandafter\endcsname
                    \csname ORGN\endcsname
    \csname N\endcsname
  }%
  \begingroup
    \def\num{0}%
    \edef\x{\jobname}%
    \edef\y{\detokenize{tex}}%
    \ifx\x\y
      \def\num{1}%
    \else
      \edef\y{\detokenize{mf}}%
      \ifx\x\y
        \def\num{1}%
      \fi
    \fi
  \expandafter\endgroup\ifnum\num=1 %
    \def\contentspagenumber{2}%
    \let\ORGpageno\pageno
    \def\pageno{%
      \let\pageno\ORGpageno
      \expandafter\let\csname ORGN\expandafter\endcsname
                      \csname N\endcsname
      \expandafter\outer\expandafter\def\csname N\endcsname{%
        \con
        \advance\pageno by -1\relax
        \expandafter\let\csname N\expandafter\endcsname
                        \csname ORGN\endcsname
        \csname N\endcsname
      }%
      \pageno
    }%
  \fi
\fi
