3

I'm trying to include a file whose path relative to the main tex file is ../folder/chapter.tex. I tried

\input{../folder/chapter.tex}

but it doesn't work: I get an error of file not found. I'm using MikTex 2.9 under Windows.

Simone
  • 133
  • 1
  • 1
  • 3

2 Answers2

5

After some testing from the command line using xelatex.exe, it seems to be the case that Latex requires the relative paths in your source files to be defined relative to the folder in which the Latex compiler is executed (not relative to the location of your main Latex file). This actually makes a fair bit of sense. These relative paths aren't being passed to your main Latex file; they're being passed to the Latex compiler. It will interpret relative paths in relation to where it was invoked.

Since you're using a GUI, I have no idea what MikTeX considers to be the folder in which the Latex compiler is executed.

I see two different solutions:

One, abandon relative paths and use absolute paths. You can even include the drive letter. Just remember to keep on using Latex's preferred forward slashes for the directory levels, for example:

\input{C:/project/latexfiles/mychapter.tex}

Two, you can start compiling from the command line. Keep a DOS box open to the folder where your main Latex file lives. When you're ready to compile, run the command to do so. Here are the parameters I use:

xelatex -synctex=-1 -shell-escape -halt-on-error mymainfile.tex

When I need to recompile, I go to the DOS box and press up to get the last command, then I hit return. Very convenient. You can of course use pdflatex.exe or whichever Latex compiler you need, and you can google to learn more about the various command line parameters and their effects.

  • You're right about paths: this is exactly why my command failed. I'm not fond of using relative paths, but after all it may not be too bad. A way to use paths that are relative to the main tex file is the import package. – Simone Mar 11 '15 at 21:04
  • This is no longer true with \input, at least with XeLaTex from TeXLive 2015. Relative paths are fine. I am not sure about \include, though. I'm having some trouble with it .... – Mars May 14 '19 at 00:44
  • @Mars What specifically are you saying is no longer true? I suggested absolute paths as an easy solution to his problem. Relative paths are also a fine solution as long as you know from which directory they are relative. – Tim Stewart May 14 '19 at 03:27
  • 2
    Tim, I think that I may have misunderstood "requires the relative paths in your source files to be defined relative to the folder in which the Latex compiler is executed". I took that to mean "relative to the location of the executable". I now think you probably mean "relative to the current working directory when the latex command is executed." I always run xelatex relative to where the main file I'm compiling is located, and my \input paths are always relative to that. (If that's what you meant--I now think it is--maybe a small clarification would be worthwhile.) – Mars May 14 '19 at 14:31
  • (But fwiw, I am done trying to make \include work with any paths. The failure messages aren't even consistent across runs of xelatex, or across different kinds of path specifications. I don't have sufficient time or interest to ask for help about it here, given that \input provides a perfectly good solution.) – Mars May 14 '19 at 14:35
  • @Mars I like your suggestion. Edited my answer to refer to the location relative to where the command is invoked. What do you think? – Tim Stewart May 14 '19 at 19:40
  • Tim, I don't see the difference. (Forgot to save?) – Mars May 14 '19 at 20:58
  • @Mars No worries. It was a small change. Search for the word “invoked” in my answer. That’s the updated part. – Tim Stewart May 14 '19 at 22:39
  • 1
    Ah, I see it now. – Mars May 15 '19 at 02:02
0

Using an IDE is in general more practical than command line. When an IDE is asked to typeset a file, it somehow goes to the directory of the file and launches the typesetting tool such that relative file paths are resolved relatively to the directory where the main file is located.

Some IDEs implement a concept of project (LaTeX Workshop...), whereas others just typeset the current file. TeXworks and others support magic comments to specify the master file.

In that case your chapter.tex could start with something like

% !TEX root = ../Main/main.tex

telling the IDE where the main file to be typeset is located.