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

Path to bib file in a command #1745

Closed
p135246 opened this issue Jul 11, 2020 · 11 comments
Closed

Path to bib file in a command #1745

p135246 opened this issue Jul 11, 2020 · 11 comments

Comments

@p135246
Copy link

p135246 commented Jul 11, 2020

Description

I have a document structure with the main file 'text.tex' and several subfiles. The preamble of 'text.tex' contains the following definitions:

\providecommand{\MainFolder}{.}
%	The directory of the main file text.tex
\providecommand{\MyPackagesFolder}{\MainFolder/latex-packages}
%	The directory with LateX packages
\providecommand{\SubfilesFolder}{\MainFolder/subfiles}
%	The directory with subfiles
\providecommand{\GraphicsFolder}{\MainFolder/graphics}
%	The directory with graphics
\providecommand{\BibliographyFile}{\MainFolder/bibliography/bibliography.bib}
%	The bibliography file

If I want to include bibliography, I have the following command either in 'text.tex' or in one of the subfiles:

\addbibresource{\BibliographyFile}

In this setting, VimTex does not detect the bibliography file 'bibliography.bib'.

Question

Would it be possible for VimTex to recursively unfold '\BibliographyFile' to the actual path to 'bibliography.bib' or do you find this sort of abstraction a bad practice in general? Of course, if I replace '\BibliographyFile' with the actual path to 'bibliograph.bib', then VimTex detects it. Thanks!

@lervag
Copy link
Owner

lervag commented Jul 11, 2020

In this setting, VimTex does not detect the bibliography file 'bibliography.bib'.

Could you explain exactly what you mean with "does not detect", here? Are you referring to the ToC? Or other features?

Would it be possible for VimTex to recursively unfold '\BibliographyFile' to the actual path to 'bibliography.bib' [?]

Well, everything is possible. But this does present a new and quite hard challenge, and I'm not sure I'm up to it. So far, vimtex has not added any support for actually parsing the TeX macros, and it seems a very complicated problem (indeed, this is what TeX itself is supposed to do, right?!).

However, I'm curious if it is possible to circumvent it by reading the aux file or something similar, in which perhaps the parsing is already done. Could you provide a minimal example of this on which I could try things out?

... or do you find this sort of abstraction a bad practice in general?

Yes, I do find this sort of abstraction somewhat a bad practice. But that's my personal opinion, and does not necessarily represent a good opinion. The reason I think it is bad practice is because you are adding complexity/abstraction for no apparant reason. What are you really trying to accomplish?

When you do things like this, you are making the life much more difficult for tools that try to help you (e.g. vimtex). Sometimes, things like this is necessary e.g. for parametrized, automatic documents. Or perhaps for documents that include a common part, or whatnot. Still, I've actually never used methods like this for anything, and I don't think I'm "missing out".

@p135246
Copy link
Author

p135246 commented Jul 11, 2020

ToC and auto-completion.

I find it handy to have '\GraphicsFolder' and '\MyPackagesFolder' globally defined when working on subfiles. As for '\BibliographyFile', I use to have one subfile 'notes.tex' which uses a bib file generated by Mendeley containing all my references whereas the main document uses a local polished bibliography. I achieve this by redefining '\BibliographyFile'. It is probably an obscure solution which can be imitated in other ways but it somehow evolved spontaneously.

Below is a test document:
test.tar.gz

If you use the aux file then it will work first after a compilation ... I understand if you don't want to spend time on this and will close the ticket if you wish. Thanks for the great work anyway!

@p135246
Copy link
Author

p135246 commented Jul 11, 2020

I don't think that making it work in my situation would require more than writing down a correct regex and applying simple recursion. If further expansion requires the TeX engine, i.e., if a command is not defined in the current files, then an error message could be displayed in ToC instead.

@lervag
Copy link
Owner

lervag commented Jul 11, 2020

I find it handy to have '\GraphicsFolder' and '\MyPackagesFolder' globally defined when working on subfiles. As for '\BibliographyFile', I use to have one subfile 'notes.tex' which uses a bib file generated by Mendeley containing all my references whereas the main document uses a local polished bibliography. I achieve this by redefining '\BibliographyFile'. It is probably an obscure solution which can be imitated in other ways but it somehow evolved spontaneously.

Ok, I see. I'm not sure I will be able to change vimtex to support your (at the least semi obscure) solutions, but let's see.

Below is a test document:
test.tar.gz

If you use the aux file then it will work first after a compilation ... I understand if you don't want to spend time on this and will close the ticket if you wish. Thanks for the great work anyway!

Happy you like it! I will check if it is possible to parse the aux file for bib locations. If so, then I think that may be a possible improvement. If it is not possible, then I am afraid I don't really have the capacity to address such a complex task as of now. Sorry.

