Perry Smith <[email protected]> writes:
> TL; DR — How is the color of individual words and phrases specified in
> such a way that the Emacs buffer shows the color as well as the final
> exported documents in HTML and PDF?
(...)
> My main objective is that the documentation in final form be available
> on the web as HTML as well as offline preferable as PDF and EPUB.
(...)
> I would like to be able to have individual words and phrases colored
> such as red and green. It would be nice that the proper color be shown
> in the Emacs buffer as well as in the exported final result.
>
> I just assumed that Org mode would have some special sequence for
> doing this but it seems it does not. (...)

Hi, Perry,

True, Org mode does not have a built-in syntax for coloring text. But it
does afford several possible ways to do it, e.g. with a macro or a
custom link type.

The Org FAQ has example code for a custom 'color:' link type that
exports text in arbitrary colors to HTML and LaTeX. 
https://orgmode.org/worg/org-faq.html#custom-colors-in-export

By adding the example code to your init, you can do e.g.

: The quick [[color:brown][fox]] jumped over the lazy dog

to get the word 'fox' colored brown.

I have just updated the code with a :face parameter to color the link
accordingly in the buffer as well, but the web version of the FAQ isn't
updating at the moment for some reason, so I include the listing here:

#+BEGIN_SRC emacs-lisp
(org-link-set-parameters
 "color"
 :follow
 (lambda (path)
   (message (concat "color "
                    (progn (add-text-properties
                            0 (length path)
                            (list 'face `((t (:foreground ,path))))
                            path) path))))
 :export
 (lambda (path desc format)
   (cond
    ((org-export-derived-backend-p format 'html)
     (format "<span style=\"color:%s;\">%s</span>" path desc))
    ((org-export-derived-backend-p format 'latex)
     (format "{\\color{%s}%s}" path desc))))
 :face
  (lambda (path)
    (list `(:foreground ,path))))
#+END_SRC

Color names differ across Emacs faces, LaTeX, and HTML, but this will
work at least for the basic color names. One way to extend the range of
compatible color names is using the header
'\usepackage[x11names]{xcolor}' in LaTeX.

Discussion has been going on for some time on adding a new extensible
syntax ('special inline blocks') that might be useful for this sort of
thing, but for now custom link types are a convenient way.

Regards,
Christian








Reply via email to