dandavison / xenops

An editing environment for LaTeX mathematical documents
MIT License
222 stars 14 forks source link

xenops renders svg images in wrong background color #51

Open wang1zhen opened 2 years ago

wang1zhen commented 2 years ago

When the background color of the default font is "black", the svg image would be rendered in wrong background color instead of the black background, see the following screenshots: xenops rendering properly: 图片 xenops rendering imporperly (set-face-attribute 'default nil :background "black"): 图片

Discussion here also confirms this.

dandavison commented 2 years ago

Hi @wang1zhen Xenops honors the :foreground and :background keys in the variable org-format-latex-options. Can you try using that? org docs and online discussion should help, but let me know if it's not clear to you how to proceed.

wang1zhen commented 2 years ago

Hi @dandavison , thanks for your reply.

I have tried (plist-put org-format-latex-options :background "Black"), and here is the value of org-format-latex-options:

(:foreground default :background "Black" :scale 2.0 :html-foreground "Black" :html-background "Transparent" :html-scale 1.0 :matchers
             ("begin" "$1" "$" "$$" "\\(" "\\["))

However, it did not help, the image is still rendered incorrectly

图片

This only happens when the background color of emacs is set to purely black, as tested in https://github.com/protesilaos/modus-themes/issues/36 , I do not know if this piece of information helps?

dandavison commented 2 years ago

However, it did not help, the image is still rendered incorrectly

Have you checked that xenops is not using the cached version of the image? For example, enter LaTeX code that you have never entered before. You can also delete the files in xenops-cache-directory.

wang1zhen commented 2 years ago

Yes, I can confirm.

图片

Actually, the svg file generated is correct,

图片

The issue could be how they are rendered in emacs.

dandavison commented 2 years ago

OK, thanks for confirming.

If you simply open that SVG in emacs, without using xenops, does it render correctly?

Can you give me instructions to reproduce the problem you are experiencing?

wang1zhen commented 2 years ago

If you simply open that SVG in emacs, without using xenops, does it render correctly?

This seems to be the issue!

The SVG generated is simply "white on black", and the rendering is perfect on default theme:

图片

When I simply switch to another theme, it seems that the rendering mechanism for svg is not working as expected, for example, here is what I got in tango-dark theme:

图片

which is kink of "white on grey", and for wheatgrass theme:

图片

I think you can reproduce it with any dark built-in themes for emacs.

failable commented 2 years ago

Any news of this?

I get the same issue.

Screen Shot 2022-08-31 at 20 24 51

I don't see the white border when I open the SVG in firefox and Emacs...

Screen Shot 2022-08-31 at 20 45 39

When I don't use any theme, the preview looks like

Screen Shot 2022-08-31 at 20 47 39

The value of org-format-latex-options is (:foreground default :background default :scale 1.0 :html-foreground "Black" :html-background "Transparent" :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\[")).

My emacs version is: GNU Emacs 28.1 (build 1, x86_64-apple-darwin21.6.0, Carbon Version 165 AppKit 2113.6) of 2022-08-30.

And the issue does not exists in org-fragtog generated SVG...


Updated: I seem to find the cause of the issue. I have the following face setting in my config

 '(org-block ((t (:inherit (shadow fixed-pitch) :extend t :background "gray98"))))

How can I fixed this while keeping the setting?

wang1zhen commented 2 years ago

Hi @liebkne , it seems we are not facing the same problem, but I have a not quite elegant fix that maybe works for you, try set xenops-math-latex-process to dvipng, it seems my problem on goes with svg rendering.

dandavison commented 2 years ago

Hi @liebkne, would it be possible / convenient for you to create a minimal reproducible example of an emacs.el and an org file that results in this problem? The example init.el doesn't have to install any packages; just the org/xenops config.

(I'm slightly worried that image quality will be degraded if you switch to PNG but if that's not the case then great. I use a Mac laptop and have struggled to get crisp PNGs; this was actually one of the reasons I first wrote xenops, because the PNGs produced by auctex didn't look right on a mac retina screen, whereas SVGs looked great, but auctex didn't support SVGs)

failable commented 2 years ago

@dandavison Hi, thanks for the quick response. It took me sometime to extract reproducible config from my .emacs... Here it is

(setq straight-use-package-by-default t)
(defvar bootstrap-version)
(let ((bootstrap-file
       (expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
      (bootstrap-version 5))
  (unless (file-exists-p bootstrap-file)
    (with-current-buffer
        (url-retrieve-synchronously
         "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
         'silent 'inhibit-cookies)
      (goto-char (point-max))
      (eval-print-last-sexp)))
  (load bootstrap-file nil 'nomessage))

(straight-use-package 'use-package)
(straight-use-package
 '(org :type git
       :repo "https://code.orgmode.org/bzg/org-mode.git"
       :branch "release_9.5.4"
       :local-repo "org"
       :depth full
       :pre-build (straight-recipes-org-elpa--build)
       :build (:not autoloads)
       :files (:defaults "lisp/*.el"
                         ("etc/styles/" "etc/styles/*")
                         ("etc/csl/" "etc/csl/*"))))

(use-package xenops
  :hook (org-mode . xenops-mode))

(setq org-highlight-latex-and-related '(native script entities))

(custom-set-faces
 '(org-block ((t (:background "red" :extend)))))

Running /Applications/Emacs.app/Contents/MacOS/Emacs -Q -l ~/Downloads/test_xenops.el and opening some org file with

\begin{equation}
a + b = d
\end{equation}

#+begin_src elisp
  (message "a")
#+end_src

results in

Screen Shot 2022-09-01 at 09 37 18

The strange part is: the issue is fixed by commenting out either of the last two s-exp of the config.

ysftaha commented 2 years ago

Can confirm this still exists in current release

wang1zhen commented 2 years ago

Can confirm this still exists in current release

Did you mean this issue

Hi @dandavison , thanks for your reply.

I have tried (plist-put org-format-latex-options :background "Black"), and here is the value of org-format-latex-options:

(:foreground default :background "Black" :scale 2.0 :html-foreground "Black" :html-background "Transparent" :html-scale 1.0 :matchers
             ("begin" "$1" "$" "$$" "\\(" "\\["))

However, it did not help, the image is still rendered incorrectly

图片

This only happens when the background color of emacs is set to purely black, as tested in protesilaos/modus-themes#36 , I do not know if this piece of information helps?

or the one by @liebkne ?

ysftaha commented 2 years ago

I'm facing this:

Yes, I can confirm.

图片

Actually, the svg file generated is correct,

图片

The issue could be how they are rendered in emacs.

I also noticed that the problem only appears when :background "Black" if :background "#111111" the color is correct.

dandavison commented 2 years ago

Hi all, I'm sorry I don't have a lot of time to investigate right now. Could you clarify whether the issues you're facing occur when using org-mode, or plain .tex files?

wang1zhen commented 2 years ago

It is both true for org-mode and latex-mode.

albertfgu commented 2 years ago

I am also having the same issue with Transparent backgrounds. It also isn't respecting the other settings. Here is a quick demo of what steps I followed.

image
name8102 commented 1 year ago

@liebkne I also face the same issue and solve it by

(setq org-src-block-faces
        '(("latex" (:background "unspecified"))))

It is caused by the theme setting which will make the background colour of source block darker. This setting can make the background color of latex blocks as default.

palisn commented 1 year ago

As far as I can tell the issue lies with how emacs renders svg <rect .../>-tag if it does not have a fill argument. If you set the tex \pagecolor to black dvisvgm renders the bounding box as a <rect .../>-tag without a fill argument. Apparently emacs cannot deal with that.

One possible fix would be to modify the svg generated by dvisvgm and add the argument fill="#000". But since I only need black backgrounds for these svgs to match my emacs background color an easier fix is to just make black a transparent background.

To achieve this I modified the function xenops-math-latex-make-latex-document to not include the line \n\\pagecolor{bg}\n in the case of a black background.

Since the color black is now hard-coded as transparent. I also "fixed" xenops behaviour when the background in org-format-latex-options is "Transparent" by remapping "Transparent" to "black" instead of "white" in the function xenops-math-latex-get-colors.

Both functions can be found in xenops-math-latex.el. Git diff outputs:

diff --git a/lisp/xenops-math-latex.el b/lisp/xenops-math-latex.el
index d998ae6..f7ec296 100644
--- a/lisp/xenops-math-latex.el
+++ b/lisp/xenops-math-latex.el
@@ -112,7 +112,8 @@ containing the foreground and background colors."
                 "\n\\begin{document}\n"
                 "\\definecolor{fg}{rgb}{" fg "}\n"
                 "\\definecolor{bg}{rgb}{" bg "}\n"
-                "\n\\pagecolor{bg}\n"
+                (unless (--every (= (string-to-number it) 0) (split-string bg ","))
+                  "\n\\pagecolor{bg}\n")
                 "\n{\\color{fg}\n"
                 latex
                 "\n}\n"
@@ -261,7 +262,7 @@ format the commands."
          (bg (if (eq bg 'default)
                  (org-latex-color :background)
                (org-latex-color-format
-                (if (string= bg "Transparent") "white" bg)))))
+                (if (string= bg "Transparent") "black" bg)))))
     (list fg bg)))

 (defvar xenops-math-latex-preamble-cache nil

It is more of a workaround than a fix but I hope it can help someone.

Allinaent commented 1 week ago

@liebkne I also face the same issue and solve it by

(setq org-src-block-faces
        '(("latex" (:background "unspecified"))))

It is caused by the theme setting which will make the background colour of source block darker. This setting can make the background color of latex blocks as default.

thanks a lot