Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the variable bibtex-completion-bibliography used by helm-bibtex to the dynamical generated bib files list corresponding to the current master tex file. #429

Closed
hongyi-zhao opened this issue May 4, 2023 · 14 comments

Comments

@hongyi-zhao
Copy link

hongyi-zhao commented May 4, 2023

Hi here,

I have the following use scenario:

  1. Use helm-bibtex to manage and insert bib entries.
  2. Use auctex to do the document creation.
  3. 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

@tmalsburg
Copy link
Owner

Please try the command helm-bibtex-with-local-bibliography.

@tmalsburg
Copy link
Owner

If no local bibliography can be found, this will fall back to the global one (default).

@hongyi-zhao
Copy link
Author

hongyi-zhao commented May 4, 2023

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:

image

@tmalsburg
Copy link
Owner

Can you please execute the function bibtex-completion-find-local-bibliography in the tex buffer and report the result?

@hongyi-zhao
Copy link
Author

hongyi-zhao commented May 4, 2023

Now, it works. But I still encounter the following strange problem.

When I first time press <menu>, the following error will be triggered:

image

When I hit "<menu>-B" the second time, it will work:

image

@tmalsburg
Copy link
Owner

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.

@hongyi-zhao
Copy link
Author

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?

@hongyi-zhao
Copy link
Author

hongyi-zhao commented May 5, 2023

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

  )

@tmalsburg
Copy link
Owner

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.

@hongyi-zhao
Copy link
Author

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.

@tmalsburg
Copy link
Owner

tmalsburg commented May 8, 2023

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:

@hongyi-zhao
Copy link
Author

hongyi-zhao commented May 8, 2023

  1. This method works.
  2. In order to print out the bibliography list for testing, the \nocite{*} command should be used as commented here and use the biber 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:
  1. 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

@tmalsburg
Copy link
Owner

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.

@hongyi-zhao
Copy link
Author

You have a typo here: reftext should be reftex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants