4

I am following this tutorial (section subfiles) on how to use the package subfiles for a large latex document. I am using this package because I want to be able to compile each chapter separately. I keep my chapters in ./tex/ but main.tex is located in the root directory.

When I compile main.tex, I get no error but when I try to compile a chapter I get an error because the file ./mystyle.sty can't be found. If I go to main.tex and change the line \usepackage{./mystyle} to \usepackage{../mystyle}, I can compile the chapters now but I can't compile main.tex. I could solve this by providing the full path of to mystyle.sty but then it won't work with other computers. I would rather not the standalone package because I would like to use the command \chapter.

I have read the six year old post about the same issue but I was wondering if in the meantime subfiles package was modified to solve this or if there is a way to use \chapter in the standalone package.

4
  • There is no problem with the standalone package (which you need here) and \chapter. But I would put the mystyle.sty in a local texmf tree. Then it is found without problems from everywhere. Aug 7, 2017 at 12:20
  • 1
    irrespective of subfiles \usepackage should take a name not a filepath, \usepackage{./mystyle} should be \usepackage{mystyle} Aug 7, 2017 at 12:24
  • @UlrikeFischer Could you explain how do you use /chapter with standalone? If I compile \documentclass{standalone} \begin{document} \chapter{Test} Test \end{document} I get undefined control sequence error.
    – Pedro
    Aug 7, 2017 at 12:46
  • I wrote the standalone package. Put \usepackage{standalone} in the main file, and use \documentclass{book} in the subfile. Aug 7, 2017 at 12:48

3 Answers 3

1

In my opinion the clean way of solving this would be to properly install mystyle.sty in your texmf folder.

If you do not want to do this, you could add the parent folder to your input path, see Can a default path be set globally for \input{...} akin to \graphicspath{...}? how to do this and then use \usepackage{mystyle}.

0

If your subfiles are in a folder inside the root folder, like so:

root/
|- main.tex
|- mystyle.sty
|- tex/
   |- chapter1.tex
   |- chapter2.tex

You can define a \main variable with \providecommand, that gives the relative path to the root dir :

% main.tex
\providecommand{\main}{.}
\documentclass[...]
...
\usepackage{subfiles}
\usepackage{\main/mystyle}
...
\subfile{tex/chapter1}
\subfile{tex/chapter2}


% chapter1.tex
\providecommand{\main}{..}
\documentclass[\main/main.tex]{subfiles}
...

% chapter2.tex
\providecommand{\main}{..}
\documentclass[\main/main.tex]{subfiles}
...

Since \providecommand defines a command once, and then ignores the next redefinitions, when you compile just chapter1.tex, \main takes the value "..", and when subfiles includes the preamble of main.tex, \main is effectively replaced by ".."

EDIT: Apparently, you're not supposed to give a path to \usepackage. You can see it because you get a warning :

LaTeX Warning: You have requested package '../mystyle', but the package provides 'mystyle'.

If you don't care about the warning, you can just leave, because it works (magically, that is...)

If you don't want a warning, then making a symlink of mystyle.sty in tex/ is a viable solution. See this answer

EDIT 2: It seems that \include and \includeonly commands are a great replacement to subfiles, if the reason why you want to compile the chapters separately is performance, and not that you really want a PDF with just one chapter.

To use it, just replace all the \subfile{} with \include. Then, in the preamble of your main.tex, you can use : \includeonly{chapter2}, and that will create a document with cover and backpages and all, but with chapter1 and all other that you didn't specify, missing. It even uses the generated .aux files to keep page numbers that are consistent with the total document.

0
0

This issue is solved in the subfiles package from version v1.4 2019/10/25 onwards; see also page 3 of its documentation, no need to define \main. Just use it this way:

% main file
\documentclass{myclass}
\usepackage{mystyle}
\usepackage{subfiles} % should be loaded last in the preamble!
\begin{document}
...
\end{document}

% subfile in sub-directory
\documentclass[../main]{subfiles}
\begin{document}
...
\end{document}

By the way, the subfiles package should be loaded last in the preamble.

You must log in to answer this question.

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