\documentclass{article}
\usepackage{lipsum}
\usepackage{tabularx}
\usepackage{switex}
\usepackage{amsmath}
\usepackage{filecontents}

\begin{filecontents}{test.pl}
user:file_search_path(library,'/Users/samer/lib/prolog/plcore').

:- use_module(library(dcgu)).
:- use_module(library(latex)).

author(Version) :- current_prolog_flag(version_data,Version).
heading(1,'Testing numerical expressions').
heading(2,'Testing LaTeX DCG table generation').

rows(Cell,Pred) -->
   { Pred=..[_|Args], findall(Args,Pred,Rows) },
   seqmap_with_sep(lbr, seqmap_with_sep(" & ",Cell),Rows).

table(Name/Arity) -->
   { functor(Pred,Name,Arity) },
   env(tabular, brace(rep(Arity,"l")), rows(wr,Pred)).

link('Brixton','Stockwell',120).
link('Stockwell','Clapham North',100).
link('Stockwell','Oval',111).
\end{filecontents}

\swi{consult(test)}

\author{\swi{author(N),write(N)}}
\title{SWITeX Test Document}
\date{\swi{get_time(T), format_time(user_output,'\pc D, \pc T',T)}}

\def\swis#1{\swi{X is (#1), write(X)}}

\begin{document}
	\maketitle
	\section{Introduction}
	The Prolog code for this test is included in the LaTeX source using 
	the \texttt{filecontents} package.

	SWI Prolog says \textit{---\swi{write(hello)}---}.
	The numerical time is \swi{get_time(T),write(T)}.
	Now a multiline Prolog call:
	\swi{
		write('Hello'),
		write(' '),
		write('world.')
	}

	\section{Verbatim Prolog code}

	Testing TeX group control, with braces in response correctly delimiting
	the extend of the boldface type:
	{\it italic \swiverb|write('{italic \\bf bold-italic}')| italic}.
	The next query is written as verbatim
	code and should result in two new paragraphs, one generated by an explicit
	\verb|\par|, the other by two new line characters: 
	\swiverb|format('Antelope. \n \\space Gibbon. \\par Fire bucket. \n\n Vestibule')|.
	Testing verbatim query with escaped characters in the response: 
	---\swiverb|write('\\%midwifery\\%')|---.

	Test verbitim query containing new lines:
	\swiverb!
		writeln('Line one.'),
		write('Line two.

		Line three.')!--- 
	The next two section headings come from Prolog, but note that the calls
	must be protected  with \verb|\protect|, as section headings are `movable' in LaTeX.

	\section{\protect\swi{heading(1,H),write(H)}}

	Some equations whos right-hand sides are evaluation by Prolog's \verb|is/2| predicate:
	\begin{align}
		\sqrt{2} &= \swis{sqrt(2)} \\ 
		\pi      &= \swis{pi} \\ 
		e        &= \swis{e}
	\end{align}

	\section{\protect\swi{heading(2,H),write(H)}}
	Now a table created entirely by Prolog:
	\begin{center}
	\swiphr{table(link/3)}
	\end{center}
	Another table with Prolog providing just the rows.
	\begin{center}
		\begin{tabular}{rrr}
			\hline
			\textbf{origin} & \textbf{destination} & \textbf{duration} \\
			\hline
			\swiphr{rows(wr,link(_,_,_))}
			\\ \hline
		\end{tabular}
	\end{center}
\end{document}
