Avoid \\printbibliography being swallowed by Org-mode headings

梦想的初衷 提交于 2019-12-03 03:04:25
Ivan Andrus

The following is lightly tested but works for me using tab and shift-tab to hide and display things. Those are the only hiding and showing commands that I use, so if you use other commands they may have to be advised or fixed in some other way.

You can of course change org-footer-regexp to anything you want. I was hoping to not have to use any advice, but without advising org-end-of-subtree the last heading never cycles with tab because it thinks it's not hidden, so it hides it and then org-cycle-hook unhides it. It calls org-end-of-subtree before running org-pre-cycle-hook so that's not an option either.

(defvar org-footer-regexp "^\\\\printbibliography\\[.*\\]$"
  "Regexp to match the whole line of the first line of the footer which should always be shown.")

(defun show-org-footer (&rest ignore)
  (save-excursion
    (goto-char (point-max))
    (when (re-search-backward org-footer-regexp nil t)
      (outline-flag-region (1- (point)) (point-max) nil))))

(add-hook 'org-cycle-hook 'show-org-footer)
(add-hook 'org-occur-hook 'show-org-footer)

(defadvice org-end-of-subtree (after always-show-org-footer
                                     ()
                                     activate)
  (when (>= (point) (1- (point-max)))
    (re-search-backward org-footer-regexp nil t)
    (setq ad-return-value (point))))

A workaround for this problem is to make \printbibliography not return a LaTeX heading so that it can appropriately be placed under an Org-mode heading.

With biblatex this can be done by supplying \printbibliography with the option heading=none and placing it under an appropriate heading. Here is an example:

* Heading

* References

  \printbibliography[heading=none]

This way references can be kept in a heading of its own and \printbibliography being swallowed by a heading is not a problem because it is being swallowed by its own heading.

One solution would be the following:

#+macro: printbiblio        (add extra spaces here, but cannot add comment)

* Test 2
  This is a test

* {{{printbiblio}}}
  Test text
  \printbibliography
* 
  asdf

Like this you end up with a blank heading at the bottom of the document. The macro expands to a blank block of text so you end up with

\section{Test 2}
\label{sec-1}

This is a test
\section{}

Test text
\printbibliography
\section{}

asdf

This also ensures you cannot accidentally add headlines after your bibliography, since it is it's own (empty) headline. It might be (seems to be actually) included in the table of contents, which is unfortunate but I would suspect the solution would be at worst run a post-export to remove the empty headline from the file (or manually do so before converting to PDF).

Another solution would be to put the bibliography under a heading named "References" like so:

* Heading
Some text
* References
\printbibliography

and remove the \section{References} from the resulting latex file by adding this to your emacs init file

(defun org-export-latex-remove-references-heading (contents backend info)
    (if (not (eq backend 'latex))
        contents
      (replace-regexp-in-string "\\\\section\\*?{References}\\s-*\\\\label{.*?}" "" contents)
      ))

(add-hook 'org-export-filter-final-output-functions 'org-export-latex-remove-references-heading)

Note that this assumes you only have one heading that's named "References", as it replaces all occurrences of it. It also assumes the sections are in this format:

\section{References}
\label{<any_string>}
\printbibliography

For other formats you need to change the regular expression in the org-export-latex-remove-references-heading function.

* References
  :PROPERTIES:
  :UNNUMBERED: t
  :END:

   \printbibliography[heading=none]

There is an easier way to solve this. Just add an "unnumbered" properties to the heading and it will be exported without any numbering.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!