\chapter{The {\tt latex-hol} library}
  
The library {\tt latex-hol} provides HOL users a very convenient means of
formatting HOL text using the \LaTeX\ typesetting system. This library
uses the general pretty printer in the {\tt prettyp} library to
translate HOL text into \LaTeX\ format. After loading the library, a
set of ML functions are available to the user to
generate \LaTeX\ sources of HOL terms, theorems or whole theories. These
can then be incorporated into a document to produce high quality
typeset output.  

\section{Using the Library}

To use the {\tt latex-hol} library, first make sure the directory
containing the library is in the \HOL\ library search path. The ML
function {\tt library\_search\_path} displays this path and the
function {\tt set\_library\_search\_path} allow you to change this.
After setting up the path, the following command loads the
library into HOL:
\begin{verbatim}
  load_library`latex-hol`;;
\end{verbatim}
Now, you can call the function {\tt latex\_thm} to translate a HOL theorem
to \LaTeX. It takes the theorem as its argument. For
example, the session below shows the result of calling {\tt latex\_thm}
to translate the theorem {\tt INDUCTION} to \LaTeX\ format. 
\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#  latex_thm INDUCTION;;

\THM \FORALL P\DOT P \,\CONST{0} \AND 
     (\FORALL n\DOT P \,n \IMP  P \,(\CONST{SUC} \,n)) \IMP 
        (\FORALL n\DOT P \,n)
() : void
\end{verbatim}\end{session}
You can then yank the output and put it into a \LaTeX\ document
(assuming you run HOL in emacs). To format the document, you have to
input the macro file {\tt holmacs.tex} in your document before where
the first theorem appears. The theorem should be put in one of the
\LaTeX\ math modes, i.e., enclosing it in a pair of {\tt \$} signs or
between \verb|\[| and \verb|\]|. After running \LaTeX, this will be
typeset as 
\[\THM \FORALL P\DOT P \,\CONST{0} \AND 
               (\FORALL n\DOT P \,n \IMP  P \,(\CONST{SUC} \,n)) \IMP 
                      (\FORALL n\DOT P \,n)
\]
Notice that all the operators are set in their conventional
mathematical symbols, HOL constants are set in {\constfont sans serif}
font and space is added where it is needed automatically.  The
trailing digits of variables {\tt x1}, {\tt x2} etc. will 
become subscript as in the following theorem ({\tt RIGHT\_AND\_OVER\_OR})
\[
\THM \FORALL t_{1} \,t_{2} \,t_{3}\DOT
        (t_{2} \OR  t_{3}) \AND  t_{1} =
           t_{2} \AND  t_{1} \OR  t_{3} \AND  t_{1}
\]

However, it is not very convenient to copy the text generated by {\tt
latex\_thm} if there are many theorems you want to typeset. Another
function {\tt latex\_thm\_to} will output the text to a file whose
name is specified as the first argument. The command
\begin{verbatim}
  latex_thm_to `induct.tex` INDUCTION;;
\end{verbatim}
puts the \LaTeX\ source of the theorem {\tt INDUCTION} into the file
{\tt induct.tex}. If such a file exists, it is overwritten. Otherwise,
a new file is created. you can also append the text into an existing
file using the function {\tt latex\_thm\_add\_to}. This takes the same
argument as {\tt latex\_thm\_to}.

There are also functions to translate terms, types and theorems with
all assumptions. The names of them are obtained by replace {\tt thm}
in the functions introduced above to {\tt term} for terms, {\tt type} for types
and {\tt all\_thm} for theorems with assumptions. 

\section{Typeset a Complete Theory}

The {\tt latex-hol} library can also translate a complete \HOL\ theory
to be typeset in a way similar to the output of the function {\tt
print\_theory}. The function to generate \LaTeX\ source for a theory
is {\tt latex\_theory\_to}. It takes three arguments: the first is a
string specifying the file name for the output to go, the second is a
boolean value which specifies the format of the output and the last one is
the name of the theory. If the boolean value is {\tt false}, the
output is in standalone format. This means that the \LaTeX\ file
generated can be formatted on its own to form a standalone document.
If it is {\tt true}, the file can be included into a larger document.
For example, 
\begin{verbatim}
  latex_theory_to `bool.tex` false `bool`;;
\end{verbatim}
will generate a file named {\tt bool.tex} which contains the theory
{\tt bool}. You can then just say 
\begin{verbatim}
  latex bool
\end{verbatim}
at you system prompt to produce a typeset listing of the {\tt bool} theory.
If you want to include the theory in a larger document, you should use
\begin{verbatim}
  latex_theory_to `bool.tex` true `bool`;;
\end{verbatim}
Then, in the including \LaTeX\ document, you need to input the macro
file {\tt holmacs.tex}. The \LaTeX\ file looks like below
\begin{verbatim}
  \input{holmacs}
  ...
  \input{bool}
\end{verbatim}
The file generated by {\tt latex\_theory\_to} in included format looks like:
\begin{verbatim}
  \theory{xxx}
  \sec{yyy}
  ...
  \sec{yyy}
  ...
  \endthy{xxx}
\end{verbatim}
where {\tt xxx} is the name of the theory and {\tt yyy} is the parts
of the theory such as `Types' or `Definitions'. By default, the command
\verb|\theory| produces a numbered section heading, \verb|\sec|
produces an unnumbered section heading and \verb|\endthy| draws a rule
to close the theory. You can redefine these commands to suit your
document style using \verb|\renewcommand|. For example, 
\begin{verbatim}
  \renewcommand{\theory}[1]{\subsection{#1}}
  \renewcommand{\sec}[1]{\subsubsection{#1}}
  \renewcommand{\endthy}[1]{}
\end{verbatim}
 redefines \verb|\theory| to be subsection, \verb|\sec| to be
subsubsection and \verb|\endthy| not to draw the closing rule.

\section{The \LaTeX\ macro file}

The macro file {\tt holmacs.tex} defines a set of \LaTeX\ macros for
use with files generated by this translator. The macros can be divided
into two groups: special symbols and environments.

As you have seen above, the translator converts the \HOL\ special
symbols into \LaTeX\ commands, for example, \verb|/\| becomes
\verb|\AND| and \verb|==>| becomes \verb|\IMP|. The macro file defines
these commands to be the symbols $\AND$ and $\IMP$. All special
symbols are processed in this way. This extra level of translation
gives the user the flexibility of changing the symbols (and the danger
of abusing it). If you prefer a
particular symbol to be set in a different way, you can change the
definition of the corresponding command in your document. For example,
put the command below in your document after the \verb|\input{holmacs}|
line
\begin{verbatim}
  \renewcommand{\IMP}{\Longrightarrow}
\end{verbatim}
then the implication sign will be typeset as $\Longrightarrow$.

There are three new environments, namely {\tt ttlist}, {\tt
typelist} and {\tt thmlist}, defined in the macro file. They are used
to format theories. The 
`Parents' and `Types' sections are in {\tt ttlist}. Text in these
sections is set in a paragraph in {\tt typewriter} font (\verb|\tt|).
The `Constants' and `Infixes' sections are formatted in {\tt typelist}
environment. Each constant is an item in the list.
The constant names are used as item labels
 and the its type is set in {\tt typewriter} font. The `Definitions' and
`Theorems' sections are set in {\tt thmlist} environment. Each theorem
forms an item. The names of the theorems are used as the label and the
theorems are in in-line math mode. You can change the definition of
these environment to suit your style if necessary.

\section{Hints on better  formatting}

Since theorems in a theory file are in in-line math mode, long ones
may be split into several lines by \TeX. The breaks will only occur at
certain places, namely after binary operators and relations.
Sometimes, this will result in \TeX\ to produce overfull or underfull
boxes. This is the time you should give some help to \TeX. You can
tell \TeX\ where you would like a line
break to occur by inserting the command \verb|\allowbreak|  to that point.
If the problem is in the `Constants' or `Infixes' section, you can
also use the command \verb|\newline| since 
it is not in math mode.

For very long theorems, it is better to put it into an {\tt eqnarray}
environment. For example, the definition {\tt GROUP\_DEF} in the
library {\tt group} reads
\begin{eqnarray*}
\lefteqn{\THM \FORALL G \,prod\DOT \CONST{GROUP} \,(G,prod) =} \\
 & &     (\FORALL x \,y\DOT G \,x \AND  G \,y \IMP  G \,(prod \,x \,y))
           \AND \\
 & &     (\FORALL x \,y \,z\DOT G \,x \AND  G \,y \AND  G \,z \IMP 
           (prod \,(prod \,x \,y) \,z = prod \,x \,(prod \,y \,z)))
           \AND  \\
 & &     (\EXISTS e\DOT G \,e \AND  
 	   (\FORALL x\DOT G \,x \IMP  (prod \,e \,x = x)) \AND 
              (\FORALL x\DOT
                   G \,x \IMP 
                      (\EXISTS y\DOT G \,y \AND  (prod \,y \,x = e))))
\end{eqnarray*}
This is formatted by the commands below
\begin{verbatim}
\begin{eqnarray*}
\lefteqn{\THM \FORALL G \,prod\DOT \CONST{GROUP} \,(G,prod) =} \\
 & &  (\FORALL x \,y\DOT G \,x \AND  G \,y \IMP  G \,(prod \,x \,y))
           \AND \\
 & &  (\FORALL x \,y \,z\DOT G \,x \AND  G \,y \AND  G \,z \IMP 
           (prod \,(prod \,x \,y) \,z = prod \,x \,(prod \,y \,z)))
           \AND  \\
 & &  (\EXISTS e\DOT G \,e \AND  
 	 (\FORALL x\DOT G \,x \IMP  (prod \,e \,x = x)) \AND 
            (\FORALL x\DOT
                 G \,x \IMP 
                   (\EXISTS y\DOT G \,y \AND  (prod \,y \,x = e))))
\end{eqnarray*}
\end{verbatim}
The commands corresponding to the theorem were generated by {\tt
latex\_thm}. Then, they are put in between the \verb|\begin{eqnarray*}| and
\verb|\end{eqarray*}| commands. The command \verb|\lefteqn| is added to outdent
the first line. The \verb|\\| commands are placed at the end of
the lines where line breaks are required and the \verb|&|'s are needed
to force the lines to be indented.


