Cam's Blog

December 1, 2009

Include, input and relative paths in latex

Filed under: Latex — cfranc @ 8:47 pm

I just spent about a half hour puzzling over a latex error, so I thought it’d be a good idea to write about it. It may some day save someone prone to make the same stupid errors as me.

Any large latex document should be split up into separate files. I like to use the \include command for all upper-level files. For instance, when using a book class, I insert each chapter into the document via an \include{chaptername} command. One advantage to using \include over \input for these top level pieces is that you can then use an \includeonly command at the top of your document to specify which of the files appearing in your document you actually want included. For instance, if you’re in the midst of writing a book, you may want to print a single chapter, say ch1.tex, for editing. Writing \includeonly{ch1.tex} before the \begin{document} line will ensure that out of all your include statments, only ch1.tex will actually be included in your document. You won’t need to comment out individual inclusion statements, and I believe that your numbering will appear as though the other include statements were executed. The numbering would get screwed up if all you did was momentarily comment out unwanted inclusions.

Now suppose that each chapter that you’re working on is itself quite large, and so you also want to break them up into multiple files. You can’t nest \include statements for certain technical reasons, but you can nest \input commands. All \input{file.tex} does is place the code inside file.tex at the location of the \input command, so nesting works just fine.

My problem arose since I was using relative path names to point to the files that I wanted to include (and because I was stupid). I had a main project folder containing

  • my preamble,
  • the main.tex file,
  • a subfolder for each chapter.

Each subfolder contained a .tex file for each chapter, as well as separate .tex files for each section of that chapter. I inserted each chapter into main.tex via an include command using relative file paths. For instance, the following line

\include{./ch1/ch1}

was in my main.tex file. Then in ch1.tex, I also had input statements for each section of chapter one. Originally I wrote the following inside ch1.tex:

\input{1.1}

This caused an error. The reason is because this code looks for 1.1.tex in the main project folder where main.tex is located! Since 1.1.tex was not in the main project folder, but in the subfolder for chapter one, I should have instead written

\input{./ch1/1.1}

inside ch1.tex. Even though ch1.tex and 1.1.tex were in the same folder, once I include ch1.tex into my main.tex file, all relative addresses appearing inside ch1.tex must be made with respect to the the main project folder as reference. In summary, if you are using nested inputs and relative paths in latex, ensure that all relative paths are made in reference to your main .tex file for proper addressing.

Edit Nov. 27, 2010: Removed the .tex extensions from \input and \include.

Advertisements

13 Comments »

  1. Hi there,

    I get errors every time I try to use multiple ‘.’ periods in relative paths with \include. \input does not have this problem. Why is this?

    Comment by justhavingfun — November 5, 2010 @ 10:57 pm

    • Do you have a short example to illustrate the problem?

      Comment by cfranc — November 6, 2010 @ 10:54 am

  2. I am unable to backtrack when using relative paths with \include{}. For example, my “latexstuff” folder currently contains a file called “Latex1.tex”. The folder “junk” is located one level above the “latexstuff” folder, and it contains a file called “includetest3.tex”, which only contains a short string of text, something like “Have a good day.” When I enter \include{../junk/includetest3} into the body of “Latex1.tex”, I receive three error messages.

    Comment by justhavingfun — November 6, 2010 @ 12:31 pm

    • I think you want \include{./junk/includetest3}. The first period says “starting from the directory latexstuff”, then you move up to junk and look for includetest3. If you have ../ then the double dots send you back one directory (to the parent of latexstuff) and it looks for junk in there.

      Comment by cfranc — November 6, 2010 @ 12:43 pm

      • Right, “junk” is a folder in the parent directory of “latexstuff”. IOW, these two folders share the same parent directory.

        \input{../junk/includetest3} works perfectly.

        \include{../junk/includetest3} gives the three error messages.

        But if I include a file from a folder at a lower level than “latexstuff”, I don’t have this problem.

        Comment by justhavingfun — November 6, 2010 @ 9:46 pm

      • Ok, I’m sorry, I misunderstood your tree structure. It looks like it’s a problem with miktex. See the thread here: http://comments.gmane.org/gmane.comp.tex.miktex/9767. The reason the error doesn’t crop up with input is because no .aux files need to be written when you access trees in the parent directory. When you use include, an .aux is written to keep track of chapter variables, sections,etc, etc. This seems to be the heart of the problem. (Added: I also get errors using include in this way)

        Comment by cfranc — November 7, 2010 @ 7:39 am

      • Thanks a lot for the tip. By the way, shouldn’t you have

        \include{./ch1/ch1}

        instead of \include{./ch1/ch1.tex} ? “\include{}” doesn’t work for me if I leave the extension on the end.

        Comment by justhavingfun — November 26, 2010 @ 10:41 am

      • Yup, there shouldn’t be an extension. I’ll fix that now. thanks!

        Comment by cfranc — November 27, 2010 @ 10:57 am

  3. Thanks man, I also made the same mistake.

    Comment by Yiguro — April 21, 2011 @ 11:46 am

  4. Try the import package. It allows relative imports

    Comment by panos — July 4, 2011 @ 10:05 am

  5. Brilliant. I spent the last 45 minutes puzzling over this. Thanks!

    Comment by W Ethan Eagle — March 25, 2012 @ 10:03 pm

  6. Thanks for the help! I was just about to give up.

    Comment by Yulia — April 9, 2012 @ 6:31 am

    • Thank you so much ! Couldn’t puzzle out this same issue ! You saved my day !

      Comment by Rand — May 16, 2013 @ 5:22 am


RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: