4
$\begingroup$

I have a lis

{{{-12,2,-1},{-11,1,-5},{-10,-2,3},10 x+6 y+z+109==0},{{-12,2,-1},{-11,1,-5},{-10,6,3},2 x-2 y+z+29==0},{{-12,2,-1},{-11,1,-5},{-9,5,-7},3 x-y+z+39==0}}

I would like to conver lis to text file has the form

\documentclass[12pt,a4paper]{article}
\usepackage{enumitem}
\usepackage[left=2cm,right=2 cm,top=2cm,bottom=2cm]{geometry}
\begin{document}
\begin{enumerate}[label=\arabic*)]
\item $A(-12,2,-1)$, \quad $B(-11,1,-5)$, \quad $C(-10,-2,3)$, \quad $(P):10 x+6 y+z+109=0$.
\item $A(-12,2,-1)$, \quad $B(-11,1,-5)$, \quad $C(-10,6,3)$, \quad $(P):2 x-2 y+z+29=0$.
\end{enumerate}
\end{document}

enter image description here

Base on the answer

I tried

lis={{{-12,2,-1},{-11,1,-5},{-10,-2,3},10 x+6 y+z+109==0},{{-12,2,-1},{-11,1,-5},{-10,6,3},2 x-2 y+z+29==0},{{-12,2,-1},{-11,1,-5},{-9,5,-7},3 x-y+z+39==0}}
SetDirectory[NotebookDirectory[]]
fileName=FileNameJoin[{Directory[],"plane.tex"}]
If[FileExistsQ[fileName],DeleteFile[fileName]];
file=OpenWrite[fileName,PageWidth->Infinity];
WriteString[file,"\\documentclass[12pt,a4paper]{article}\n"<>"\\usepackage{enumitem}\n"<>"\\begin{document}\n"<>"\\begin{enumerate}[label=\\arabic*)]\n"];

Do[z=ToString[lis[[n,1]]]<>", \\quad "<>ToString[lis[[n,2]]];
z=StringReplace[z,"{"->"$("];
z=StringReplace[z,"}"->")$"];
WriteString[file,"\\item "<>z<>"\n"],{n,1,Length@lis}];
WriteString[file,"\\end{enumerate}\n\\end{document}\n"];
Close[file]

I only get

\documentclass[12pt,a4paper]{article}
\usepackage{enumitem}
\begin{document}
\begin{enumerate}[label=\arabic*)]
\item $(-12, 2, -1)$, \quad $(-11, 1, -5)$
\item $(-12, 2, -1)$, \quad $(-11, 1, -5)$
\item $(-12, 2, -1)$, \quad $(-11, 1, -5)$
\end{enumerate}
\end{document}
$\endgroup$

1 Answer 1

6
$\begingroup$

This code generates this PDF

enter image description here

Code

ClearAll["Global`*"]
lis = {
  {{-12, 2, -1}, {-11, 1, -5}, {-10, -2, 3}, 
   10 x + 6 y + z + 109 == 0},
  {{-12, 2, -1}, {-11, 1, -5}, {-10, 6, 3}, 2 x - 2 y + z + 29 == 0},
  {{-12, 2, -1}, {-11, 1, -5}, {-9, 5, -7}, 3 x - y + z + 39 == 0}
  }

SetDirectory[NotebookDirectory[]]
fileName = FileNameJoin[{Directory[], "main.tex"}]
If[FileExistsQ[fileName], DeleteFile[fileName]];
file = OpenWrite[fileName, PageWidth -> Infinity];
WriteString[file, "\\documentclass[12pt,a4paper]{article}\n" <>
   "\\usepackage{enumitem}\n" <>
   "\\begin{document}\n" <>
   "\\begin{enumerate}[label=\\arabic*)]\n"];

format[e_List] := ("A" <> ToString[e[[1]]] <> ",\\quad B" <> 
    ToString[e[[2]]] <> ",\\quad C" <> ToString[e[[3]]] <> 
    ",\\quad (P) : $" <> ToString[TeXForm[e[[4]]]] <> "$");
Do[myTMP = format[lis[[n]]];
  myTMP = StringReplace[myTMP, "{" -> "("];
  myTMP = StringReplace[myTMP, "}" -> ")"];
  WriteString[file, "\\item " <> myTMP <> "\n"],
  {n, 1, Length@lis}
  ];
