Skip to content

Fix as_latex_escaped: correct RGB channel order and backslash escaping - #702

Merged
keith-hall merged 2 commits into
trishume:masterfrom
teddytennant:fix-as-latex-escaped
Oct 27, 2026
Merged

keith-hall merged 2 commits into
trishume:masterfrom
teddytennant:fix-as-latex-escaped

Conversation

@teddytennant

Copy link
Copy Markdown

Fixes two bugs in as_latex_escaped (src/util.rs):

  1. Swapped color channels. The \textcolor[RGB]{...} directive was emitted with the green and blue components transposed (style.foreground.r, style.foreground.b, style.foreground.g), so colors came out wrong.

  2. Broken backslash escaping. A literal backslash in the text was escaped to \\, which is a line break inside the commandchars=\\\{\} Verbatim environment rather than a literal backslash, and the sequential .replace() loop could re-process characters it had just introduced. Escaping is now done in a single pass over the input, mapping \ → \textbackslash{}, { → \{, } → \}, which produces LaTeX that compiles.

For input a\b{c} with foreground RGB(10,20,30) the output is now \textcolor[RGB]{10,20,30}{a\textbackslash{}b\{c\}}.

Test

Added test_as_latex_escaped, which builds a Style with a known foreground color and text containing a backslash and braces, and asserts the channel order, \textbackslash{}, escaped braces, and the absence of a bare \\.

cargo test --lib as_latex

Closes #472.

Copilot AI review requested due to automatic review settings July 8, 2026 00:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Comment thread src/util.rs Outdated
Move the escaping into a `write_latex_escaped` helper that searches for
the next `\`, `{` or `}` and copies the text between matches in one go,
rather than pushing every char individually. Text containing none of
those characters is now appended with a single copy.

Also assert the exact output in the test, and cover text with no special
characters, multi-byte characters and a style change.
@teddytennant

Copy link
Copy Markdown
Author

Good idea. It now searches for the next \, { or } and copies everything between matches in one go, so text with none of those is a single append. Pulled it out into a write_latex_escaped helper and added tests for the no-special-chars and multi-byte cases.

@keith-hall keith-hall left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good. I'm a little unsure of when best to merge it, please bear with me. We are in the middle of supporting newer sublime-syntax features etc and I wonder about conflicts with #627 also

@teddytennant

Copy link
Copy Markdown
Author

627 already has the RGB channel order, but it still maps \ to \\ via LATEX_REPLACE, which is a line break inside Verbatim. this PR is the escaping fix.

@keith-hall
keith-hall merged commit 582e445 into trishume:master Oct 27, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

as_latex_escaped: two small issues

3 participants