Disclaimer: This question is a bit specific to TeXShop because I have to help somebody who's using this editor I don't know as I don't own a Mac. Nevertheless, it could be helpful to others.

The aim is as follows: a main.tex main file \includes some subfiles, say ChapOne.tex and ChapTwo.tex:

% main.tex file

By running:

  1. pdflatex main, all the subfiles are included;
  2. e.g. pdflatex "\includeonly{ChapTwo}\input{main}", only ChapTwo.tex subfile is included.

I'm looking for a way to be able to launch the 2nd run from the corresponding subfile. The (TeXShop) magic comment % !TEX root = main.tex is nice but doesn't let me add the \includeonly command.

Hence my question: is there another magic comment, like:

% !TEX program = pdflatex "\includeonly{ChapTwo}\input{main}"

or, better, like:

% !TEX program = pdflatex "\includeonly{\currentsubfile}\input{main}"

I could use to achieve my goal?

(I know I could achieve a closer goal with the standalone class but this would require to add a complete preamble and document environment in the subfiles, which I'd like to avoid especially because in the real use case the main and subfiles are not in the same directories and relative paths to some auxiliary files (graphic files among other) wouldn't be the same.)

  • Sorry but... when one is using TeXShop or other GUI, one would not want to pass pdflatex "\blah\blah\blah" to terminal, right? I would say a GUI-flavor workaround is to create a control panel.tex containing \includeonly and only include this file in the root.
    – Symbol 1
    Jan 30, 2015 at 7:42
  • @Symbol1 That's the reason I'm looking for a magic comment to put at the beginning of the subfile that lets the user compile as usual (e.g.command-T shortcut) but tells 1) the file to be compiled is not the current one but main.tex (% !TEX root = main.tex magic comment is OK for that), 2) typeset should be run, not with pdflatex main", but with pdflatex "\includeonly{...}\input{main}". About your workaround, no need for an extra file: \includeonly could be put in main.tex, but I'd like to avoid this as it needs to be changed for each subfile to be individually typeset. Jan 30, 2015 at 8:20
  • I said so because I hope that one day we would have a control panel recognizing this file that allows us to check something like <input type="checkbox">... Well, since nowadays editors are able to recognize \section in lines, an independent file is unnecessary in any case.
    – Symbol 1
    Jan 30, 2015 at 8:35

1 Answer 1


Apart from my comment, you can also

For example I have an IncludeOnly.engine under ~/Library/TeXShop/Engines:

line=$(head -n 1 $1)
re="\% root \= (.+)$"
if [[ $line =~ $re ]];
then pdflatex "\includeonly{$basename}\input{${BASH_REMATCH[1]}}";
else echo "bad subfile";

and 225362.tex:

    This is the root file.

and 225362-3.tex

% root = 225362
This is the third whatever.

magic enough? you may also create some % includealso = 225362-4 syntax to control everything.

You must log in to answer this question.

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