emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Tip] Export a bibliography to HTML with bibLaTeX and make4ht


From: Juan Manuel Macías
Subject: [Tip] Export a bibliography to HTML with bibLaTeX and make4ht
Date: Sat, 23 Jan 2021 12:03:14 +0100

Hi,

When I export to LaTeX an Org document that contains a bibliography, I
use bibLaTeX with a very custom style (i.e. quite a few lines of code
related to bibLaTeX in the preamble). I wanted to apply all that
bibLaTeX setting and styles when exporting to HTML too, so I came up
with this method, using make4ht. I share it here, in case it is useful
to someone.

The idea is to compile with make4ht (see:
https://www.ctan.org/pkg/make4ht) a simple file with *only* the
bibliography, and "embed" the HTML output in the Org document. You need
to create in the working directory a tex file, which will serve as a
minimal preamble and which also includes all code related to bibLaTeX.
We can name it preamble.tex, and it would start like this:

#+begin_src latex
\documentclass{article}
\usepackage{fontspec}
\usepackage[<whatever-language>]{babel}
\usepackage[backend=biber,style=authortitle,dashed=true,sorting=nyt]{biblatex}
%% more code related to bibLaTeX...
#+end_src

We also need a small lua file that will control the make4ht compilation.
If we run make4ht in draft mode it will not call Biber. This file can be
named build.lua:

#+begin_src lua
if mode=="draft" then
Make:htlatex {}
else
Make:htlatex {}
Make:biber {}
Make:htlatex {}
end
#+end_src

And finally, this function is defined in Elisp, which takes two
arguments: the preamble-file and the *.bib file to generate the list of
references. The optional draft argument is for make4ht to run in draft
mode (that is, so you don't rebuild the bibliography). In the end Pandoc
is executed with shell output to simplify the resulting HTML:

#+begin_src emacs-lisp
  (defun my-biblio-html (preamble bib &optional draft)
    (when (org-export-derived-backend-p org-export-current-backend 'html)
      (let ((file (file-name-sans-extension bib))
            (d (if draft
                   "-m draft "
                 "")))
        (shell-command (concat
                        "echo \"\\input{"
                        preamble
                        "}"
                        "\\addbibresource{"
                        bib
                        "}"
                        "\\begin{document}
    \\nocite{*}
    \\printbibliography[heading=none]
    \\end{document}\" > "
                        file "-bib.tex"))
        (shell-command-to-string (concat "make4ht -ule build.lua "
                                         d
                                         file
                                         "-bib.tex > /dev/null && "
                                         "pandoc -f html -t html "
                                         file
                                         "-bib.html")))))
#+end_src

An example:

#+begin_src org
  ,#+HTML_HEAD: <style> dd { text-indent: -2em; margin-left: 2em; } </style>
  ,#+HTML_HEAD: <style> .rm-lmri-12{ font-style:italic;} </style>
  ,* References
  ,#+begin_src emacs-lisp :exports results :results html
  (my-biblio-html "preamble.tex" "file.bib")
  ,#+end_src
#+end_src

As you can see, the method is somewhat tricky, but it works well, for now. I 
hope that
be useful!

Regards,

Juan Manuel

reply via email to

[Prev in Thread] Current Thread [Next in Thread]