Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions TeXmacs/plugins/latex/progs/convert/latex/tmtex.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1588,10 +1588,10 @@

(define (tmtex-decode-color s . force-html)
(with cm (if (string-starts? s "#") "HTML" (named-color->xcolormap s))
(cond ((and (== cm "none") (nnull? force-html))
(cond ((== cm "HTML")
(string-append "#" (html-color->latex-xcolor s)))
((and (== cm "none") (nnull? force-html))
(tmtex-decode-color (get-hex-color s) force-html))
((and (== cm "HTML") (nnull? force-html))
`((!option "HTML") ,(html-color->latex-xcolor s)))
((== cm "texmacs")
(when (nin? s tmtex-colors)
(set! tmtex-colors (append (list s) tmtex-colors)))
Expand All @@ -1607,7 +1607,7 @@
(define (tmtex-make-color val arg)
(with ltxcolor (tmtex-decode-color val #t)
(if (list? ltxcolor)
`(!group (!append (color ,@ltxcolor) ,arg))
`(!group (!append (color ,@ltxcolor) (!group ,arg)))
`(tmcolor ,ltxcolor ,arg))))

(define (post-process-math-text t)
Expand Down
8 changes: 4 additions & 4 deletions TeXmacs/progs/convert/latex/tmtex.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1588,10 +1588,10 @@

(define (tmtex-decode-color s . force-html)
(with cm (if (string-starts? s "#") "HTML" (named-color->xcolormap s))
(cond ((and (== cm "none") (nnull? force-html))
(cond ((== cm "HTML")
(string-append "#" (html-color->latex-xcolor s)))
Comment on lines +1591 to +1592
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 force-html guard removed from HTML branch — silent behavior change for colorbox/fcolorbox/ornament-color

Previously the HTML case was gated on (nnull? force-html), so callers that pass no force-html argument (lines 2371, 2374, 2699 — used for \colorbox, \fcolorbox, and ornament-color) would fall through to the else branch and return the raw string (e.g. "#a66") unchanged. Now those callers also receive the normalized "#AA6666" form.

This is probably an improvement (the normalized 6-digit hex is more portable with xcolor), but it is an unintentional side effect. The force-html variadic parameter now has no effect on the HTML path at all — only the none recursion still consults it. Consider renaming it to force-expand or adding a comment documenting that its scope has narrowed, to prevent future confusion.

((and (== cm "none") (nnull? force-html))
(tmtex-decode-color (get-hex-color s) force-html))
((and (== cm "HTML") (nnull? force-html))
`((!option "HTML") ,(html-color->latex-xcolor s)))
((== cm "texmacs")
(when (nin? s tmtex-colors)
(set! tmtex-colors (append (list s) tmtex-colors)))
Expand All @@ -1607,7 +1607,7 @@
(define (tmtex-make-color val arg)
(with ltxcolor (tmtex-decode-color val #t)
(if (list? ltxcolor)
`(!group (!append (color ,@ltxcolor) ,arg))
`(!group (!append (color ,@ltxcolor) (!group ,arg)))
Comment on lines 1609 to +1610
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Dead code branch after the HTML-path change

After the refactor, tmtex-decode-color always returns a string — the "HTML" branch now returns (string-append "#" ...), and every other branch also returns a string or recurses (which also now returns a string). Therefore (list? ltxcolor) is always #f, and the (!group (!append (color ,@ltxcolor) (!group ,arg))) line can never be reached.

The (!group ,arg) addition that was part of this PR's fix is consequently also dead code. If the grouping fix is needed, it should be applied to the active code path (the else branch via tmcolor), although the tmcolor macro definition already wraps arg in (!group 2) — so grouping is already handled there.

Consider either removing the dead list branch, or documenting why it is preserved as a future extension point.

`(tmcolor ,ltxcolor ,arg))))

(define (post-process-math-text t)
Expand Down
45 changes: 45 additions & 0 deletions TeXmacs/tests/203_27.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; MODULE : 203_27.scm
;; DESCRIPTION : Unit tests for partial color LaTeX export
;; COPYRIGHT : (C) 2026 AcceleratorX
;;
;; This software falls under the GNU general public license version 3 or later.
;; It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
;; in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(import (liii check))

(check-set-mode! 'report-failed)

(define (normalize-latex latex)
(set! latex (string-replace latex "\r\n" "\n"))
(set! latex (string-replace latex "\r" ""))
(set! latex (string-replace latex "\n" ""))
(set! latex (string-replace latex "\t" ""))
(string-replace latex " " ""))

(define (test-export-latex-partial-color)
(use-modules
(data latex)
(data tmu))
(let* ((tmu-doc (string-load "$TEXMACS_PATH/tests/tmu/203_27.tmu"))
(latex (normalize-latex
(texmacs->latex-document (tmu->texmacs tmu-doc) '()))))
(check (string-contains? latex "\\color{red}{ddd}") => #t)
(check (string-contains? latex
(string-append "\\color{" "#" "AA6666}{ddd}"))
=> #t)
(check (string-contains? latex "\\color{blue}{ddd}") => #t)
(check (string-contains? latex
(string-append "\\color[HTML]{" "AA6666}"))
=> #f)
(check (string-contains? latex
(string-append "\\color{" "#" "AA6666}ddd"))
=> #f)))

(tm-define (test_203_27)
(test-export-latex-partial-color)
(check-report))
25 changes: 25 additions & 0 deletions TeXmacs/tests/tmu/203_27.tmu
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<TMU|<tuple|1.1.0|2026.2.1-rc5>>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

测试文件只有输入,没有显示当前导出的实际 LaTeX 结果。建议补充:

  • 当前 PR 修复后,导出 LaTeX 的实际结果是什么
  • 与修复前的对比


<style|<tuple|generic|chinese|table-captions-above|number-europe|preview-ref>>

<\body>
<\equation*>
<with|color|red|d d d> d d d
</equation*>

<\equation*>
<with|color|#a66|d d d>d d d
</equation*>

<\equation*>
<with|color|blue|d d d> d d d
</equation*>

</body>

<\initial>
<\collection>
<associate|page-medium|paper>
<associate|page-screen-margin|false>
</collection>
</initial>
22 changes: 22 additions & 0 deletions devel/203_27.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# [203_27] 公式带前景色时,使用部分颜色时导出 LaTeX 代码会出错

## 如何测试

1. 打开测试文件 `TeXmacs/tests/tmu/203_27.tmu`,复制其中公式到 LaTeX,预期效果:
```
\[ {\color{red}{d d d}} d d d \]
\[ {\color{#AA6666}{d d d}} d d d \]
\[ {\color{blue}{d d d}} d d d \]
```

原本复制到 LaTeX 效果
```
\[ {\color{red}{d d d}} d d d \]
\[ {\color[HTML]{AA6666}d d d} d d d \]
\[ {\color{blue}{d d d}} d d d \]
```

2. 集成测试文件在 `TeXmacs/tests/203_27.scm`
```
xmake run 203_27
```
Loading