texmagic.nvim icon indicating copy to clipboard operation
texmagic.nvim copied to clipboard

Cannot get `build = _G.TeXMagicBuildConfig,` and `xelatex` working together

Open amalgame21 opened this issue 2 years ago • 1 comments
trafficstars

I used the init.lua in https://github.com/nvim-lua/kickstart.nvim and modified it a very little bit for texmagic and texlab.

init.lua config:

  use({'jakewvincent/texmagic.nvim',
     config = function()
        require('texmagic').setup({
            -- Config goes here; leave blank for defaults
            engines = {
                xelatex = {
                    executable = "latexmk",
                    args = {
                        "-xelatex",
                        "-verbose",
                        "-file-line-error",
                        "-synctex=1",
                        "-interaction=nonstopmode",
                        "%f"
                    },
                    isContinuous = false,
                }
            }
        })
     end
  })

vim.g['tex_flavor'] = 'latex'
require('lspconfig').texlab.setup{
    cmd = {"texlab"},
    filetypes = {"tex", "bib"},
    settings = {
        -- https://github.com/latex-lsp/texlab/wiki/Configuration
        texlab = {
            rootDirectory = nil,
            --      ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
            build = _G.TeXMagicBuildConfig,
            --      ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
            --------------------------------
            -- START force to use xelatex
            --------------------------------
            -- build = {
            --     executable = "latexmk",
            --         args = {
            --             "-xelatex",
            --             "-verbose",
            --             "-file-line-error",
            --             "-synctex=1",
            --             "-interaction=nonstopmode"
            --         }
            -- },
            --------------------------------
            -- END force to use xelatex
            --------------------------------
            forwardSearch = {
                executable = "zathura",
                args = {"%p"}
            }
        }
    }
}


I have a file HelloWorld.tex with magic comment to test it.

%! TEX program = xelatex
\documentclass[a4paper]{article}

\begin{document}
	Hello world!
\end{document}

Tried command :TeXMagicConfigFound, return true :TeXMagicSetupStatus, return true :TeXMagicCommentFound, return true :TexMagicSeletedProgram, return Program 'xelatex' selected from user-provided build engine. :TexMagicShowsComment, return %! TEX program = xelatex and finally :lua print(vim.inspect(_G.TeXMagicBuildConfig)) can also display the corresponding value.

However after using :TexlabBuild command to build the file I check the HelloWorld.log file first line shows (preloaded format=pdflatex 2022.11.9) Indicate it use pdflatex instead of xelatex

when I comment out build = _G.TeXMagicBuildConfig, and force Texlab to use xelatex and perform :TexlabBuild HelloWorld.log file first line indicate (preloaded format=xelatex 2022.11.9) This time use xelatex successfully

So I think the line build = _G.TeXMagicBuildConfig, is not working. I am very new to lua, just switch from init.vim to init.lua few days ago. Did I mess up something? Please let me know! thanks!

amalgame21 avatar Dec 23 '22 01:12 amalgame21

Hi @amalgame21, very sorry I didn't respond when you first filed this issue. I'm responding in case you'd still like to try to get this to work, but no worries otherwise.

This appears tricky since the diagnostics you provided clearly indicate the plugin is loaded, the magic comment has been read, and the build engine has been found in your TeXMagic config. It seems like what's probably happening is that your TeXMagic setup is not being called before your Texlab setup. As a result, _G.TeXMagicBuildConfig may be nil when Texlab is set up, so Texlab falls back on the default build engine. Then TeXMagic is setup, _G.TeXMagicBuildConfig is given a value according to your magic comment and TeXMagic config, and the diagnostics suggest everything is normal.

I see that your TeXMagic config is set up in Packer's use function but that you're setting up Texlab right in your init.lua. I'm not entirely sure how Packer loads plugins and whether plugin loading is delayed at all compared to init.lua contents outside of the use function, but my first suggestion would be to find a way to enforce a strict ordering in the loading/setup of TeXMagic and the setup of Texlab. What I do is I configure both in a setup file for configuring my LSPs (call it lsp_setup.lua) which is placed in .config/nvim/lua/, and I load this lsp_setup module in the config function for lspconfig. Here's a little example:

-- init.lua
-- ...

use({
    "jakewvincent/texmagic.nvim"
})

use({
    "neovim/nvim-lspconfig",
    config = function()
        require("lsp_setup")
    end
})

-- ...
-- lsp_setup.lua
-- ...

require("texmagic").setup({
    engines = {
        -- your build engines
    }
})

require("lspconfig").texlab.setup({
    -- ...
    settings = {
        texlab = {
            build = _G.TeXMagicBuildConfig
        }
    }
})
-- ...

jakewvincent avatar Nov 26 '23 22:11 jakewvincent