11

I want bibtex-mode to align and properly indent fields. I copied a bibtex entry for the following article into Emacs: (Article source here)

But, with a simple paste, the alignment is lost in Emacs and the indentation is messed up:

The TAB key doesn't work to align or indent the entry. How to solve this problem?

3
  • 1
    Unrelated to your question, consider using bibslurp package, it allows you to retriev BibTeX entries from The SAO/NASA Astrophysics Data System within Emacs itself.
    – giordano
    Commented Jul 14, 2017 at 13:32
  • 1
    I can't reproduce that behaviour. What happens if you start Emacs with the -Q switch?
    – Nova
    Commented Jul 15, 2017 at 15:01
  • I get the same behaviour when starting with a -Q switch
    – Viesturs
    Commented Jul 15, 2017 at 17:57

4 Answers 4

17

When the cursor is somewhere in the entry, run the command bibtex-fill-entry (bound to C-c C-q), which will align the fields. You may also want to set variable bibtex-align-at-equal-sign to a non nil value to change the details of alignment.

3
  • Can bibtex-fill-entry be applied after each save in bibtex-mode?
    – alper
    Commented Oct 26, 2022 at 10:42
  • @alper: that's an unrelated question, but you can look at before-save-hook and after-save-hook.
    – Dan
    Commented Oct 27, 2022 at 16:16
  • I have done it but bibtex-fill-entry only work at the block where cursor is at, not on the entire buffer
    – alper
    Commented Oct 27, 2022 at 18:29
3

I had a similar issue, when using the smartparens package in bibtex-mode, where shameful amounts of spaces where inserted. For some reason unknown, bibtex-mode sets the fill-prefix variable to a string containing 18 spaces. (setq fill-prefix nil) in the bibtex-mode-hook fixed the issue in my case.

3

As mentioned by @JonatanLindén, fill-prefix is set to a string containing 18 spaces. This is because bibtex-clean-entry is using fill-prefix to align continuing text after equal sign. Setting fill-prefix to "" can solve the indentation issue. But to have better alignment when formating entry, you can advice bibtex-clean-entry to temporarily set fill-prefix.

(defun bibtex-mode-setup ()
  (setq-local fill-prefix ""))
(add-hook 'bibtex-mode-hook #'bibtex-mode-setup)

(defun bibtex-reset-fill-prefix (orig-func &rest args)
  (let ((fill-prefix (make-string (1+ bibtex-text-indentation) ? )))
    (apply orig-func args)))
(advice-add 'bibtex-clean-entry :around #'bibtex-reset-fill-prefix)
1

Usually, you would use the align-regexp command (or the align command if bibtex-mode supported it). But that won't help you with the broken indentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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