TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to use subfiles, in a way that you can compile the middle chapters, let me explain by example.

main.tex (root folder)
--Chapter 1 (Chapter 1 folder)
----Section 1.1 (Chapter 1 folder)

Capter 1 is subfile in main.tex
Section 1.1 is subfile in Chapter 1 docment

What I can do is that I can build and view every section, and the main.tex, but I cannot build Chapter on its own. The code:

%main.tex
\usepackage{subfiles}
\begin{document}
\subfile{chap1/chapter1}
...
\end{document}

%chapter1.tex
\documentclass[../main.tex]{subfiles}
\begin{document}
\chapter{Introduction}
\subfile{chap1/purpose}
...
\end{document}

%purpose.tex
\documentclass[../main.tex]{subfiles}
\begin{document}
\section{Purpose}
\end{document}

The trouble I am in stems from the relative paths I believe, if I change the subfile in the chapters to:

\subfile(../chap1/purpose)

I can build the chapter and view it. But then it won't work from the main.tex files. Any idea how to do it right ?

Edit: If there is a method or command to get the absolute path, I would be able to solve it easily, is there such a thing ?

share|improve this question
up vote 3 down vote accepted

I have had the same issue as you.

It is important to note that \providecommand only defines \main one time which is the first time it is run.

So when providing \main in a subfile then it is also provided in master.tex and master.tex will not redefine it.

That is also why it needs to be done before the document class in subfiles as this is where the master.tex code is executed.

If you use \def\main{..} instead then it will be overridden every time you include a subfile.

It was solved as:

%main.tex
\usepackage{subfiles}
\providecommand{\main}{.} % relative path to master.tex
\begin{document}
\subfile{chap1/chapter1}
...
\end{document}

%chapter1.tex
\providecommand{\main}{..} %Important: It needs to be defined before the documentclass
\documentclass[\main/main.tex]{subfiles}
\begin{document}
\chapter{Introduction}
\subfile{\main/chap1/purpose} %Insert relative before file
...
\end{document}

%purpose.tex
\providecommand{\main}{..}
\documentclass[\main/main.tex]{subfiles}
\begin{document}
\section{Purpose}
\end{document}

I hope someone will find is useful.

Edit:

One can also solve this by providing \input@path. This makes the solution work without inserting \main in all path's

\makeatletter
\providecommand*{\input@path}{{..}}
\g@addto@macro\input@path{{..}}% append % if you cant override it
\makeatother
share|improve this answer
    
Thank already handed in my bachelor awhile ago, but its still nice to know, it is perfectly understandable, i just could not see my way out of it :D – Henrik Bøgelund Lavstsen Jan 3 '15 at 17:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.