James-Yu / LaTeX-Workshop

Boost LaTeX typesetting efficiency with preview, compile, autocomplete, colorize, and more.
MIT License
10.55k stars 522 forks source link

Bibliography autocomplete fails when using subfiles #4192

Closed randolf-scholz closed 6 months ago

randolf-scholz commented 6 months ago

Pre-checks*

Environment*

The Issue*

I am using a multi-file project via subfiles. Following the manual, I include the bibliography via:

\usepackage[backend=biber]{biblatex}
\addbibresource{\subfix{bibliography.bib}}

However, then citation autocomplete fails, both in the main file and in included chapters.

Reproduction Steps

main.tex

\documentclass{book}
\usepackage{subfiles}
\usepackage[backend=biber]{biblatex}
\addbibresource{\subfix{bibliography.bib}}

\begin{document}
\subfile{chapters/chapter1.tex}
\backmatter
\printbibliography
\end{document}

chapters/chapter1.tex

\documentclass[../main.tex]{subfiles}
\begin{document}
\cite{....} % <- no autocomplete available!
\end{document}

Notably, autocomplete becomes available again, if \addbibresource{bibliography.bib} is used instead of \addbibresource{\subfix{bibliography.bib}} (even in the subfiles!), but then only the main.tex file can be compiled successfully.

James-Yu commented 6 months ago

\subfix is not supported. You may consider file a feature request on that and showing an example of the directory structure with latex examples indicating how the bib file should be located.

randolf-scholz commented 6 months ago

@James-Yu I assume some form of pattern matching is used? Because the following ridiculous workaround seems to function:

\usepackage[backend=biber]{biblatex}
\addbibresource{\subfix{bibliography.bib}}
\newcommand{\ignore}[1]{\relax}
\ignore{\addbibresource{bibliography.bib}}
jlelong commented 6 months ago

To detect the name of the .bib file, we scan the content of the command \addbibresource, which fails if the content is a macro.

I thought the .fls could help but maybe not. I will look into it.

randolf-scholz commented 6 months ago

Can't one just add an optional capture group here to add \subfix support:

https://github.com/James-Yu/LaTeX-Workshop/blob/a5f20a908985276142b0c19fee277aad91cfb14b/src/core/cache.ts#L390

randolf-scholz commented 6 months ago

I modified the line in my local ~/.vscode/extensions to

const bibReg = /(?:\\(?:bibliography|addbibresource)(?:\[[^[\]{}]*\])?){(?:\\subfix{)?([\s\S]+?)}(?:\})?|(?:\\putbib)\[(.+?)\]/gm;

and that seems to work.

James-Yu commented 6 months ago

No. Also need to consider compiling from subfile.

randolf-scholz commented 6 months ago

@James-Yu The simple patch

const bibReg = /(?:\\(?:bibliography|addbibresource)(?:\[[^[\]{}]*\])?){(?:\\subfix{)?([\s\S]+?)}(?:\})?|(?:\\putbib)\[(.+?)\]/gm;

seems to work, I can compile both main and chapters and I get autocomplete for \cite in both main and chapters.