0

Setup

I have the following folder structure:

home/ A/ A.tex C.tex B/ B.tex C.tex 

and the file contents are as follows

home/A/A.tex

\input{../B/B.tex} 

home/B/B.tex

\documentclass{article} \begin{document} Hello from B! \input{C.tex} \end{document} 

home/A/C.tex

Hello from A/C! 

home/B/C.tex

Hello from B/C! 

Result

As mentioned in the title, I'm using the LaTeX Workshop in VSCode. When I run A/A.tex, I expect it to create A/A.pdf which displays

Hello from B! Hello from A/C! 

However, it does not do that! Instead, it creates B/B.pdf which displays

Hello from B! Hello from B/C! 

which is not what I expect and not what I need. It seems as if VSCode automatically changes the input path to that of B/B.tex.

Some research

I noticed this doesn't happen in Texmaker. Also, if I temporarily change the content of A/A.tex to what's displayed below, then the input path resolves itself and I obtain A/A.pdf.

% \input{../B/B.tex} \documentclass{article} \begin{document} Hello from A! \end{document} 

I then undo these changes, then compile A/A.tex again and then I do obtain the A/A.pdf with the expected content!

What's more, the editor tab in VSCode displays the input path (I think?) and it's updated as mentioned above:

Before:
Before

After:
After

To me, it is a lot of indication that it's VSCode, or LaTeX Workshop, and not LaTeX that does a lot of automatic resolving of input paths, which I do not want/need.

Question

Why does it do this and how can I resolve it? How can I ensure that VSCode always creates the PDF corresponding to the TeX file that I'm compiling?

Edits to this post

EDIT 1

Before I update A/A.tex with a document class and undo this, the output file says

Running 'pdflatex --max-print-line=10000 -synctex=1 -interaction=nonstopmode -file-line-error -recorder "c:/Users/.../B/B.tex"' 

The file being asked to compiled is indeed plain wrong. This fits perfectly with what I'm seeing. How can this be corrected in VSCode?

EDIT 2

I also see that the LaTeX Workshop extension has a configuration option for automatic root file detection. This automatic detection is not working in my favour here. How can I disable this? Neither of the two existing options are what I want.

configuration options

4
  • input paths are relative to the working directory of the tex process, so the directory in which you start latex, they are not relative to the current file. So it is usually less confusing if you always start latex in the folder of the main document so latex main not latex some/path/to/main so that the relative paths agree. Commented Oct 15 at 8:48
  • @Explorer yes indeed, the green triangle. Commented Oct 15 at 8:55
  • @DavidCarlisle the output mentions Running 'pdflatex --max-print-line=10000 -synctex=1 -interaction=nonstopmode -file-line-error -recorder "c:/Users/.../A/A.tex"'. This tells me that LaTeX Workshop never starts latex from the main document folder anyway. Doesn't mean you're not on to something. Commented Oct 15 at 8:57
  • well the editor just does what you ask it to do, but that presumably means all input paths are relative to your home directory (or whever you started the editor) which seems unfortunate but I don't have latex workshop so I'll let others answer. Commented Oct 15 at 8:59

1 Answer 1

3

I found that this link answers the questions "why?" and "how to solve?":

https://github.com/james-yu/latex-workshop/wiki/compile#multi-file-projects

Why:

While it is fine to write all contents in one .tex file, it is common to split things up for simplicity. For such LaTeX projects, the file with \documentclass[]{} is considered as the root file, which serves as the entry point to the project. LaTeX Workshop intelligently finds the root file when a new document is opened, the active editor is changed, or any LaTeX Workshop command is executed.

How to fix:

Magic comment: % !TEX root = relative/or/absolute/path/to/root/file.tex. If such comments exist in the currently active editor, the referred file is set as root. You can use the command latex-workshop.addtexroot to help you insert the magic comment. Note that magic comments need you to set latex-workshop.latex.build.forceRecipeUsage to false. The default true disables magic comments.

2
  • 2
    Could you summarize this? If the link rots, we still would have a valid answer then. Commented Oct 15 at 12:12
  • 2
    @JasperHabicht added. Commented Oct 15 at 12:52

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.