Subfiles errors while using style in the main file [duplicate]

I would like to build a modular document in latex. My main directory (in which I put the main file "thesis.tex") is called "tesi". Inside "tesi" i have another directory named "capitoli" where I woluld like to store my chapters named "chapter1.tex","chapter2.tex", and so on. Also I woluld like to compile either from my main file or directly from my subfiles, and so I used the subfile package.

Also I'm using a style file named mystyle.sty placed in the main directory "tesi". There is also a directory called "immagini" (where i store my images) inside the main directory.

If I use the following code

% main file "thesis.tex" in the main directory "tesi" %
\documentclass[a4paper,12pt]{memoir}
\usepackage{./tesi/mystyle.sty}
\begin{document}
\subfile{../tesi/capitoli/chapter1.tex}
\end{document}

% subfile "chapter1.tex" in the subdirectory "capitoli" %
\documentclass[../thesis.tex]{subfiles}
\begin{document}
\begin{figure}
\centering
\includegraphics{../immagini/image1.jpg}
\end{figure}
\end{document}


I can compile from subfile "chapter1.tex" but can't compile from main file "thesis.tex" (error messages refers to the path of the image files)

If I use the following code ( just changed the relative/absolute paths of mystyle.sty and image1.jpg)

% main file "thesis.tex" in the main directory "tesi" %
\documentclass[a4paper,12pt]{memoir}
\usepackage{../tesi/mystyle.sty}
\begin{document}
\subfile{../tesi/capitoli/chapter1.tex}
\end{document}

% subfile "chapter1.tex" in the subdirectory "capitoli" %
\documentclass[../thesis.tex]{subfiles}
\begin{document}
\begin{figure}
\centering
\includegraphics{./immagini/image1.jpg}
\end{figure}
\end{document}


I can't compile from subfile "chapter1.tex" (error message cannot find mystyle.sty) but can compile from main file "thesis.tex".

Someone could fix this issue, thanks.

Edit

I also post mystyle.sty

   \usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{subfiles}
\usepackage{graphicx}

• You mix in your explanation "chapter1.txt" and "chapter1.tex". Is chapter1 a tex or a txt file? – Clément Jul 3 '14 at 8:42
• Do you really need the mystyle.sty. Without this, it works perfectly. – LaRiFaRi Jul 3 '14 at 8:43
• @Clément it's chapter1.tex – user51020 Jul 3 '14 at 8:51
• @LaRiFaRi it's just for having a clean main file – user51020 Jul 3 '14 at 8:55
• … and I think you are right. Could the answers provided at Using subfiles package with .sty files could be of any help to you ? – Clément Jul 3 '14 at 8:57

It seems as this is not possible. You could report this behaviour to the maintainer of the subfiles package. The problem is that subfiles misinterprets your paths. In my opinion, you are having three options here:

1. You may omit the mystyle.sty and copy your header in the thesis.tex.

tesi/thesis.tex

% arara: pdflatex

\documentclass[a4paper,12pt]{memoir}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{subfiles}
\usepackage[demo]{graphicx}
\begin{document}
\subfile{capitoli/chapter1.tex}

Test for main file
\end{document}


tesi/capitoli/chapter1.tex

% arara: pdflatex

\documentclass[../thesis.tex]{subfiles}
\begin{document}
\begin{figure}
\centering
\includegraphics{./immagini/image1.jpg}
\end{figure}

Test for subfile
\end{document}


2. Or you have to make a copy of your most recent mystyle.sty into the folder tesi/capitoli and keep it actual (shortcuts do not work).

In this case it would look like:

tesi/thesis.tex

% arara: pdflatex

\documentclass[a4paper,12pt]{memoir}
\usepackage{mystyle}
\begin{document}
\subfile{capitoli/chapter1.tex}

Test for main file
\end{document}


and the chapter1.tex just as in my first example.

3. A third possibility would be, to omit your folder tesi/capitoli and to put all chapters in the tesi folder.

Edit

As pointed out by Steve B in his answer here (thanks to Clément for linking that in his comment), you could omit the problem by putting the thesis.tex into a lower folder level as well.

tesi/main/thesis.tex

% arara: pdflatex

\documentclass[a4paper,12pt]{memoir}
\usepackage{../main/mystyle}
\begin{document}
\subfile{../capitoli/chapter1.tex}

Test for main file
\end{document}


tesi/capitoli/chapter1.tex

% arara: pdflatex

\documentclass[../main/thesis.tex]{subfiles}
\begin{document}
\begin{figure}
\centering
\includegraphics{../immagini/image1.jpg}
\end{figure}

Test for subfile
\end{document}

• I would definitely go for this last option: move your main file to a main folder, leave the rest as it. – Clément Jul 3 '14 at 9:10
• Ok, nice didn't understand that I should move also my main files in a subdirectory...with relative path now it works. Thank you yery much. – user51020 Jul 3 '14 at 9:15