Michiel Borkent
Michiel Borkent

Reputation: 34850

org-mode: counter for items, visible in export

I'm writing a document in org-mode. Is it possible to use a counter for exercises that are spread across the document instead of numbering them myself? I am exporting this to HTML and PDF (via LaTeX). Example:

* First chapter
Blabla.

Exercise 1.

* Second chapter.

Blabla

Exercise 2.

* Third chapter.

Exercise 3.

Exercise 4.

Upvotes: 4

Views: 1198

Answers (1)

Michiel Borkent
Michiel Borkent

Reputation: 34850

I solved this using dynamic blocks in org-mode.

In my init.el I defined the following:

(setf exercise-counter 0)
(defun org-dblock-write:reset-exercise-counter (params)
  (setf exercise-counter 0))
(defun org-dblock-write:exercise (params)
  (incf exercise-counter)
  (insert (concat "Exercise " (int-to-string exercise-counter) ".")))

At the top of my document I reset the counter:

#+BEGIN: reset-counter

#+END

Spread across the document I can now put this:

#+BEGIN: exercise
#+END
Blablabla.

#+BEGIN: exercise
#+END
Blablabla.

After calling org-update-all-dblocks the right numbers will be inserted.

Text before

After

Open for suggestions if anything can be improved.

How do I call org-update-all-dblocks automatically when exporting? ANSWER: like this: (add-hook 'org-export-first-hook 'org-update-all-dblocks)

Upvotes: 5

Related Questions