2

I am using overleaf and I have a main document and several subfiles. Is there a way of always having the main file compile even if I have selected and I am editing subfiles? Cheers in advance, Blake

  • 2
    https://www.overleaf.com/learn/how-to/Can_I_choose_which_file_is_the_main_tex_file_in_a_project_on_Overleaf%3F – Ulrike Fischer Oct 31 '22 at 11:33
  • Yes, I had already found that and looked into it, however, that doesn't solve the problem. If I press compile when on a subfile only compiles that subfile. I just wondered if there was a way always to force it to compile the main text irrespective of what subfile is open. – Blake Rowan Oct 31 '22 at 11:53
  • ask the overleaf support. They can check your project setup. – Ulrike Fischer Oct 31 '22 at 12:13
  • Alternatively just use \input and \include instead of using subfiles, that way there would be no \documentclass in your sub-files. – user202729 Oct 31 '22 at 13:21
  • Actually some old question I can find claims it happens even without documentclass Get Overleaf to compile main document instead of \input chapter? - TeX - LaTeX Stack Exchange. Maybe it's fixed now. – user202729 Oct 31 '22 at 13:22
  • @user202729 Overleaf literally searches for a line that starts with \documentclass (whitespace ignored) within the first 30kB of the file contents, so something else must have been happening for that user. See https://github.com/overleaf/overleaf/blob/4688cb7b5485cdb58e4fa0da23d671a1828bdc69/services/web/app/src/Features/Documents/DocumentHelper.js#L38 and https://github.com/overleaf/overleaf/blob/dc2097e6845e5d86050498f6be1153140a731608/services/web/app/src/Features/Project/ProjectRootDocManager.js#L29 – yo' Oct 31 '22 at 14:09

1 Answers1

5

(Tom from Overleaf Support here.)

To me it seems that always wanting to compile the main file quite defies the purpose of using subfiles, but if you wish, you can do that by replacing

\documentclass[...]{subfiles}

with

\csname documentclass\endcsname[...]{subfiles}

in the sub-file. This dirty hack will trick our compiler into not seeing the \documentclass command and thus compiling the main file instead.

yo'
  • 51,322
  • That works wonders cheers. I was just using subfiles to split the document up. Cheers for the help. – Blake Rowan Oct 31 '22 at 14:04
  • @BlakeRowan Cool -- great to hear it works. Note that then a simple \input would do the trick just the same, without the overhead of the non-trivialities subfiles is doing. – yo' Oct 31 '22 at 14:10