Skip to content
This repository has been archived by the owner on Mar 25, 2019. It is now read-only.

[Feature request] compile the main.tex while editing a subfile #8

Open
Yougmark opened this issue Mar 13, 2018 · 17 comments
Open

[Feature request] compile the main.tex while editing a subfile #8

Yougmark opened this issue Mar 13, 2018 · 17 comments
Assignees

Comments

@Yougmark
Copy link

Things work fine when I'm in main.tex. But it issues errors when I move to other tex files, for example, files in sections/ subdirectory. So live preview doesn't work when I'm editing non-main files.

Error detected while processing function <SNR>50_latex_compile:
line    7:
E121: Undefined variable: b:neotex_jobexe
E116: Invalid arguments for function jobstart
E15: Invalid expression: jobstart(['bash', '-c', b:neotex_jobexe], {'cwd': expand('%:p:h'), 'on_exit': function('s:job_exit')})
@donRaphaco
Copy link
Owner

Please try adding let g:tex_flavor = "latex" to your vimrc. (See :h g:tex_flavor for information)
If this does not solve the problem:
What is the output of :echo &ft when inside the buffer of a non-main file? (should be tex)
If you just open a non-main file (without having vim loaded any other .tex file before) does :NeoTex command give you the same error?

@Yougmark
Copy link
Author

let g:tex_flavor="latex" does not solve the problem.

:echo &ft outputs plaintex in a non-main tex file, even after I did the above command.

If I just open a non-main file, NeoTex is not a editor command: E492: Not an editor command: NeoTex.

@donRaphaco
Copy link
Owner

Ok looks like the file has the wrong filetype, as the plugin only gets loaded for tex filetypes.
I'm not sure why let g:tex_flavor="latex" doesn't work though... Did you add it to vimrc or just executed inside vim (if so you should reload the file afterwards with :e).
If you don't want to add let g:tex_flavor="latex" permanently to your vimrc you could force the filetype by adding % vim: set filetype=tex: to the last line of the tex file.
Another option is to symlink (or copy) neotex/ftplugin/tex.vim to neotex/ftplugin/plaintex.vim inside the plugin folder of your plugin manager (e.g. ~/.config/nvim/plugged), so the plugin also loads for plaintex filetypes.

@Yougmark
Copy link
Author

Yougmark commented Mar 15, 2018

Okay, I added let g:tex_flavor="latex" to neovim configuration file. NeoTex command in non-main tex file doesn't report errors, but the pdf file is not re-compiled, i.e., not changed when I recompile with new content.

It may matter that my non-main file is in a subdirectory, not in the same directory with main.tex?

@donRaphaco
Copy link
Owner

You mean the main pdf file of the hole project doesn't get updated?
Yes that's true, currently only the pdf for the subfile itself is updated and not the pdf of it's main file.
That's because when you edited one buffer the updated contents are saved to a tempfile which then gets compiled by pdflatex.
I understand that this might not be the desired behavior in this use case.
But I would call it a missing feature, rather than an error.
I will think about how this might be implemented in a simple way.
For now what you can do is open the pdf of the subfile you're currently editing (which should get created und updated in the same subdirectory).

@Yougmark Yougmark changed the title Errors when not editing main.tex [feature request]: compile the main.tex while editing a subfile Mar 15, 2018
@Yougmark
Copy link
Author

Okay, will be expecting the feature!

No, subfile isn't a valid latex document, as it doesn't have \begin{document} and so on.

I'll live without live preview for now.

Thanks!

@Yougmark Yougmark changed the title [feature request]: compile the main.tex while editing a subfile [Feature request] compile the main.tex while editing a subfile Mar 15, 2018
@donRaphaco donRaphaco self-assigned this Mar 15, 2018
@donRaphaco
Copy link
Owner

subfile isn't a valid latex document, as it doesn't have \begin{document}

Ok.. I thought you're using the subfiles package, my fault

@james-oldfield
Copy link

+1 for this feature!

As of current, I'm keeping the main.tex open in another small split and running :NeoTex manually from there every time I update another .tex file that is included in main.tex via \input{}.

Would love to offer a hand writing the feature—perhaps some option of passing the name of the main tex file?

donRaphaco pushed a commit that referenced this issue Mar 26, 2018
@donRaphaco
Copy link
Owner

donRaphaco commented Mar 27, 2018

I made the subfile branch and pushed a prototype.
There a two Problems which make the implementation a bit tricky:

  1. The way neotex currently works is that after every edit of a .tex file the contens of the buffer get saved to a tempfile, which then gets compiled with pdflatex. But when you now edit a subfile and want to actually compile the mainfile you need to tell pdflatex somehow that it needs to use the edited tempfile instead of the saved file everywhere \input or \include is used in the mainfile.
  2. How can the plugin detect that a file is a mainfile or a subfile. @james-oldfield, as you already mentioned, here should be some kind of option to set the mainfile. But optimally this option is only set once per file and keeps saved.

