Skip to content

Commit 05138c1

Browse files
authored
Merge pull request #12518 from quarto-dev/bugfix/issue-11695
LaTeX, ansi - escape latex inside highlighting environments
2 parents e95a894 + d023ee2 commit 05138c1

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

news/changelog-1.7.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ All changes included in 1.7:
7878

7979
## `pdf`
8080

81+
- ([#11695](https://github.com/quarto-dev/quarto-cli/issues/11695)): Translate ANSI color codes more carefully inside `highlighting` environments.
8182
- ([#11835](https://github.com/quarto-dev/quarto-cli/issues/11835)): Take markdown structure into account when detecting minimum heading level.
8283
- ([#11878](https://github.com/quarto-dev/quarto-cli/issues/11878), [#12085](https://github.com/quarto-dev/quarto-cli/issues/12085)): Correctly fixup raw LaTeX table having an unexpected table env with options (e.g `\begin{table}[!ht]`) to be handled as crossref table.
8384
- ([#11903](https://github.com/quarto-dev/quarto-cli/issues/11903)): `crossref` configuration like `fig-title` or `tbl-title` now correctly supports multi word values, e.g. `fig-title: 'Supplementary Figure'`.

src/resources/filters/quarto-post/latex.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: "foo"
3+
format: pdf
4+
keep-tex: true
5+
---
6+
7+
::: {.cell execution_count=1}
8+
``` {.python .cell-code}
9+
stuff
10+
```
11+
12+
::: {.cell-output .cell-output-error}
13+
```
14+
 793 err_msg += '\n' + '\n'.join(remarks)
15+
```
16+
```
17+
Bad but handled somehow: \n
18+
```
19+
:::
20+
:::

0 commit comments

Comments
 (0)