% $Id: tex4ht-auto-script.tex 182 2016-07-03 23:46:51Z karl $
% This file is not used, the scripts are generated by tex4ht-mkht.tex.
% Copyright (C) Kai-Mikael J\"a\"a-Aro
% Released under the LPPL, any version.
% See tex4ht-cpright.tex for license text.

\documentclass{article}

\immediate\openin15=ProTex.sty 
\ifeof15  
   \immediate\write16{The compilation of this file requires 
       the packages ProTex.sty and AlProTex.sty available at 
       http://www.cis.ohio-state.edu/\string~gurari/tpf/} 
    \def\next{\end{document}}\expandafter\next 
\fi                                                       
\immediate\closein15                                        
                                                                   
\input ProTex.sty                                               
\AlProTex{txt,<<<>>>,list,title,`} 
                                  

\begin{document}


%%%%%%%%%%%%%%%%%%
\section{Bash Scripts for TeX4ht}
%%%%%%%%%%%%%%%%%%


\begin{itemize}
\item
Author: Kai-Mikael J\"a\"a-Aro \verb+<kai@popwire.com>+

\item Automatically determine the number of times latex needs to be
called.

\item Remove the extension \verb+.txt+ from the names of the script
files created during the compilation.

\end{itemize}




\AtEndDocument{\OutputCode\<xhlatex\>}

\<xhlatex\><<<
tex4htmk "$1" "$2" "$3" "$4" "$5" ",xhtml" "$6"
>>>



\AtEndDocument{\OutputCode\<xhmlatex\>}

\<xhmlatex\><<<
tex4htmk "$1" "$2" "$3 -cunihtf" "$4" "$5" ",xhtml,mathml" "$6"
>>>



\AtEndDocument{\OutputCode\<uxhlatex\>}

\<uxhlatex\><<<
tex4htmk "$1" "$2" "$3 -cunihtf" "$4" "$5" ",xhtml,uni-html4" "$6"
>>>



\AtEndDocument{\OutputCode\<wlatex\>}

\<wlatex\><<<
tex4htmk "$1" "$2" "$3 -csymhtf" "$4" "$5" ",xhtml,word" "$6"
>>>



\AtEndDocument{\OutputCode\<xhlatex\>}

\<xhlatex\><<<
tex4htmk "$1" "$2" "$3" "$4" "$5" ",xhtml" "$6"
>>>



\AtEndDocument{\OutputCode\<xhmlatex\>}

\<xhmlatex\><<<
tex4htmk "$1" "$2" "$3 -cunihtf" "$4" "$5" ",xhtml,mathml" "$6"
>>>


\AtEndDocument{\OutputCode\<uxhlatex\>}

\<uxhlatex\><<<
tex4htmk "$1" "$2" "$3 -cunihtf" "$4" "$5" ",xhtml,uni-html4" "$6"
>>>



\AtEndDocument{\OutputCode\<wlatex\>}

\<wlatex\><<<
tex4htmk "$1" "$2" "$3 -csymhtf" "$4" "$5" ",xhtml,word" "$6"
>>>



\AtEndDocument{\OutputCode\<tex4htmk\>}

\<tex4htmk\><<<
#!/bin/bash
# This script is a make file for tex4ht.  It is written in bash
# but should be simple to translate into csh, perl or whatever your platform 
# prefers.
# Original author: Kai-Mikael Jää-Aro <kai@popwire.com>
# 
# This code is placed under the LaTeX Project Public License 
# (http://www.latex-project.org/lppl.txt).
#
# Motivation:
# The LaTeX file has to be compiled until the dvi file stabilises.  
# We check on auxiliary files to determine if a new compilation will change 
# anything or if we are done now.
#
# This script takes seven arguments:
# $1 The LaTeX file
# $2 Files to be included by latex
# $3 Options to tex4ht, of which the first must be a font directory
# $4 Options to t4ht
# $5 Options to latex/tex
# $6 Package inclusions to create the desired type of document 
# $7 A user-supplied script which will be executed each time around the loop
# $8 A user-supplied test which should have an exit value of 0 if we need to 
#    compile the file again. 
# 
# Sometimes, typically due to moving page references in the text, a file will 
# never converge, so there is a checkpoint every TEX4HTLIMIT iterations 
# (default 10).  If TEX4HTBATCH is set the script will just exit the loop, 
# otherwise it asks the user whether to continue.  If the output is 
# deemed not to have converged, the script will return a status of 1, 
# otherwise 0.

file=`basename $1 .tex`
includes=$2
tex4htoptions=$3
t4htoptions=$4
latexoptions=$5
doctype=$6
usercommand=$7
usertest=$8

if [[ $TEX4HTLIMIT ]] ; then : ; else TEX4HTLIMIT=10; fi

exitcode=0

counter=1
while [[ true ]]
do
  # There are (rare) instances when the process actually does not converge, 
  # so we give the users a heads up every few times around the loop, so they
  # can decide if they hadn't better check what's going on.  
  # (I prefer this to a fixed cut-out as tex4ht does not have a known upper 
  #  limit on the number of iterations necessary for unknown input.)
  if [[ $(($counter % $TEX4HTLIMIT)) == 0 ]]
  then
    if [[ $TEX4HTBATCH ]]
    then
      exitcode=1
      break
    fi
    read -p "LaTeX has now run $counter times without the output stabilising."$'\n'"Do you still wish to continue (y/n)? " reply
    if [[ $reply==n || $reply==no ]]
    then
      exitcode=1
      break
    fi  
  fi
  let counter=$counter+1

  tex4htmklatex "$file" "$includes" "$latexoptions" "$doctype" 

  # Do a BibTeX run if necessary. 
  grep -q bibdata $file.aux && bibtex $file

  # Creating an index may also require several iterations.  
  if [[ -f $file.idx ]]
  then
    tex $latexoptions "\def\filename{{$file}{idx}{4dx}{ind}} \input idxmake.4ht" 
    makeindex -o $file.ind $file.4dx
  fi
  # Both BibTeX and makeindex are fairly quick operations, so I simply redo 
  # them every time around the loop, even if it may not be strictly necessary.
  # If this is a problem, a more sophisticated check of the contents of the 
  # idx file can be substituted.

  # Perform any user commands
  eval $usercommand

  # Save copies of the files that transmit information about the need to rerun LaTeX
  cp ${file}.4tc ${file}-old.4tc
  cp ${file}.xref ${file}-old.xref
  cp ${file}.aux ${file}-old.aux

  # This is the logic here:
  # If LaTeX tells us we need to rerun, we do so without further discussion,
  # otherwise we check if any of the .4tc, .xref, .aux files is different from 
  # the previous version.  
  # Finally we use the user-supplied test to see if there is anything more to do.
  # If none of these tests trigger, we're done.
  grep -q 'Rerun to get cross-references right' $file.log && continue
  diff ${file}.4tc ${file}-old.4tc > /dev/null || continue
  diff ${file}.xref ${file}-old.xref > /dev/null || continue
  diff ${file}.aux ${file}-old.aux > /dev/null || continue
  if [[ $usertest ]] ; then `$usertest` && continue; fi
  break
done

rm ${file}-old.* # Always clean up after yourself...

tex4ht -f/$file  -i~/tex4ht.dir/texmf/tex4ht/ht-fonts/$tex4htoptions
t4ht -f/$file $t4htoptions

exit $exitcode
>>>





\AtEndDocument{\OutputCode\<tex4htmklatex\>}

\<tex4htmklatex\><<<
latex $2 '\makeatletter\def\HCode{\futurelet\HCode\HChar}\def\HChar{\ifx"\HCode\def\HCode"##1"{\Link##1}\expandafter\HCode\else\expandafter\Link\fi}\def\Link#1.a.b.c.{\g@addto@macro\@documentclasshook{\RequirePackage[#1'$3']{tex4ht}}\let\HCode\documentstyle\def\documentstyle{\let\documentstyle\HCode\expandafter\def\csname tex4ht\endcsname{#1'$3'}\def\HCode####1{\documentstyle[tex4ht,}\@ifnextchar[{\HCode}{\documentstyle[tex4ht]}}}\makeatother\HCode '$4'.a.b.c.\input ' $1
>>>



A test file (watch the changes in the .xref file):

\begin{verbatim}
\documentclass{article}
\begin{document}
   \begin{tabular}{llllllllll}
   \multicolumn{9}{c}{xx}&10\\
   1&2&3&4&4&6&7&8&9&10
   \end{tabular}
\end{document}
\end{verbatim}



\end{document}
