Set the variable bibtex-completion-bibliography used by helm-bibtex to the dynamical generated bib files list corresponding to the current master tex file.
Closed this issue · 14 comments
Hi here,
I have the following use scenario:
- Use helm-bibtex to manage and insert bib entries.
- Use auctex to do the document creation.
- I save the bib files used for each master TeX file in a subfolder
located under the same directory as it.
I want to set the bibtex-completion-bibliography
variable of helm-bibtex to the dynamical generated bib files list corresponding to the current master TeX file.
The example setting of this variable is as follows:
(setq bibtex-completion-bibliography (directory-files-recursively
(concat (getenv "HOME")
"/texmf/bibtex/bib/local") "^[A-Za-z].+.bib$"))
But the above setup uses a fixed directory, so it doesn't meet the requirements here. Any suggestions for achieving the goal will be appreciated.
Regards,
Zhao
Please try the command helm-bibtex-with-local-bibliography
.
If no local bibliography can be found, this will fall back to the global one (default).
I have a bib file located in the same director as the master TeX file:
werner@X10DAi:~/Desktop/iucr/latex$ ls orbnpg.bib
orbnpg.bib
And I have set the following in my master TeX file:
\bibliographystyle{iucr}
% To submit a manuscript, simply provide the corresponding bbl file.
\bibliography{orbnpg}
The helm-bibtex configuration in Emacs init.el is as follows:
(use-package helm-bibtex
:init
(setq
;; bibtex-completion-bibliography (directory-files-recursively
;; (concat (getenv "HOME") "/texmf/bibtex/bib/local") "^[A-Za-z].+.bib$")
;;bibtex-completion-library-path (concat (getenv "HOME") "/pdf")
;;bibtex-completion-notes-path (concat (getenv "HOME") "/notes")
bibtex-completion-pdf-field "File"
;;https://github.com/tmalsburg/helm-bibtex#insert-latex-cite-commands
bibtex-completion-cite-prompt-for-optional-arguments nil)
:bind
(("<menu>" . helm-command-prefix)
:map helm-command-map
("b" . helm-bibtex)
("B" . helm-bibtex-with-local-bibliography)
("n" . helm-bibtex-with-notes)
("<menu>" . helm-resume))
:config
(require 'helm-config)
;;https://github.com/tmalsburg/helm-bibtex#application-used-for-opening-pdfs
;;https://github.com/tmalsburg/helm-bibtex/issues/386
(defun bibtex-completion-open-pdf-external (keys &optional fallback-action)
(let ((bibtex-completion-pdf-open-function
(lambda (fpath) (start-process "evince" "*helm-bibtex-evince*" "/usr/bin/evince" fpath))))
(bibtex-completion-open-pdf keys fallback-action)))
(helm-bibtex-helmify-action bibtex-completion-open-pdf-external helm-bibtex-open-pdf-external)
(helm-add-action-to-source
'helm-bibtex
'(("P" helm-bibtex-open-pdf-external "Open PDF file in external viewer (if present)")))
;;https://github.com/tmalsburg/helm-bibtex#browser-used-for-opening-urls-and-dois
(setq bibtex-completion-browser-function
(lambda (url _) (start-process "firefox" "*firefox*" "firefox" url)))
)
But when I hit "<menu>-B"
, the local bibliography still cannot be found:
Can you please execute the function bibtex-completion-find-local-bibliography
in the tex buffer and report the result?
Hm, no idea why it doesn't work the first time.
Sidenote: I see that the display of entries is messed up. Apparently due to the entry numbering. I wasn't aware that helm could be configured to number the entries. Could you please let know which option that is? Perhaps I can find a work-around. Thank you.
I see that the display of entries is messed up.
What do you mean by saying messed up
?
Apparently due to the entry numbering.
I have a lot of configurations in init.el
, can you give me some clues to locate the corresponding configuration?
Hm, no idea why it doesn't work the first time.
The reason is explained here, and the following modifications should be made
(use-package helm-bibtex
:init
(setq
bibtex-completion-pdf-field "File"
;;https://github.com/tmalsburg/helm-bibtex#insert-latex-cite-commands
bibtex-completion-cite-prompt-for-optional-arguments nil)
:bind-keymap
("<menu>" . helm-command-prefix)
:bind
(
:map helm-command-map
("b" . helm-bibtex)
("B" . helm-bibtex-with-local-bibliography)
("n" . helm-bibtex-with-notes)
("<menu>" . helm-resume)
)
:config
(require 'helm-config)
;;https://github.com/tmalsburg/helm-bibtex#application-used-for-opening-pdfs
;;https://github.com/tmalsburg/helm-bibtex/issues/386
(defun bibtex-completion-open-pdf-external (keys &optional fallback-action)
(let ((bibtex-completion-pdf-open-function
(lambda (fpath) (start-process "evince" "*helm-bibtex-evince*"
"/usr/bin/evince" fpath))))
(bibtex-completion-open-pdf keys fallback-action)))
(helm-bibtex-helmify-action bibtex-completion-open-pdf-external
helm-bibtex-open-pdf-external)
(helm-add-action-to-source
'helm-bibtex
'(("P" helm-bibtex-open-pdf-external "Open PDF file in external
viewer (if present)")))
;;https://github.com/tmalsburg/helm-bibtex#browser-used-for-opening-urls-and-dois
(setq bibtex-completion-browser-function
(lambda (url _) (start-process "firefox" "*firefox*" "firefox" url)))
)
What do you mean by saying messed up?
In your screenshot, each entry occupies two lines, instead of just one. This is because some extra space is used by the numbering. This numbering is not added by helm-bibtex (I never saw it). I assumed that the entry numbering is a helm feature that you activated.
I have noticed that the helm-bibtex-with-local-bibliography
command only try to find the bib file on the top level directory of the master TeX file and doesn't do a recursive search into subdirectories.
Say, if the bib file is set to \bibliography{./refs/orbnpg}
, in this case, helm-bibtex-with-local-bibliography
can not find this bib file at all.
Detection of the .bib
files associated with a .tex
file is handled by the reftex package. In my setup, it does succeed at finding .bib
files in subdirectories. Example below:
\documentclass[]{article}
\usepackage[backend=biber]{biblatex}
\addbibresource{dir1/test.bib}
\title{The title}
\oneauthor{Myself}
\oneaffiliation{My Affiliation}
\abstract{The abstract}
\begin{document}
The document:
\printbibliography[]
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
- This method works.
- In order to print out the bibliography list for testing, the
\nocite{*}
command should be used as commented here and use thebiber
command to generate the bibliographies:
\documentclass[]{article}
% for working with biblatex
\usepackage[backend=biber]{biblatex}
\addbibresource{references/orbnpg.bib}
\nocite{*}
\begin{document}
The document.
% for testing biblatex
\printbibliography[]
% for testing bibtex
%\cite{gahlerCaratInterfaceInterfaceCARAT2022}
% for working with bibtex
%\bibliographystyle{plain}
%\bibliography{references/orbnpg}
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
- I'm not familiar with biblatex, so I adapt the example code to use BibTeX instead of biblatex, and it also works:
\documentclass[]{article}
% for working with biblatex
% \usepackage[backend=biber]{biblatex}
% \addbibresource{references/orbnpg.bib}
\begin{document}
The document.
% for testing biblatex
% \printbibliography[]
% for testing bibtex
\cite{gahlerCaratInterfaceInterfaceCARAT2022}
% for working with bibtex
\bibliographystyle{plain}
\bibliography{references/orbnpg}
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
Sidenote: Maybe my problem is caused by not enabling the reftex package, as you have pointed out. I now have enabled it according to the guide here:
(require 'reftex) ; Loading RefTeX
(add-hook 'LaTeX-mode-hook 'turn-on-reftex) ; with AUCTeX LaTeX mode
Sidenote: Maybe my problem is caused by not enabling the reftex package,
Hm, helm-bibtex should actually take care of loading reftext. But anyway, good that you got it to work.
You have a typo here: reftext
should be reftex
.