While the implementation I committed did handle these problems I'm not perfectly satisfied with the solution.
What works so far and what doesn't:

  • for a subfile you can specify the mainfile in a comment like: % NeoTex: mainfile=../main.tex.
    It works similar like the vim modeline options, so it must be within the first 5 or last 5 lines of the file (default value is 5, but can be chanded by the modelines option). At the moment it's important to have only space after NeoTex: and no spaces around the =, everything after the = is interpreted as the filname of the mainfile and must be relative to the subfile.
  • When you edited the NeoTex "modeline" you have to reload the file.
  • It works if the subfile is included by \input or \include to the mainfile
  • \includeonly isn't supported so far, as well as nested \input
  • the \input/\include in the mainfile must specify the relative location to the subfiles
  • the subfiles must be inside the same directory or a subdirectory as the mainfile
  • if you edit subfiles and/or the mainfile at the same time (having unsaved changes in mutliple files) the compiled preview will always show the version with the (unsaved) current file and the saved versions of the other files.
  • I think it doesn't work with latexdiff yet (I haven't actually tested it)

How it works:

  • If neotex detects a valid "modeline" for the mainfile it knows the current file is a subfile and also the location of the mainfile (I think it's good solution to problem 2)
  • when a subfile gets updated neotex still saves the content of the current buffer to a tempfile, but instead of compiling the tempfile sed is executed in background and all occurrences of \input{relative/path/to/subfile} or \include{relative/path/to/subfile} are substituted with \input{/fullpath/to/tempfile} (or \include{.. respectively). The output of the sed command is saved to another tempfile which then gets compiled instead. (solves problem 1, but I don't like it)

The solution using sed was the most simple I came up with, but it's not verry variable (e.g. it's hard to add features like support for \includeonly or nested \inlude).
My goal is to keep this plugin as simple as possible, so having as few lines as possible and as less internal functionality as possible by using external command and tools.

Feedback, suggestions, comments are welcome :)

P.S. I also added the option g:neotex_log to help debugging. if you set it to 1 all stderr messages of the command pipeline are saved to neotex.log in the current directory, if set to 2 also stdout messages are logged

@Yougmark
Copy link
Author

Yougmark commented Mar 27, 2018

It works quite well. Thanks!! A few comments:

  1. The modeline option only works when it's within the first 5 lines fo the file, not the last 5.
  2. The tempfile is not applied. I think mine is still compiling the file referred by \input.
  3. Could you turn off the log when opening the file, as follows?
"automotive_background.tex" 170L, 9982C
mainfile:./main.tex
mainfile:./main.tex
Press ENTER or type command to continue

@donRaphaco
Copy link
Owner

Thanks @Yougmark for the feedback.

  1. The modeline option only works when it's within the first 5 lines fo the file, not the last 5.
  2. The tempfile is not applied. I think mine is still compiling the file referred by \input.
  3. Could you turn off the log when opening the file, as follows?
  1. found the bug it should be fixed now
  2. That's propably because it's currently not verry flexible when trying to substitute the the \input in the mainfile. One limitation I forgot to mention is that the filename in \input must end with .tex.
    You should be able to open the substituted mainfile with
    :execute 'tabe ' . b:neotex_compile_tempname when inside a buffer with a subfile (only works if you edited the subfile before).
  3. A debug message I forgot to delete, which I did now

@donRaphaco
Copy link
Owner

I pushed a update, which hopefully works much better.
Things that changed:

  • The "modeline" now must end with a : at the end of the line.
  • You can now edit multiple subfiles and the mainfile and all changes should be reflected in the created pdf.
  • latexdiff is disabled for now

I would appreciate some help with testing.

@Yougmark
Copy link
Author

Very cool. I think everything works well. It's updated automatically even without saving the file. So you fixed the compilation with cache files. Great!!

Thanks!

@steveej
Copy link

steveej commented Jun 5, 2018

This sounds very promising. Is it working well enough to make a pre-release?

@donRaphaco
Copy link
Owner

This sounds very promising. Is it working well enough to make a pre-release?

It worked quite well for me so for, so I added the option g:neotex_subfile and merged it with the master branch.

@Pitasi
Copy link

Pitasi commented Aug 1, 2018

Hi, thanks your excellent plugin. I'm trying to use this subfile feature and it kinda works but maybe I'm missing something.

I'm gonna recap a bit all what I read in this issue so you can see what I've done:

  • I added let g:neotex_subfile=1 in my init.vim
  • I have a main.tex with a \include{sections/subfile.tex} (the .tex extension is necessary)
  • Inside the subfile at the very beginning I added the modline % NeoTex: mainfile=../main.tex:

Now, I open neovim and edit `subfile.tex. I can see my PDF viewer (evince) flashing, so the PDF has been updated - but there are no changes.

I save subfile.tex, this time no PDF flashes (I think it's right), so still no changes.

I make another edit to subfile.tex, the PDF this time is updated and show the old changes I saved a moment ago, but not the unsaved ones.


I'm not complaining that much, it's an -almost- live preview. Just reporting what I experienced :)

Also, probably a little subsection on how enable and use this feature can be added to the README.

@Pitasi
Copy link

Pitasi commented Aug 5, 2018

I understood what was causing my problem, I was using this snippet to include a bunch of files:

\foreach \i in {1, ..., 9} {%
    \edef\FileName{sections/file\i.tex}%
    \IfFileExists{\FileName}{%
       \input{\FileName}%
    }
}

using a more classical approach like:

\input{sections/file1.tex}
\input{sections/file2.tex}
\input{sections/file3.tex}
...

everything works fine!

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

No branches or pull requests

5 participants