(*showing example how to generate Latex from Mathematica. by Nasser M. Abbasi. July 31, 2017 version*)

SetDirectory[NotebookDirectory[]]

SetDirectory[NotebookDirectory[]];
ClearAll[x,s];
tex[s_] := ToString@TeXForm[s];

fileName ="index.tex";
file=OpenWrite[fileName,PageWidth->Infinity];

from=0; to=1;
int=HoldForm[Integrate[x^2,{x,0,1}]];

s="
\\documentclass[11pt]{article}
\\usepackage{amsmath} 
\\usepackage{graphicx}
\\usepackage{array}
\\usepackage[letterpaper,  margin=1in]{geometry}
\\usepackage{longtable}
\\usepackage{float}
\\usepackage{hyperref}
\\begin{document}

\\ifdefined\\HCode
\\begin{enumerate}
\\item \\href{index.pdf}{PDF file here}
\\item \\href{example_making_web_page.nb}{Mathematica notebook used to generate the Latex}
\\end{enumerate}
\\fi

\\title{Web page generated using Mathematica to Latex to tex4ht to HTML process}
\\date{\\today}
\\author{By Mathematica, Latex and tex4ht}
\\maketitle
\\tableofcontents
\\section{First example}
   The integral of $x^2$ from $"<>tex[from]<>"$ to $"<>tex[to]<>"$ is 
   \\[
      \\boxed{ " <>tex[int]<>" = "<> tex[ReleaseHold[int]]<>"}
   \\]
   From $0$ to $2 \\dots 4$ the integral evaluates to 
   \\begin{itemize}
";
 
  Do[
     With[{n=i},
          int=HoldForm@Integrate[x^2,{x,0,n}];
          s=s<>"\\item $"<>tex[int]<>" = "<> tex[ReleaseHold@int] <>"$\n"
     ]
    , {i,2,4}
  ];  
  s=s<>"\\end{itemize}
  The values of $\\sin(x)$ in an inteval of $0$ to $3$ in $0.1$ steps (rounded to $10^{-4}$ are 
  \\["<>
  tex[TableForm@Transpose@Table[{x,N@Round[Sin[x],10^-4]},{x,0,3,1/3}]]<>
  "\\]
  The Plot for $\\cos(x)$ is";
  p=Plot[Cos[x], {x, 0, 2 Pi},Frame->True,PlotStyle->Red,GridLines->Automatic,GridLinesStyle->LightGray];
  Export["myImage.pdf",p];
  s=s<>"
     \\begin{figure}[H]
     \\centering
        \\includegraphics[width=0.5\\textwidth]{myImage}
     \\caption{my plot of $\\cos(x)$}
     \\end{figure}
  ";
   
s=s<>"
     \\section{Second example}\n";
     functions = {Sin, Cos, Tan, Sqrt, RiemannSiegelZ}
     s=s<>"\\begin{center}
           %\\begin{table}
           \\begin{longtable}{|l|m{.7\\textwidth}|}\\hline \n";
     Do[
         s=s<>"$"<>tex[functions[[i]]]<>"$&";
         p=Plot[functions[[i]][x], {x, 0, 3},Frame->True,ImagePadding->20,GridLines->Automatic,GridLinesStyle->LightGray];
         imageName = "myImage"<>ToString[i]<>".pdf";
         Export[imageName,p];
         imageName = "myImage"<>ToString[i];
         s=s<>"\\includegraphics[width=0.3\\textwidth]{"<>imageName<>"}\\\\ \\hline \n"
         , 
        {i, 1, Length@functions}
     ];
     s=s<>"\\end{longtable}
           %\\caption{My table of functions}
           %\\end{table}
           \\end{center}";
     s=s<>"\\end{document}";
     WriteString[file,s];
     Close[file];