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.

11
  • You know it should be \input{../folder/chapter.tex} (i.e. braces not parentheses) right? Commented Mar 10, 2015 at 22:25
  • Relative paths up forbidden? i.e. .. in every case.
    – Johannes_B
    Commented Mar 10, 2015 at 22:27
  • Sorry, I fail to see the difference. If you refer to the curly braces, I have already edited them in the question (I typed regular parentheses by accident, I have curly braces in my document, sorry about that).
    – Simone
    Commented Mar 10, 2015 at 22:27
  • @Johannes_B You can normally read from up: it's writing that's forbidden. With \input that should be OK, but \include would fail.
    – Joseph Wright
    Commented Mar 10, 2015 at 22:38
  • @JosephWright Ah, ok. That somehow slipped my mind at this hour. Thanks for clarification.
    – Johannes_B
    Commented Mar 10, 2015 at 22:40

2 Answers 2

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.

9
  • 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
    Commented Mar 11, 2015 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
    Commented May 14, 2019 at 0: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. Commented May 14, 2019 at 3: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
    Commented May 14, 2019 at 14:31
  • 1
    Ah, I see it now.
    – Mars
    Commented May 15, 2019 at 2: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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .