# Unable to get proper Latex format from Mathematica

Based on this answer, I tried this code

    Clear["Global*"]
list = {{a, b}, {c, d}, {m, n}} /.
Solve[{(a + c)/2 == m, (b + d)/2 == n, -5 <= a <= 5, -5 <= b <=
5, -5 <= c <= 5, -5 <= d <= 5, a > c, a  b  c  d  m  n != 0,
a != b != c != d, a ∈ Integers, b ∈ Integers,
c ∈ Integers, d ∈ Integers,
m ∈ Rationals, n ∈ Rationals,
m ∉ Integers && n ∉ Integers  }, {a, b,
c, d, m, n}];
pM = {x, y};
lis = list[[1 ;; 5]]


and got

When I tried,

    SetDirectory[NotebookDirectory[]]
fileName = FileNameJoin[{Directory[], "test.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 $$M" <> ToString[e[[3]]] <> "$$");
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]


I got the text file,

\documentclass[12pt,a4paper]{article}
\usepackage[letterpaper,margin=1.2in]{geometry}
\usepackage{enumitem}
$$\begin{document} \begin{enumerate}[label=\arabic*)] \item A(-4, -3),\quad B(-5, -2),\quad M 9 5 (-(-), -(-)) 2 2 \item A(-4, -3),\quad B(-5, 2),\quad M 9 1 (-(-), -(-)) 2 2 \item A(-4, -3),\quad B(-5, 4),\quad M 9 1 (-(-), -) 2 2 \item A(-4, -2),\quad B(-5, -3),\quad M 9 5 (-(-), -(-)) 2 2 \item A(-4, -2),\quad B(-5, -1),\quad M 9 3 (-(-), -(-)) 2 2 \end{enumerate} \end{document}$$


How can I get the correct result?

• change ToString[e[[2]]] to ToString[InputForm@e[[2]]] and same for the other one e[[3]] if you do not use inputForm you can get 2D strings which messes up things. Commented Mar 4 at 1:37
• Thanks. How can I present $M(-9/2, -5/2)$ has the form $M(-\dfrac{9}{2},-\dfrac{5}{2})$? Commented Mar 4 at 1:48
• But what is M(-9/2, -5/2) in Mathematica? This is not valid expression in Mathematica. If you are trying to generate this in Latex you'd have to do it manually. Like this ToString@TeXForm[M] <> "\\left(" <> ToString@TeXForm[-9/2] <> "," <> ToString@TeXForm[-5/2] <> "\\right)" !Mathematica graphics since you can't use TeXForm on the whole thing, You can only use TeXForm on valid Mathematica expressions. Commented Mar 4 at 2:05
• Thank you very much. Commented Mar 4 at 2:18

TeXForm can be used to convert the lists, or any Mathematica expression, to LaTeX. In this case you may want use TeXForm with MatrixForm, like this

format = ToString @* TeXForm @* MatrixForm;

enum = "\\begin{enumerate}[label=\\arabic*)]\n" <>
"\\item $$A = " <> format[list[[1]]] <> "$$\n" <>
"\\item $$B = " <> format[list[[2]]] <> "$$\n" <>
"\\item $$C = " <> format[list[[3]]] <> "$$\n" <>
"\\item $$D = " <> format[list[[4]]] <> "$$\n" <>
"\\item $$M = " <> format[list[[5]]] <> "$$\n" <>
"\\end{enumerate}\n"


This should produce the LaTeX needed to give a nice looking result in your document. Here's a screenshot

Other pieces of the document are

beginDoc =  "\\documentclass[12pt,a4paper]{article}\n" <>
"\\usepackage[letterpaper,margin=1.2in]{geometry}\n" <>
"\\usepackage{enumitem}\n" <>
"\\begin{document}\n" ;

endDoc="\\end{document}\n";


Instead of doing the whole Open-Write-Close routine, you may want to use

Export[fileName, beginDoc <> enum <> endDoc, "Text"]
`
• Please post full your code. I don't know how past your code in my document. Commented Mar 4 at 1:50