-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhotoToTex.java
More file actions
117 lines (104 loc) · 3.61 KB
/
PhotoToTex.java
File metadata and controls
117 lines (104 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.util.*;
import java.io.*;
public class PhotoToTex() {
public static final String[] OPERTIONS = {"+", "-", "x", "*", "÷"};
public static final String[] RELATIONS = {"=", "≤", "≥"};
public static final String[] SETS = {"∩", "∪", "∈", "∉"};
private String math;
private String newLineWithSeparation;
private ArrayList<String> list;
public static void main(String[] args) {
String dir = "This PC\Welson P20 Pro\Internal shared storage\Download";
String tex_file_name = "new_tex";
list = new ArrayList<String>;
for (String[] str1: OPERATIONS) {
list.add(str1);
}
for (String[] str2: RELATIONS) {
list.add(str2);
}
for (String[] str3: SETS) {
list.add(str3);
}
newLineWithSeparation = System.getProperty("line.separator")+System.getProperty("line.separator");
math = "\\documentclass{article}" + newLineWithSeparation;
math += "\\usepackage{amsfonts}" + newLineWithSeparation;
math += "\\usepackage{amssymb}" + newLineWithSeparation;
math += "\\usepackage{amsmath}" + newLineWithSeparation;
math += "\\usepackage{mathtools}" + newLineWithSeparation;
math += "\\begin{document}" + newLineWithSeparation;
readFile(args[0]);
math += "\\end{document}";
writeFile(dir, tex_file_name);
}
public void readFile(String fileName) {
// This will reference one line at a time
String line = null;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
convertToLatex(line);
}
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" + fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '" + fileName + "'");
// Or we could just do this: ex.printStackTrace();
}
}
public void writeFile(String dir, String tex_file_name) {
FileWriter writer = null;
try {
writer = new FileWriter(dir + "\\" + tex_file_name + ".tex", false);
writer.write(math, 0, math.length());
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void convertToLatex(String line) {
String[] splited = line.trim().split("\\s+");
for (String str: splited) {
if (list.contains(str)) {
switch (str) {
case "+": math += "\\plus" + " ";
break;
case "-": math += "\\minus" + " ";
break;
case "x": math += "\\times" + " ";
break;
case "*": math += "\\ast" + " ";
break;
case "÷": math += "\\div" + " ";
break;
case "=": math += "\\equals" + " ";
break;
case "≤": math += "\\leq" + " ";
break;
case "≥": math += "\\geq" + " ";
break;
case "∩": math += "\\cap" + " ";
break;
case "∪": math += "\\cup" + " ";
break;
case "∈": math += "\\in" + " ";
break;
case "∉": math += "\\notin" + " ";
break;
}
} else {
math += str + " ";
}
}
math = math.trim() + newLineWithSeparation;
}
}