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

Vimtex can't recognize the main.tex file #2483

Closed
Yorkwn opened this issue Sep 9, 2022 · 7 comments
Closed

Vimtex can't recognize the main.tex file #2483

Yorkwn opened this issue Sep 9, 2022 · 7 comments
Labels

Comments

@Yorkwn
Copy link

Yorkwn commented Sep 9, 2022

Description

For some reason the last 2 weeks or so i've been unable to compile the main file of my latex project. I've explained the general issue also in the neovim subreddit so i won't repeat what i explained there. On the minimal directory,

main.tex includes:

\input{preamble.tex}

\begin{document}
Hello World
\end{document}

and preamble.tex:

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}

Steps to reproduce

  1. vim minimal.vim same as the example file but with plugged/ instead of bundle/
  2. mkdir Documents/minimal
  3. cd Documents/minimal/
  4. touch main.tex
  5. touch preamble.tex
  6. vim -u ~/minimal.vim main.tex

Expected behavior

The expected behavior would be to recognize the main.tex file and compile the document.

Actual behavior

Instead i get this error:

VimTeX: Compilation error due to failed mainfile detection!
Please ensure that VimTeX can locate the proper main .tex file.
Read ":help vimtex-multi-file" for more info.

Do you use a latexmkrc file?

No

VimtexInfo

System info:
  OS: Fedora release 36 (Thirty Six)
  Vim version: VIM 9.0 (1-348)
  Has clientserver: false

VimTeX project: main
  base: main.tex
  root: /home/gk/Documents/minimal
  tex: /home/gk/Documents/minimal/main.tex
  main parser: fallback current file
  document class: article
  packages: inputenc
  source files:
    main.tex
    preamble.tex
  compiler: latexmk
    engine: -pdf
    options:
      -verbose
      -file-line-error
      -synctex=1
      -interaction=nonstopmode
    callback: 1
    continuous: 1
    executable: latexmk
  viewer: General
  qf method: LaTeX logfile
@Yorkwn Yorkwn added the bug label Sep 9, 2022
@Rmano
Copy link

Rmano commented Sep 9, 2022

This seems the new main file detection code; it does not find a \documentclass do it thinks it's not the main file.

If you add:

%! TeX root = main.tex

at the top of your main.tex file. and exit and re-enter vim, it should work (it does here).

BTW, @lervag, maybe it would be better if after failing to detect the root file you force a re-parsing to find the root file when one try \ll again --- the need to reload/exit and re-enter is a bit non-intuitive ;-)

@lervag
Copy link
Owner

lervag commented Sep 9, 2022

For some reason the last 2 weeks or so i've been unable to compile the main file of my latex project. I've explained the general issue also in the neovim subreddit so i won't repeat what i explained there. On the minimal directory, …

The automatic detection of the main file assumes that the main file, or starting point, is the file that includes all other files. However, for this to work, VimTeX must be able to detect that a file is a main file. How do we do that? Well, VimTeX assumes the main file has both:

  • \documentclass at the top
  • \begin{document} somewhere below

Your example breaks this assumption, and VimTeX is therefore not able to verify that main.tex is the file you need to compile.

So, you ask, why does it fail now when it did not fail previously? That's because I pushed some code that would specifically prevent this from working. If VimTeX is not able to verify that a project has a valid starting point, then, instead of trying to compile the current file, VimTeX now instead errors with this message:

Instead i get this error:

VimTeX: Compilation error due to failed mainfile detection!
Please ensure that VimTeX can locate the proper main .tex file.
Read ":help vimtex-multi-file" for more info.

You have several choices to fix this, most of them are documented well in :help vimtex-multi-file. Alternatively, you can move the \documentclass line into main.tex.

@lervag lervag closed this as completed Sep 9, 2022
@lervag
Copy link
Owner

lervag commented Sep 9, 2022

BTW, @lervag, maybe it would be better if after failing to detect the root file you force a re-parsing to find the root file when one try \ll again --- the need to reload/exit and re-enter is a bit non-intuitive ;-)

No, it is not that easy. The main file detection has more consequences, because if you change the main file, you also change which files are detected as part of the project. However, you can always use \lx to reload VimTeX. That should work also in this case, I think.

Notice, though: If the main file parser yields "fallback current file", then the compiler will check if the current file is a valid main file. This check is performed when you do \ll. So, if you opened the sample project here (main.tex), then first \ll will fail. Now, if you move the \documentclass line from preamble.tex to main.tex and do \ll, it should work.

@Rmano
Copy link

Rmano commented Sep 9, 2022

Notice, though: If the main file parser yields "fallback current file", then the compiler will check if the current file is a valid main file. This check is performed when you do \ll. So, if you opened the sample project here (main.tex), then first \ll will fail. Now, if you move the \documentclass line from preamble.tex to main.tex and do \ll, it should work.

Yes, probably, but it will not work if I just add the %! TeX root directive --- so I imagine this is by design?

@Yorkwn
Copy link
Author

Yorkwn commented Sep 9, 2022

So all this time i was using it wrong but it would still fallback to the specific file and compile it. This also explains the fact that if i compiled first time on another file of the project, it would compile with tons of errors.

Thanks a lot, didn't know that you patched the fallback option. The more effective way of those i read is to just modify the preamble and the template so they fit the patern. I've also seen the autocmd one, but wouldn't that mean that i would have to have an augroup for all the same autocmds but for different project paths? Or can i do it universal somehow?

@lervag
Copy link
Owner

lervag commented Sep 9, 2022

Notice, though: If the main file parser yields "fallback current file", then the compiler will check if the current file is a valid main file. This check is performed when you do \ll. So, if you opened the sample project here (main.tex), then first \ll will fail. Now, if you move the \documentclass line from preamble.tex to main.tex and do \ll, it should work.

Yes, probably, but it will not work if I just add the %! TeX root directive --- so I imagine this is by design?

Yes. The tex root directive is only used during initialization and will decide the main file. Again, you can force reload with \lx, so no need to quit. But this is more complicated than check for a valid main file which I mentioned in my previous comment.

So all this time i was using it wrong but it would still fallback to the specific file and compile it. This also explains the fact that if i compiled first time on another file of the project, it would compile with tons of errors.

Exactly. Or, "using it wrong" is perhaps wording it too strongly. Your project obviously compiles and works. But LaTeX is hard to parse, and by allowing some of these assumptions on the structure, it makes things much easier. I believe this is a pragmatic decision that impose a trivial restriction on users.

Thanks a lot, didn't know that you patched the fallback option. The more effective way of those i read is to just modify the preamble and the template so they fit the patern.

Agreed.

I've also seen the autocmd one, but wouldn't that mean that i would have to have an augroup for all the same autocmds but for different project paths? Or can i do it universal somehow?

You mean the method explain in :help b:vimtex_main? I've never used it myself - I've added support for it because someone wanted it, once.

I believe the more common alternative to the automatic parsing is to use a tex root directive, see :help vimtex-tex-root. It works well and is also supported by other editors.

Still, by adjusting your files to follow the structure explained above, I believe the automatic main file detection will work well.

@lervag
Copy link
Owner

lervag commented Nov 5, 2022

PR #2558 is now merged, which means mainfile detection for cases like this will now work as expected:

% preamble.tex
\documentclass{minimal}

% main.tex
\input{preamble}
\begin{document}
\end{document}

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

No branches or pull requests

3 participants