zepinglee / citeproc-lua

A Lua implementation of the Citation Style Language (CSL)
MIT License
62 stars 7 forks source link

Few issues with the LaTeX formatter #4

Closed michal-h21 closed 2 years ago

michal-h21 commented 2 years ago

Hi, I've found few issues the text_escape function in the LaTeX formatter. One issue is fatal, there is a missing string concatenation in string.gsub. The second issue is missing escaping of the $ pattern, so it adds $ character at end of each string. So the example Lua script produces something like: (\$F.G.\$ \$Bennett\$ \$Jr.,\$ \$2009\$; \$B.\$ \$D’Arcus\$, \$2005\$)\$, for example.

Here is a patch that should fix these issues.

diff --git a/citeproc/citeproc-formats.lua b/citeproc/citeproc-formats.lua
index f22186f..6c998a8 100644
--- a/citeproc/citeproc-formats.lua
+++ b/citeproc/citeproc-formats.lua
@@ -61,7 +61,7 @@ formats.latex = {
   ["text_escape"] = function (str)
     str = str:gsub("\\", "\\textbackslash")
     str = str:gsub("#", "\\#")
-    str = str:gsub("$", "\\$")
+    str = str:gsub("%$", "\\$")
     str = str:gsub("%%", "\\%")
     str = str:gsub("&", "\\&")
     str = str:gsub("{", "\\{")
@@ -69,7 +69,7 @@ formats.latex = {
     str = str:gsub("_", "\\_")
     str = str:gsub(util.unicode["no-break space"], "~")
     for char, sub in pairs(util.superscripts) do
-      str = string.gsub(str, char, "\\textsuperscript{" .. sub "}")
+      str = string.gsub(str, char, "\\textsuperscript{" .. sub .. "}")
     end
     return str
   end,
michal-h21 commented 2 years ago

BTW, I've created simple LaTeX package that shows usage of Citeproc-lua with LuaLaTeX: https://tex.stackexchange.com/a/618815/2891.

zepinglee commented 2 years ago

I fixed it in ed2dfe5.

BTW, I've created simple LaTeX package that shows usage of Citeproc-lua with LuaLaTeX: https://tex.stackexchange.com/a/618815/2891.

It is a great start for the LuaLaTeX package.