From baf9812cb050c804d4d12bf3e2819bd4a5987722 Mon Sep 17 00:00:00 2001
From: Matt Huszagh <huszaghmatt@gmail.com>
Date: Thu, 5 Dec 2019 23:16:39 -0800
Subject: [PATCH] org.el: Remove leading/trailing whitespace from latex
 fragment

* lisp/org.el (org-create-formula-image): Ensure user input ends
with a % character to remove trailing whitespace. Also, remove
newlines from macro calls.
---
 lisp/org.el | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 9b84592ba..4e3dbe4c3 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -16554,13 +16554,18 @@ a HTML file."
 	(setq bg (org-latex-color :background))
       (setq bg (org-latex-color-format
 		(if (string= bg "Transparent") "white" bg))))
+    ;; remove tex \par at end of snippet to avoid trailing
+    ;; whitespace
+    (if (string= (substring string -1 nil) "\n")
+        (aset string (- (length string) 1) ?%)
+      (setq string (concat string "%")))
     (with-temp-file texfile
       (insert latex-header)
       (insert "\n\\begin{document}\n"
-	      "\\definecolor{fg}{rgb}{" fg "}\n"
-	      "\\definecolor{bg}{rgb}{" bg "}\n"
-	      "\n\\pagecolor{bg}\n"
-	      "\n{\\color{fg}\n"
+	      "\\definecolor{fg}{rgb}{" fg "}"
+	      "\\definecolor{bg}{rgb}{" bg "}"
+	      "\\pagecolor{bg}"
+	      "{\\color{fg}\n"
 	      string
 	      "\n}\n"
 	      "\n\\end{document}\n"))
-- 
2.24.0