I don't think that making it work in my situation would require more than writing down a correct regex and applying simple recursion. If further expansion requires the TeX engine, i.e., if a command is not defined in the current files, then an error message could be displayed in ToC instead.

But I don't want to make things work in just "your situation", I want to improve vimtex in a more general manner. I try to avoid "hacks" that may lead to unexpected problems. Thus, if I were to look into a proper solution, it would include:

  • parsing of \newcommand, \providecommand, \let, etc. to capture all local macros and their expansions (here it would be possible to e.g. ignore all "non constant" macros that take an argument)
  • allow recursive expansion of macros

Again, I'm not saying this is impossible, but I think it is complicated and it will need quite some time to implement properly. I will be very glad to consider PRs for this (and will gladly help in reviewing and so on).


Looking at your test files: Again I don't quite see the benefit of using the custom path macros instead of just typing them out. Can you not simply add a second \addbibresource line for the second bibliography file in your notes.tex?

@p135246
Copy link
Author

p135246 commented Jul 11, 2020

Yes, it seems to behave in the same way.

@lervag
Copy link
Owner

lervag commented Jul 11, 2020

Yes, it seems to behave in the same way.

I've responded in length, so it is unclear what you are now replying to. What is behaving in the same way?

@p135246
Copy link
Author

p135246 commented Jul 11, 2020

Sorry, It is the very last question that I don't have to use my definitions and can just write \adddbibresource{...} instead. I find it interesting and maybe more reasonable if you manage to gather the data from the aux file. Otherwise, I can just change my template and stop using \BibliographyFile as it is obviously unnecessary.

@lervag
Copy link
Owner

lervag commented Jul 12, 2020

Ok, it seems it is possible to parse auxiliary files for the bib files, and I believe it will be more efficient than the current method. However, it will only be possible as a solution for completion, not for the ToC, since the ToC parses the latex files.

I will keep the issue open until I've looked into this. It might be better to parse e.g. the .log file or the .fdb_latexmk file for a list of sourced bib files.

@lervag
Copy link
Owner

lervag commented Jul 12, 2020

Ok, I've now implemented a minor change where the .blg file is used to parse the included bib files when possible. This enables cite completion in your example and thus partially solves your original request. That's as far as I go in this case, but I do think it is a minor improvement, as it should at least somewhat the minor lag for cite completion as long as the blg file is present.

@lervag lervag closed this as completed Jul 12, 2020
@RiMillo
Copy link
Contributor

RiMillo commented Aug 2, 2020

Hi,
I don't know if it can be done something about it, but I've noticed that since commit #b7fbff1 (I git bisected and it seems that this is the origin of the problem) when using YouCompleteMe g:ycm_semantic_triggers.tex = g:vimtex#re#youcompleteme an extra \cite{}-suggestion is listed whenever a *-blx.bib file is present. I attach a screen-shot
bug_vimtex_YCM

My chain of work is:

  1. Write main file: No extra suggestion at this stage
  2. Compile: pdflatex minimal.tex
  3. Compile bibliography: bibtex minimal.aux
  4. Edit main: Now the extra suggestion appears
    Optional:
  5. Clear generated files
  6. Edit main: No extra suggestion

Thank you in advance and for the awesome plugin !

EDIT: The problems persists even without YCM when using

minimal.tex: (notice the loading of biblatex with bibtex as backend)

\documentclass{article}
\usepackage[backend=bibtex]{biblatex}
\addbibresource{minimal.bib}

\begin{document}

This is a test document.

Cite one time to allow to cite \texttt{main-blx.bib} \cite{knuth1981}.

\printbibliography
\end{document}

minimal.bib

@book { knuth1981,
        author = "Donald E. Knuth",
        title = "Seminumerical Algorithms",
        publisher = "Addison-Wesley",
        year = "1981" }

minimal.vimrc (using Vundle to load the plugins, I hope it is OK)

set nocompatible              " be iMproved, required
filetype off                  " required

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'lervag/vimtex'
Plugin 'ycm-core/YouCompleteMe'
call vundle#end()            " required
filetype plugin indent on    " required
filetype plugin on
filetype on

nnoremap q :qall!<cr>

if !exists('g:ycm_semantic_triggers')
    let g:ycm_semantic_triggers = {}
endif
let g:ycm_semantic_triggers.tex = g:vimtex#re#youcompleteme

lervag added a commit that referenced this issue Aug 2, 2020
@lervag
Copy link
Owner

lervag commented Aug 2, 2020

Thanks @RiMillo for the heads up and good description. I think it is fixed now.

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

No branches or pull requests

3 participants