WriteString[file, "\\end{enumerate}\n\\end{document}\n"];
Close[file]

Latex file generated

\documentclass[12pt,a4paper]{article}
\usepackage{enumitem}
\begin{document}
\begin{enumerate}[label=\arabic*)]
\item A(-12, 2, -1),\quad B(-11, 1, -5),\quad C(-10, -2, 3),\quad (P) : $10 x+6 y+z+109=0$
\item A(-12, 2, -1),\quad B(-11, 1, -5),\quad C(-10, 6, 3),\quad (P) : $2 x-2 y+z+29=0$
\item A(-12, 2, -1),\quad B(-11, 1, -5),\quad C(-9, 5, -7),\quad (P) : $3 x-y+z+39=0$
\end{enumerate}
\end{document}

Update

To make each field Math, you can use this version

enter image description here

Code

ClearAll["Global`*"]
lis = {
  {{-12, 2, -1}, {-11, 1, -5}, {-10, -2, 3}, 
   10 x + 6 y + z + 109 == 0},
  {{-12, 2, -1}, {-11, 1, -5}, {-10, 6, 3}, 2 x - 2 y + z + 29 == 0},
  {{-12, 2, -1}, {-11, 1, -5}, {-9, 5, -7}, 3 x - y + z + 39 == 0}
  }

SetDirectory[NotebookDirectory[]]
fileName = FileNameJoin[{Directory[], "main.tex"}]
If[FileExistsQ[fileName], DeleteFile[fileName]];
file = OpenWrite[fileName, PageWidth -> Infinity];
WriteString[file, "\\documentclass[12pt,a4paper]{article}\n" <>
   "\\usepackage[letterpaper,margin=1.2in]{geometry}\n" <>
   "\\usepackage{enumitem}\n" <>
   "\\begin{document}\n" <>
   "\\begin{enumerate}[label=\\arabic*)]\n"];

format[e_List] := ("$A" <> ToString[e[[1]]] <> "$,\\quad $B" <> 
    ToString[e[[2]]] <> "$,\\quad $C" <> ToString[e[[3]]] <> 
    "$,\\quad $(P) : " <> ToString[TeXForm[e[[4]]]] <> "$");
Do[myTMP = format[lis[[n]]];
  myTMP = StringReplace[myTMP, "{" -> "("];
  myTMP = StringReplace[myTMP, "}" -> ")"];
  WriteString[file, "\\item " <> myTMP <> "\n"],
  {n, 1, Length@lis}
  ];
WriteString[file, "\\end{enumerate}\n\\end{document}\n"];
Close[file]

Which generates this latex file

\documentclass[12pt,a4paper]{article}
\usepackage[letterpaper,margin=1.2in]{geometry}
\usepackage{enumitem}
\begin{document}
\begin{enumerate}[label=\arabic*)]
\item $A(-12, 2, -1)$,\quad $B(-11, 1, -5)$,\quad $C(-10, -2, 3)$,\quad $(P) : 10 x+6 y+z+109=0$
\item $A(-12, 2, -1)$,\quad $B(-11, 1, -5)$,\quad $C(-10, 6, 3)$,\quad $(P) : 2 x-2 y+z+29=0$
\item $A(-12, 2, -1)$,\quad $B(-11, 1, -5)$,\quad $C(-9, 5, -7)$,\quad $(P) : 3 x-y+z+39=0$
\end{enumerate}
\end{document}
$\endgroup$
7
  • $\begingroup$ Can I use mathmode instead TeXForm? $\endgroup$ Jan 28 at 3:43
  • $\begingroup$ @JohnPaulPeter what is mathmode ? I never seen this function in Mathematica. TeXForm is used to convert mathematica math expressions to Latex in Mathematica. Since the 4th field in your list is an question, I used TeXForm on it. $\endgroup$
    – Nasser
    Jan 28 at 4:06
  • $\begingroup$ Please see my text file. $\endgroup$ Jan 28 at 4:11
  • $\begingroup$ @JohnPaulPeter fyi, updated. Hopefully this is what you meant. $\endgroup$
    – Nasser
    Jan 28 at 4:23
  • $\begingroup$ Thank you very much. $\endgroup$ Jan 28 at 5:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.