In this HTML source:

<title>mark test</title>
<h1>mark test</h1>
  <code data-trim>
    def add(a,b):
        <mark>return</mark> a+b


<mark> is used to highlight some of the content of the <pre><code> block. The marked code is highlighted by being rendered in a different style.

How can something similar be achieved with org export to HTML in SRC and EXAMPLE blocks?

The following naive attempt does not work:

* Code

#+BEGIN_SRC python
  def fib(n):
      if n < 2:
          <mark>return</mark> 1
      return fib(n-1) + fib(n-2)

  def fib(n):
      if n < 2:
          <mark>return</mark> 1
      return fib(n-1) + fib(n-2)

[Bonus question: is there an HTML-independent way of highlighting parts of SRC and EXAMPLE blocks?]

Edit: as discussed in Picaud Vincent's answer the ref mechanism doesn't do the job, because of the requirements:

  • the highlight must be visible without the reader's intervention,
  • it must be possible to highlight portions of lines, not just entire lines.

2 Answers 2


This is something the export filter functions are particularly suited for, because they kick in after htmlize. See (info "(org) Advanced configuration").

(defun org-html-add-mark-tag (src backend _info)
  (when (org-export-derived-backend-p backend 'html)
    (replace-regexp-in-string "{{{\\([^\n]+\\)}}}" "<mark>\\1</mark>" src)))
(add-to-list 'org-export-filter-src-block-functions 'org-html-add-mark-tag)
def fib(n):
    if n < 2:
        {{{return}}} 1 
    return fib(n-1) + fib(n-2)
  • Very nice. I wouldn't be surprised if there are other mechanisms, and I wouldn't mind hearing about them, so I'll hold off accepting this answer for a little while. But this certainly points to a way of creating a very decent solution to my problem.
    – jacg
    Commented Nov 1, 2017 at 8:48
  • Might you know how to make the idea work with org reveal? org-reveal-src-block seems to throw some spanner into the works for source blocks, and it's not clear to me how to work around it.
    – jacg
    Commented Nov 1, 2017 at 14:35
  • ... this seems to happen when use-highlight in non-nil (which seems to be a consequence of #+REVEAL_PLUGINS: (highlight) which I would like to use). The relevant branch appears to neutralize #'org-html-htmlize-region-for-paste by temporarily replacing it with #'buffer-substring ... but It's not clear to me where to go from there.
    – jacg
    Commented Nov 1, 2017 at 15:08
  • I'm not familiar with org-reveal, sorry.
    – mutbuerger
    Commented Nov 3, 2017 at 12:27

AFAIK you can use http://orgmode.org/manual/Literal-examples.html for code block, but this is at the best only a partial answer to your question. I am not aware of such equivalent for BEGIN_EXAMPLE ... END_EXAMPLE block.

Code line highlight example:

If you html-export this org mode example, the return 1 line will be automatically highlighted if your cursor is over the "use the link". Moreover, you will jump to the code line if you click on it.

#+TITLE: Highlight

#+BEGIN_SRC python -n -r
  def fib(n):
      if n < 2:
          return 1                                                (ref:my link)
      return fib(n-1) + fib(n-2)

Now you can [[(my link)][use the link]].
  • Apologies, I should have mentioned up-front why this doesn't satisfy my requirements: a) I need the highlight without any intervention on the reader's behalf, while this requires hovering over the relevant line; b) I need to be able to highlight portions of a line while this highlights entire lines.
    – jacg
    Commented Oct 31, 2017 at 8:08
  • @jacg no needs for apologies, writing my answer I feared that your real requirements were those you described in your comment. Unfortunately, I do not know how to do that. Maybe an alternative idea is to build your code incrementally orgmode.org/manual/noweb_002dref.html#noweb_002dref . This let you the opportunity to detail important parts. Commented Oct 31, 2017 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.