5:- module(latex, 
    6   [  lbr//0
    7   ,  nm//1, (#)//1
    8   ,  pc//0
    9   ,  env//3
   10   ,  cmd//2, cmd//3
   11   ,  def//3
   12   ]).   13
   14:- meta_predicate 
   15      env(+,//,//)
   16   ,  def(+,//,//)
   17   ,  cmd(+,//,//)
   18   ,  cmd(+,//).   19
   20brace(C) --> "{", C, "}".
   21wr(A,H,T) :- with_output_to(codes(H,T),write(A)).
   22
   23env(Name,Opts,Body) -->
   24   nm(begin), brace(wr(Name)), Opts, 
   25   Body, nm(end), brace(wr(Name)).
   26
   27def(Name,Pattern,Body) --> nm(def), nm(Name), Pattern, brace(Body).
   28
   29cm --> "%".
   30pc --> "\\%".
   31amp --> "\\&".
   32lbr --> "\\\\".
   33nm(A) --> "\\", wr(A).
   34#(N) --> "#", wr(N).
   35
   36cmd(N,A1) --> nm(N), brace(A1).
   37cmd(N,A1,A2) --> nm(N), brace(A1), brace(A2).
   38
   39seqmap(_,[]) --> [].
   40seqmap(P,[A1|AS]) --> call(P,A1), seqmap(P,AS)