Skip to content

Instantly share code, notes, and snippets.

@royshil
Last active December 30, 2015 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save royshil/7810388 to your computer and use it in GitHub Desktop.
Save royshil/7810388 to your computer and use it in GitHub Desktop.
LaTeX Colorizer for Google Documents.Just add it as a script to your GDoc via Tools -> Script Editor (then create a blank script and copy-paste). Don't forget to plug in your GDoc ID in the right place (you can take it right off the URL). Rob Miller's group from CSAIL also created a similar thing: https://github.com/uid/gdoc-downloader/blob/mast…
function onOpen() {
// Add a menu with items to colorize the whole document or colorize around the cursor only
DocumentApp.getUi().createMenu('LaTeX')
.addItem('Colorize LaTeX global', 'searchAndReplace')
.addItem('Colorize LaTeX local', 'searchAndReplaceLocal')
.addToUi();
}
/**
* "normalize" the text, remove coloring
*/
function normalizeElement(p) {
if(p == null) return;
if(typeof p === 'undefined') return;
//normalize paragraph text
if(p.getType() == DocumentApp.ElementType.PARAGRAPH)
p.setHeading(DocumentApp.ParagraphHeading.NORMAL);
p.editAsText().setForegroundColor('#000000').setBackgroundColor("#ffffff");
}
function colorizeHeading(p) {
var isTitle = false;
var sr = p.findText("\\\\title{[^}]+}");
if(sr != null) {
p.setHeading(DocumentApp.ParagraphHeading.TITLE);
isTitle = true;
}
sr = p.findText("\\\\section{[^}]+}");
if(sr != null) {
p.setHeading(DocumentApp.ParagraphHeading.HEADING1);
isTitle = true;
}
sr = p.findText("\\\\subsection{[^}]+}");
if(sr != null) {
p.setHeading(DocumentApp.ParagraphHeading.HEADING2);
isTitle = true;
}
sr = p.findText("\\\\subsubsection{[^}]+}");
if(sr != null) {
p.setHeading(DocumentApp.ParagraphHeading.HEADING3);
isTitle = true;
}
//Not a heading - normalize again to 11pt, no bold or italic, Arial font
if(!isTitle) {
p.editAsText()
.setBold(false)
.setItalic(false)
.setFontFamily(DocumentApp.FontFamily.ARIAL)
.setFontSize(11);
}
}
/**
* Colorize the paragraph's LaTeX commands
*/
function colorizeParagraph(p) {
var sr = p.findText("[^\\\\]%.+$");
if(sr != null)
sr.getElement().editAsText().setForegroundColor(sr.getStartOffset(),sr.getEndOffsetInclusive(),'#11aa11');
else {
sr = p.findText("^%.+$");
if(sr != null)
sr.getElement().editAsText().setForegroundColor(sr.getStartOffset(),sr.getEndOffsetInclusive(),'#11aa11');
else {
//.setHeading works on Paragraph objects only, so check for that
if(p.getType() == DocumentApp.ElementType.PARAGRAPH) {
colorizeHeading(p);
}
sr = p.findText("\\\\[a-zA-Z]+");
while(sr != null) {
sr.getElement().editAsText().setForegroundColor(sr.getStartOffset(),sr.getEndOffsetInclusive(),'#0000ff');
sr = p.findText("\\\\[a-zA-Z]+",sr);
}
sr = p.findText("\\\\textbf{[^}]+");
while(sr != null) {
sr.getElement().editAsText().setBold(sr.getStartOffset()+8,sr.getEndOffsetInclusive(),true);
sr = p.findText("\\\\textbf{[^}]+",sr);
}
sr = p.findText("\\\\textit{[^}]+");
while(sr != null) {
sr.getElement().editAsText().setItalic(sr.getStartOffset()+8,sr.getEndOffsetInclusive(),true);
sr = p.findText("\\\\textit{[^}]+",sr);
}
}
}
}
/**
* Colorize around the cursor only - will be faster
*/
function searchAndReplaceLocal() {
var cursor = DocumentApp.getActiveDocument().getCursor();
var p = cursor.getElement();
normalizeElement(p);
colorizeParagraph(p);
}
/**
* Colorizes the entire document, work paragraph-by-paragraph
*/
function searchAndReplace() {
var bodyElement = DocumentApp.getActiveDocument().getBody();
normalizeElement(bodyElement);
var ps = bodyElement.getParagraphs();
for (var i = 0; i < ps.length; ++i) {
var p = ps[i];
normalizeElement(p);
colorizeParagraph(p);
}
/*
var sr = bodyElement.findText("\\\\if");
if(sr != null) {
DocumentApp.getUi().alert("found if at " + sr.getStartOffset());
var endsr = bodyElement.findText("\\\\fi", sr);
if(endsr != null)
bodyElement.editAsText().setForegroundColor(sr.getStartOffset(), endsr.getEndOffsetInclusive(), '#050505');
}
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment