-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocToLatex.gs
105 lines (94 loc) · 3.13 KB
/
DocToLatex.gs
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
const TITLE = 1;
const BODY = 0;
const AUTHOR = "Asad Hasan (Alex)";
const AUTHOR_TITLE = "Theoretical Computational Scientist";
// Edit these title patterns
const titlePatterns = ["Preface", "Foreword", "Chapter\\s\\d.+"];
const noIndentChapterPatterns = ["Preface", "Chapter 12.+"];
function main() {
const { chapterTitle, chapterTitleTest } = chapterRegex();
let noIndentRegex = chapterIndentRegex();
let doc = DocumentApp.getActiveDocument();
let text = doc.getBody().getText();
let title = doc.getName();
let chapterTexts = text.split(chapterTitle);
let template = initializeTemplate(title);
let previousNoIndent = false;
let i = 0;
for (let chapterText of chapterTexts) {
if (chapterText) {
if (chapterTitleTest.test(chapterText)) {
previousNoIndent = false;
if (noIndentRegex.test(chapterText)) previousNoIndent = true;
template += `\\chapter*{${chapterText.trim()}}
`;
} else if (previousNoIndent) {
template += `${chapterText}
`;
} else {
template += `${indentParagraph(chapterText)}
`;
}
}
}
template += `\\end{document}`;
// Create a file in Google Drive
let folder = DriveApp.getRootFolder(); // Change to the desired folder if needed
let fileName = `${title}.tex`; // Name of the file
folder.createFile(fileName, template);
}
function indentParagraph(paragraphText) {
return paragraphText
.trim()
.replace(/^/g, "\n\\indent ")
.replace(/\n/g, "\n\n\\indent ");
}
function initializeTemplate(title) {
let template = `\\documentclass[ebook,12pt,oneside,openany]{memoir}
\\usepackage[utf8x]{inputenc}
\\usepackage[english]{babel}
\\usepackage{url}
\\usepackage{titlesec}
\\usepackage{lettrine}
\\usepackage{amsthm}
\\usepackage{amssymb}
% for placeholder text
\\usepackage{lipsum}
\\title{${title}}
\\author{${AUTHOR} \\\\ ${AUTHOR_TITLE}}
% Remove the generated chapter title
\\titleformat{\\chapter}[display]
{\\normalfont\\huge\\bfseries}{}{0pt}{\\Huge}
\\titlespacing*{\\chapter}{0pt}{-50pt}{40pt}
\\newtheorem*{proposition}{Proposition} % Define theorem environment without numbering
\\newtheorem*{thoughtexperiment}{Thought Experiment} % Define theorem environment without numbering
\\begin{document}
\\makeatletter
\\renewcommand{\\@date}{} % Remove the definition of \\@date
\\makeatother
\\maketitle
`;
return template;
}
function chapterRegex() {
let chapterTitleLiteral = "";
let chapterTitleTestLiteral = "";
for (let p of titlePatterns) {
chapterTitleLiteral += `(${p})|`;
chapterTitleTestLiteral += `(^${p})|`;
}
chapterTitleLiteral = chapterTitleLiteral.replace(/\|$/, "");
chapterTitleTestLiteral = chapterTitleTestLiteral.replace(/\|$/, "");
const chapterTitle = new RegExp(chapterTitleLiteral, "g");
const chapterTitleTest = new RegExp(chapterTitleTestLiteral, "g");
return { chapterTitle, chapterTitleTest };
}
function chapterIndentRegex() {
let chapterTitleLiteral = "";
for (let p of noIndentChapterPatterns) {
chapterTitleLiteral += `(${p})|`;
}
chapterTitleLiteral = chapterTitleLiteral.replace(/\|$/, "");
const chapterTitle = new RegExp(chapterTitleLiteral, "g");
return chapterTitle;
}