2

Looking for way to export a document with table of contents anchor links only (without the H2 that says "Table of Contents").

Basically would like to change from this:

Table of Contents

  • Heading
  • Heading
  • Heading

To this:

  • Heading
  • Heading
  • Heading

In the final export.

  • It looks like the function org-html-toc in ox-html.el always adds a "Table of Contents" string to the output. It uses (org-html--translate "Table of Contents" info), so you could invent a fake language, say en-no-toc, so the translation of "Table of Contents" is the empty string. That would erase the text but leave an empty heading in the HTML. If you don't like that, you can redefine this function to omit it. Or you could use CSS to hide the heading (hide the element with id="table-of-contents" and override this in the child with `id="text-table-of-contents", so it shows). – Omar Jun 26 at 13:24
2

The following lisp code introduces the new export keyword TOC_NO_HEADING. If it has the value t or yes the heading of the table of contents is surpressed.

(require 'ox-html)

;; Adding keyword TOC_NO_HEADING to html export keywords:
(push '(:html-toc-no-heading "TOC_NO_HEADING" nil nil t) (org-export-backend-options (org-export-get-backend 'html)))

(defun my-org-html-toc-no-heading (args)
  "Avoid toc heading in html export if the keyword TOC_HO_HEADING is t or yes.
Works as a :filter-args advice for `org-html-toc' with argument list ARGS."
  (let* ((depth (nth 0 args))
     (info (nth 1 args))
     (scope (nth 2 args)))
    (when (and (assoc-string (plist-get info :html-toc-no-heading) '(t yes) t)
           (null scope))
      (setq scope (plist-get info :parse-tree)))
    (list depth info scope)))

(advice-add 'org-html-toc :filter-args #'my-org-html-toc-no-heading)

Test:

  • emacs-version 26.1
  • org-version 9.1.13
  • Test file:
#+TOC_NO_HEADING: t
* Test header 1
** Test header 1.1
Some text below test header 1.1.
** Test header 1.2
Some text below test header 1.2.
* Header 2
Some text below header 2.
  • Very nice! I didn't read the source code of org-html-toc very carefully and didn't notice that when the scope parameter is non-nil you don't get the heading. This is much better than the approaches I outlined in my comment to the question. – Omar Jun 27 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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