(* ::Package:: *)

(************************************************************************)
(* This file was generated automatically by the Mathematica front end.  *)
(* It contains Initialization cells from a Notebook file, which         *)
(* typically will have the same name as this file except ending in      *)
(* ".nb" instead of ".m".                                               *)
(*                                                                      *)
(* This file is intended to be loaded into the Mathematica kernel using *)
(* the package loading commands Get or Needs.  Doing so is equivalent   *)
(* to using the Evaluate Initialization Cells menu command in the front *)
(* end.                                                                 *)
(*                                                                      *)
(* DO NOT EDIT THIS FILE.  This entire file is regenerated              *)
(* automatically each time the parent Notebook file is saved in the     *)
(* Mathematica front end.  Any changes you make to this file will be    *)
(* overwritten.                                                         *)
(************************************************************************)



(* ::Input::Initialization:: *)
SetOptions[$FrontEndSession,PrintingStyleEnvironment->"Working"];
nb2tex[nb_String,opts___Rule]:=nb2tex[nb,nb,opts];


(* ::Input::Initialization:: *)
nb2tex[nb_String,tex_String, opts___Rule]:=Module[
{notebook,PDFCounter=0,lines,type,tag,pdfname,cell,c,cl,
PDFFolder=PDFFolder/. {opts}/.PDFFolder->nb
},
nb2tex$PDFWidth = PDFWidth /. {opts} /. PDFWidth->6.5;
notebook=NotebookGet[NotebookOpen@FileNameJoin[{Directory[],nb<>".nb"}]];
If[FileType[PDFFolder]===None, CreateDirectory[PDFFolder]];
DeleteFile/@FileNames["*.pdf",PDFFolder];
lines=Table[
type=cell[[2]];
tag=CellTags /. Cases[cell,_Rule] /. CellTags->"";
Which[
type=="Text"\[And]tag=="tex", StringReplace[cell[[1]],{"\[CloseCurlyQuote]"->"'", "\[CloseCurlyDoubleQuote]"->"\""}],
StringMatchQ[tag,"pdf"~~___], (
pdfname=PDFFolder<>"/"<>ToString[++PDFCounter]<>".pdf";
Export[pdfname, Join[cell, Cell[PageWidth -> 80nb2tex$PDFWidth/0.75]]];
cl="c:\\drorbn\\bin\\cpdf.exe -scale-page \"0.75 0.75\" "<>pdfname<>" -o "<>pdfname;
Close@OpenRead["!"<>cl];
StringReplace[
"\\noindent\\nbpdfXXXType{pdfname}",
{"XXX"->StringDrop[tag,3], "Type"->type, "pdfname"->pdfname}
]
),
type=="Text"\[And]tag=="exec", ToExpression[cell[[1]]]; "",
True, ""
],
{cell,Cases[notebook,c_Cell /; Length[c]>=2(*\[And]FreeQ[c, _Cell, {1,\[Infinity]}]*),\[Infinity]]}
];
lines=StringJoin@@Riffle[DeleteCases[lines, ""], "\n\n"];
WriteString[tex<>".tex",lines]; Close[tex<>".tex"]; ]
