#767: Fix #709: typing '\text{' barfs

diff --git a/src/commands/math/LatexCommandInput.js b/src/commands/math/LatexCommandInput.js
index fb4e18e..19dfc82 100644
--- a/src/commands/math/LatexCommandInput.js
+++ b/src/commands/math/LatexCommandInput.js
@@ -33,7 +33,7 @@
       if (ch.match(/[a-z]/i)) VanillaSymbol(ch).createLeftOf(cursor);
       else {
         this.parent.renderCommand(cursor);
-        if (ch !== '\\' || !this.isEmpty()) this.parent.parent.write(cursor, ch);
+        if (ch !== '\\' || !this.isEmpty()) cursor.parent.write(cursor, ch);
       }
     };
     this.ends[L].keystroke = function(key, e, ctrlr) {
diff --git a/src/commands/text.js b/src/commands/text.js
index 59614ee..5af2946 100644
--- a/src/commands/text.js
+++ b/src/commands/text.js
@@ -65,7 +65,7 @@
   _.latex = function() {
     var contents = this.textContents();
     if (contents.length === 0) return '';
-    return '\\text{' + contents + '}';
+    return '\\text{' + contents.replace(/\\/g, '\\backslash ').replace(/[{}]/g, '\\$&') + '}';
   };
   _.html = function() {
     return (
diff --git a/test/unit/publicapi.test.js b/test/unit/publicapi.test.js
index 8c1d8bb..f980855 100644
--- a/test/unit/publicapi.test.js
+++ b/test/unit/publicapi.test.js
@@ -521,7 +521,7 @@
       });
       test('backslashes', function() {
         assertPaste('something \\pi something \\asdf',
-                    '\\text{something \\pi something \\asdf}');
+                    '\\text{something \\backslash pi something \\backslash asdf}');
       });
       // TODO: braces (currently broken)
       test('actual math LaTeX wrapped in dollar signs', function() {
diff --git a/test/unit/typing.test.js b/test/unit/typing.test.js
index b67677e..c77fc90 100644
--- a/test/unit/typing.test.js
+++ b/test/unit/typing.test.js
@@ -57,6 +57,11 @@
       mq.typedText('$');
       assertLatex('\\$');
     });
+
+    test('\\text followed by command', function() {
+      mq.typedText('\\text{');
+      assertLatex('\\text{\\{}');
+    });
   });
 
   suite('auto-expanding parens', function() {