I export org-mode files quite often, and almost all of them have code in them. When I do export code, it ends up coloring the text depending on my theme, however, I switch between a dark theme and a light theme regularly. In the case of the light theme, the <code>
blocks need to have a light background, and for a dark theme they need to have a dark background (otherwise I end up with light yellow text on a greyish background and it's unreadable).
I have to add this line for dark backgrounds:
#+HTML_HEAD: <style>pre.src {background-color: #303030; color: #e5e5e5;}</style>
And then remember to remove it any time I switch to a light colored background
Is there a way I can automatically detect the background color of the current theme at export time and use it in the exported HTML's CSS?
EDIT
I'm going with Jordon's answer (thanks Jordon, you get the karma points!), however I wanted to post the solution I ended up adding to my code, since it was a slightly modified version of his answer:
(defun my/org-inline-css-hook (exporter)
"Insert custom inline css to automatically set the
background of code to whatever theme I'm using's background"
(when (eq exporter 'html)
(let* ((my-pre-bg (face-background 'default))
(my-pre-fg (face-foreground 'default)))
(setq
org-html-head-extra
(concat
org-html-head-extra
(format "<style type=\"text/css\">\n pre.src {background-color: %s; color: %s;}</style>\n"
my-pre-bg my-pre-fg))))))
(add-hook 'org-export-before-processing-hook 'my/org-inline-css-hook)
This sets not only the background color, but also the foreground color. It also appends the line to the existing org-html-head-extra
setting so other HTML isn't overwritten accidentally. I've tested and this works great for me!
disable-theme
), I do not want to have to restart Emacs with a separate theme just to export HTML, which I do many times a day.org-html-head-extra
gets appended to, leading to unbounded growth, though functionality of the style won't be impaired.