9

When using Org-mode and its LaTeX export BibTeX or Biblatex is often used to handle references. In that case the LaTeX command \printbibliography is often included in the org file. \printbibliography is placed in the org file where LaTeX is supposed to write out the reference list. What \printbibliography does is to insert a LaTeX header along with the reference list. In most cases \printbibliography is placed at the end of the org file simply because in most documents the reference list is to be placed last. This means that \printbibliography will be included under the last heading in the org file, e.g.

* Heading

  \printbibliography

It also means that when that heading is folded the \printbibliography will be swallowed:

* Heading...

But this goes against the meaning of \printbibliography because it includes its own heading in the output. Also, it will be confusing when \printbibliography is swallowed and a new heading is placed after it because then the reference list will no longer appear last in the document.

How can I make it so that \printbibliography is not swallowed by sections in Org-mode? A bonus question: how can I make it so that Org-mode does not create headings after \printbibliography unless C-Ret is pressed when the cursor is after it?

In searching for a solution to this problem I found http://comments.gmane.org/gmane.emacs.orgmode/49545.

N.N.
  • 8,336
  • 12
  • 54
  • 94

5 Answers5

3

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.

N.N.
  • 8,336
  • 12
  • 54
  • 94
  • Note that if section numbering is on the References heading will have a number as well.. I wrote an [answer](http://stackoverflow.com/a/27165105/1346426) that handles also this case. – david-hoze Nov 27 '14 at 07:23
1

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).

Jonathan Leech-Pepin
  • 7,684
  • 2
  • 29
  • 45
  • 1
    With this, as you note, `* {{{printbiblio}}}` becomes `\section{}` and `*` becomes `\section{}`. LaTeX treats `\section` with an empty argument as a section, and thus your test case produces one section without a name before `\printbibliography` and one section without a name after it. What I would like is that `\printbibliography` is not swallowed by an org heading and that org does not export so that it places it under a heading because in LaTeX it will produce its own heading. – N.N. Feb 04 '12 at 10:13
  • This gave me an idea for @N.N.'s question about inline comments: http://stackoverflow.com/questions/9212737/how-to-make-inline-comments-in-org-mode/9307681#9307681 I hope you don't mind--I have voted up this answer. – Ivan Andrus Feb 16 '12 at 08:15
  • Not a problem, I hadn't thought of using it that way for that other question. – Jonathan Leech-Pepin Feb 16 '12 at 18:58
  • Note that if section numbering is on the empty heading will turn into a section title with just a number.. I wrote an [answer](http://stackoverflow.com/a/27165105/1346426) that deals with that – david-hoze Nov 27 '14 at 07:22
1

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))))
N.N.
  • 8,336
  • 12
  • 54
  • 94
Ivan Andrus
  • 5,221
  • 24
  • 31
  • This seems to work for me too for tab and shift-tab. It does, as you imply, not work with sparse trees. Can it be made to work with sparse trees? – N.N. Feb 16 '12 at 09:36
  • Yes, it looks like adding it to `org-occur-hook` does the trick. – Ivan Andrus Feb 16 '12 at 10:33
  • As you can read in [the biblatex manual](http://mirror.ctan.org/macros/latex/contrib/biblatex/doc/biblatex.pdf) `\printbibliography` takes arguments in the form `\printbibliography[key=value, ...]` and, thus, to make your answer even better you might want to edit the `org-footer-regexp` to allow `\printbibliography` to be followed by `[`, any characters, `]`. – N.N. Feb 16 '12 at 10:48
  • When checking out the options for `\printbibliography` I found another solution, or rather a simple but elegant workaround, to the problem that motivates this question. I documented it as [an answer](http://stackoverflow.com/a/9310026/789593). – N.N. Feb 16 '12 at 10:57
0

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.

david-hoze
  • 1,045
  • 15
  • 31
0
* 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.

X.Arthur
  • 277
  • 2
  • 13