@@ -587,6 +587,23 @@ function render_latex_fixups()
587587 end
588588 end ,
589589 CodeBlock = function (code )
590+ local function escape_latex (line )
591+ -- unfortunately, we can't use stringEscape here (or pandoc.write(..., "latex")
592+ -- more generally) because it doesn't preserve multiple spaces, which the
593+ -- "highlighting" environment does
594+
595+ -- In addition, we have the following tricky situation:
596+ -- \ -> \textbackslash{}
597+ -- { -> \{
598+ --
599+ -- these two replacement rules both generate \ and {, and
600+ -- so there's no order that works. We need to use a
601+ -- unique replacement for \ first.
602+
603+ -- obtained by a local call to uuid and removing dashes
604+ local uuid = " edbdf4a3bc424f5b8ac0e95c92ef5015"
605+ return line :gsub (" [\\ ]" , uuid ):gsub (" ([{}$%&%_])" , " \\ %1" ):gsub (" [%^]" , " \\ textasciicaret{}" ):gsub (" [~]" , " \\ textasciitilde{}" ):gsub (uuid , " \\ textbackslash{}" )
606+ end
590607 if code .text :match (" \027 %[[0-9;]+m" ) and # code .classes == 0 then
591608 local lines = split (code .text , " \n " )
592609 local new_lines = pandoc .List ({
@@ -595,6 +612,7 @@ function render_latex_fixups()
595612 local cur_color = " \\ textcolor{black}"
596613 for _ , line in ipairs (lines ) do
597614 local start_color = cur_color
615+ line = escape_latex (line )
598616 line = line :gsub (" \027 %[([0-9;]+)m" , function (n )
599617 local this_color = " \\ textcolor" .. emit_quarto_ansi_color (n )
600618 cur_color = this_color
0 commit comments