lervag / vimtex

VimTeX: A modern Vim and neovim filetype plugin for LaTeX files.
MIT License
5.53k stars 391 forks source link

magic comment '%! TeX program = *' only work for *.tex #2326

Closed Freed-Wu closed 2 years ago

Freed-Wu commented 2 years ago

Description

Set latexmk and vimtex's default tex program to xelatex, then use %! TeX program = lualatex to look which tex program vimtex will use for latexmk. For main.tex, vimtex will use lualatex, it's right. But for other file which extension is not tex, such as dtx or tikz, it still use xelatex.

Steps to reproduce

This is a simple tex file:

%! TeX program = lualatex
\documentclass[tikz]{standalone}
\usepackage{iftex}
\begin{document}
\ifluatex
1
\else
0
\fi
\end{document}

this is a simple latexmkrc:

$show_time = 1;
$dependents_phony = 1;
$do_cd = 1;
$preview_continuous_mode = 1;
push @default_files, '*.ins', '*.dtx';
push @default_files, '*.tikz';
$pdf_mode = 5;
$xelatex = 'xelatex -shell-escape -8bit -file-line-error -no-pdf -synctex=1 %O %S';

and in vimtex config:

  let g:vimtex_compiler_latexmk = {
        \ 'options' : [
          \ '-verbose',
          \ '-synctex=1',
          \ '-interaction=nonstopmode',
          \ '-shell-escape',
          \ '-8bit',
          \ ],
          \ }
  let g:vimtex_compiler_latexmk_engines = {
        \ '_': '-xelatex',
        \ }

pdf_mode = 5 means we use xelatex to generate dvi and convert dvi to pdf by xdvipdfmx.

When the tex file named main.tex, vimtex:

System info:
  OS: Arch Linux
  Vim version: NVIM v0.6.1
  Has clientserver: true
  Servername: /tmp/nvimTwypKA/0

VimTeX project: main
  base: main.tex
  root: /dev/shm
  tex: /dev/shm/main.tex
  main parser: current file
  document class: standalone
  packages: epstopdf-base graphics graphicx ifluatex iftex infwarerr ltxcmds luatex85 pdftexcmds pgf pgfcomp-version-0-65 pgfcomp-version-1-18 pgfcore pgffor pgfkeys pgfmath pgfrcs pgfsys shellesc tikz trig xcolor xkeyval
  compiler: latexmk
    engine: -lualatex
    options:
      -verbose
      -synctex=1
      -interaction=nonstopmode
      -shell-escape
      -8bit
    callback: 1
    continuous: 1
    executable: latexmk
  viewer: Zathura
    xwin id: 0
  qf method: LaTeX logfile

It is right.

However, when the file is main.dtx or main.tikz:

System info:
  OS: Arch Linux
  Vim version: NVIM v0.6.1
  Has clientserver: true
  Servername: /tmp/nvimBgTwle/0

VimTeX project: main
  base: main.dtx
  root: /dev/shm
  tex: 
  main parser: current file
  document class: 
  packages: epstopdf-base graphics graphicx ifluatex iftex infwarerr ltxcmds luatex85 pdftexcmds pgf pgfcomp-version-0-65 pgfcomp-version-1-18 pgfcore pgffor pgfkeys pgfmath pgfrcs pgfsys shellesc tikz trig xcolor xkeyval
  compiler: latexmk
    engine: -xelatex
    options:
      -verbose
      -synctex=1
      -interaction=nonstopmode
      -shell-escape
      -8bit
    callback: 1
    continuous: 1
    executable: latexmk
  viewer: Zathura
    xwin id: 0
  qf method: LaTeX logfile

It use xelatex, and will output a pdf which content is 0 not 1.

Expected behavior

I think whatever the file's name is main.dtx or main.tikz or main.ins or other name, the tex program always be lualatex.

Actual behavior

only main.tex can work.

Do you use a latexmkrc file?

Yes

VimtexInfo

vimtexinfo has been attached above.
Freed-Wu commented 2 years ago

It seem to be related to the code judge the self.tex:

  let l:ext = fnamemodify(a:main, ':e')
  let l:new.tex = l:ext ==# 'tex' ? a:main : ''

If the file's ext is not tex, self.tex will be ''. And magic comment will not be used. Why not get extensions from latexmk's @default_files? Or just simply provide a variable named g:vimtex_extension_list?

Freed-Wu commented 2 years ago
index 0ea336d3..1163ec54 100644
--- a/autoload/vimtex/state/class.vim
+++ b/autoload/vimtex/state/class.vim
@@ -18,7 +18,7 @@ function! vimtex#state#class#new(main, main_parser, preserve_root) abort " {{{1
   endif

   let l:ext = fnamemodify(a:main, ':e')
-  let l:new.tex = l:ext ==# 'tex' ? a:main : ''
+  let l:new.tex = a:main

   " Get preamble for some state parsing
   let l:preamble = !empty(l:new.tex)

This can solve this problem.

lervag commented 2 years ago

Thanks! I agree that the suggested code should be relaxed. I want to avoid options, so I'll relax to allow .dtx and .tikz as well for now, which should resolve your issue.

Freed-Wu commented 2 years ago

.ins is also tex file extension. According to document of docstrip and doc, compile .ins to get package.sty, document.cls, example.tex, etc, and compile .dtx to get a document pdf about this macro package. I advice to add .ins.

I also suppose avoiding options (too many options maybe confuse user). However, I am not sure about .dtx, .ins, ... are all tex file extensions. If any new extensions are found (it seldom happen), we should add them.

lervag commented 2 years ago

Ok :)