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 compiles within symlink directory instead of base directory #2820

Closed
ravibrock opened this issue Oct 28, 2023 · 4 comments
Closed

VimTex compiles within symlink directory instead of base directory #2820

ravibrock opened this issue Oct 28, 2023 · 4 comments
Labels

Comments

@ravibrock
Copy link

ravibrock commented Oct 28, 2023

Description

For my math homework, I store the $\LaTeX$ source files in a git repo, and symlink them to my desktop for easy access. This is my VimTex config (for Lazy):

{
    "lervag/vimtex",
    ft = { "tex" },
    keys = {
        { "<leader>ll", "<CMD>VimtexCompile<CR>" },
        { "<leader>lv", "<CMD>VimtexView<CR>"},
        { "<leader>lc", "<CMD>VimtexWipe<CR>"},
    },
    config = function()
        vim.g.vimtex_view_method = "sioyek"
        vim.g.vimtex_view_sioyek_options = "--new-window"
        vim.g.tex_conceal = "abdmg"

        function VimtexWipe()
            local current_file = vim.fn.expand("%:t:r") -- Filename without extension
            local extensions_to_delete = {
                ".aux",
                ".bbl",
                ".bcf",
                ".blg",
                ".cps*",
                ".fdb_latexmk",
                ".fls",
                ".log",
                ".mw",
                ".out",
                ".run.xml",
                ".synctex.gz",
                ".toc",
            }
            for _, ext in ipairs(extensions_to_delete) do
                vim.cmd("silent! !rm " .. current_file .. ext)
            end
            vim.cmd([[echo "VimTex: Compiler clean finished"]])
        end

        vim.api.nvim_create_user_command("VimtexWipe", VimtexWipe, {})
        vim.api.nvim_create_autocmd("VimLeave", { command = "lua VimtexWipe()" })
    end,
}

I have a function in my Neovim configuration that automatically opens symlinks in their base directory:

function FollowSymlink()
    local file = vim.fn.expand("%:p")
    local real_file = vim.fn.resolve(file)
    if (string.match(real_file, "[^%d]+:/") or real_file == file) then
        return
    end
    vim.cmd("file " .. real_file)
    vim.cmd("silent! w!")  -- Workaround for "file exists" error on write
end
autocmd("BufRead", { command = "lua FollowSymlink()" })

and I also have a custom template that I source with the following commands:

\usepackage{import}
\import{templates/math}{homework.sty}

However, I noticed that if I open the symlink, rather than the full filename, VimTex compiles in my Desktop folder (i.e. the auxfiles appear in my desktop) and it therefore produces an error as the templates/math folder is in the git repo with my homework, not my desktop.

This isn't a major bug, of course, so if there's not a trivial solution feel free to close the issue and I can live with it. Thank you!

Steps to reproduce

\ProvidesPackage{homework}
\newcommand\testtesttest{\LaTeX}
\usepackage{import}
\import{test}{homework.sty}
\begin{document}
\testtesttest
\end{document}
  1. cd ~/Desktop
  2. ln -s test/math.tex math.tex
  3. vim math.tex
  4. :VimtexCompile

Expected behavior

I would expect VimTex to compile in the base directory, not the symlink directory. Vim appears to be in the correct directory, as it (for example) provides tab completions for files in the proper git repo, not my desktop.

Actual behavior

VimTex compiles in my Desktop, and that's where it generates auxfiles. It's clear from VimtexInfo that it doesn't recognize the directory change, for some reason.

Do you use a latexmkrc file?

No

VimtexInfo

System info:
  OS: macOS 13.3.1 (22E772610a)
  Vim version: NVIM v0.9.4
  Has clientserver: true
  Servername: /var/folders/5j/84p0q84s4452bjhnyb8bjtqr0000gn/T/nvim.ravibrock/c0JZ41/nvim.60816.0

VimTeX project: MATH147
  base: MATH147.tex
  root: /Users/ravibrock/Desktop
  tex: /Users/ravibrock/Desktop/MATH147.tex
  main parser: current file verified
  document class: report
  packages: import silence
  source files:
    MATH147.tex
    templates/math/homework.sty
  compiler: latexmk
    engine: -pdf
    options:
      -verbose
      -file-line-error
      -synctex=1
      -interaction=nonstopmode
    callback: 1
    continuous: 1
    executable: latexmk
  viewer: sioyek
  qf method: LaTeX logfile
@ravibrock ravibrock added the bug label Oct 28, 2023
@lervag
Copy link
Owner

lervag commented Oct 29, 2023

For my math homework, I store the LATEX source files in a git repo, and symlink them to my desktop for easy access.

VimTeX does not automatically resolve the symlink and will use the source file's path as the active directory for compilation. I'm not going to change that, because some people rely on this as a feature. For example, some people use a shared "main" file across projects/notes and symlink it. When you open the symlink, things will work as expected because VimTeX does not resolve the symlink. Notice that this is also how tools like latexmk also work, i.e., if you compile the symlinked file, then it will treat the working directory as that of the symlink.

This is my VimTex config (for Lazy):

DON'T lazy load VimTeX! Remove ft = { "tex" }. Your custom VimtexWipe should not be necessary, you should instead use VimtexClean or VimtexClean! (to also remove pdf's). I propose you change your config to this:

{
    "lervag/vimtex",
    init = function()
        vim.g.vimtex_view_method = "sioyek"
        vim.g.vimtex_view_sioyek_options = "--new-window"

        vim.api.nvim_create_autocmd(
            "VimtexEventQuit",
            { command = "VimtexClean" }
        )
    end,
}

If you find VimtexClean does not remove enough files, then you want to update your ~/.latexmkrc file. For an example, see mine.

I have a function in my Neovim configuration that automatically opens symlinks in their base directory: …

It seems this is what you want to do, but you need to apply this change before the FileType is specified. Perhaps the BufReadPre autocommand is necessary for that, I'm not quite sure.

It seems to me, though, as your symlinks don't really have a useful purpose. Or, perhaps, I just don't understand the purpose.

I would expect VimTex to compile in the base directory, not the symlink directory. Vim appears to be in the correct directory, as it (for example) provides tab completions for files in the proper git repo, not my desktop.

The CWD for Vim is not necessarily the same as the CWD used by VimTeX for compilation. That is, VimTeX uses the main source file's path as the CWD during compilation - regardless of the CWD of Vim itself.

@lervag
Copy link
Owner

lervag commented Oct 29, 2023

In your steps to reproduce, could you make it more clear where your files are located? Are both math.tex and homework.sty located in ~/Desktop/test?

@ravibrock
Copy link
Author

Update: What you said about ignoring symlinks being a feature, not a bug, makes sense. I was also able to get the desired functionality by running :VimtexReload on enter, after changing the directory (a BufReadPre autocommand doesn't work). I'll make those changes that you suggested to my VimTex config and latexmkrc. This should be good to close now, unless there's something you had specifically wanted to address. Thank you!

@lervag
Copy link
Owner

lervag commented Oct 29, 2023

Update: What you said about ignoring symlinks being a feature, not a bug, makes sense. I was also able to get the desired functionality by running :VimtexReload on enter, after changing the directory (a BufReadPre autocommand doesn't work).

Glad to hear that you've found a way that works for you!

I'll make those changes that you suggested to my VimTex config and latexmkrc. This should be good to close now, unless there's something you had specifically wanted to address. Thank you!

If you find things to work as you want it to, then it seems safe to close the issue :)

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

2 participants