2

I want to call some files in folders. So I have 3 files : 1. main.tex (in the root) 2. commands.tex (in ./chapters) 3. control1.tex (in ./chapters) These files contains below codes: main:

\documentclass{report}
\usepackage{subfiles}
\input{chapters/commands}

\begin{document}

\subfile{./chapters/control1.tex}

\end{document}

commands:

\usepackage{graphicx}

control1:

\documentclass[../main.tex]{subfiles}
\begin{document}
\chapter{Control }
\section{intro}
some text
\end{document}

When I run main.tex everything looks good and without error. But if I run control1.tex I got this error:

enter image description here

It should be mentioned that without commands it runs perfect. What should I do? My goal is to put commands line in separate file.

5
  • Why not \include{filename}? That's what I placed in my university's thesis template to insert parts into the document. Commented Aug 4, 2016 at 18:19
  • Include can not solve my problem. With subfile you should run all file included in main, alone. Commented Aug 4, 2016 at 18:39
  • (should*- -> can) Commented Aug 5, 2016 at 1:47
  • 1
    I had the same problem, and I resolved keeping the main.tex file and all the chapters.tex files in the same folder - in this way the relative path is the same for every file, and \input should work properly. Otherwise, you may want to try the solution given in this answer: tex.stackexchange.com/a/79060/74960
    – MarcoG
    Commented Aug 5, 2016 at 5:46
  • @MarcoG Thanks for the suggestion 👍
    – programmer
    Commented Jun 1, 2020 at 14:31

3 Answers 3

2

I am just turning my comment into an answer, since it solved OP's issue.

The problem is that when the file in ./chapters folder is compiled, it cannot find a file in folder ./chapters/commands.tex, since this is the path relative to main.tex file. I had the same problem while organising the files for my thesis: the easiest solution is to keep all the subfiles and the main file in the same directory - in this way the relative path is the same for every file, and \input should work properly.

If this is not possible, one may want to try the solution given in this answer by Heiko Oberdiek: https://tex.stackexchange.com/a/79060/74960 .

0

If using \subfile one has to set the subfile up as

\documentclass[../main.tex]{introduction}
\begin{document}
  % content The first line is needed, for the compiler
  % This way it knows where the main.tex lays.
  % The subfile does not require any header.
  % introduction.tex uses the main.tex header
\end{document}

in main.tex include it as \subfile{Subfiles/01_introduction/introduction}

+This way one can compile from within the subfile, because the dependencies are explicit. I suggest that, because this way one can achieve clarity.

One could use a single folder per chapter containing all additional information regarding that chapter such as photos or graphs, respectively.

0

One possible solution is to not inherit the document class and settings of main.tex in ./chapters/control1.tex.Instead you can define the documentclass and preamble in control1.tex itself.
In file chapter/control1.tex instead of inheriting(1st line):

\documentclass[../main.tex]{subfiles}

\begin{document}
\chapter{Control }
\section{intro}
some text
\end{document}

Change it to:

\documentclass{report}
\usepackage{subfiles}

%rest is same
\begin{document}
\chapter{Control }
\section{intro}
some text
\end{document}
2
  • Welcome to TeX.SE!
    – Mensch
    Commented Aug 26, 2023 at 14:29
  • Thanks @Mensch !! Commented Sep 6, 2023 at 15:37

You must log in to answer this question.

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