# Compile directive via an included file

## Background:

For one project, I store all my header .tex/.sty files in ../../../Templates, and all the files need to be at a particular relative directly location so using this relative hierarchy enforces that rule.

In order to speed up compilation, I created a pre compiled .fmt file. Now to use this, I can either:

1. Add to the command line:

-fmt ../../../Templates/MyPreamble

2. Add the following line as the the first line in my .tex file:

%& ../../../Templates/MyPreamble


and both methods work fine.

## Question:

What I would like to do is to to have the first line in the .tex file as

\input{../../../Templates/MySetup.tex}


and the content of the MySetup.tex invoke the .fmt file with something such as:

%& ../../../Templates/MyPreamble


Is there anyway to do this?

## Reasons:

I am open to any other suggestions as well, but here is my reasons for this:

1. I used the first method and adjusted the pdflatex command in TeXShop to have that directive. However, when I am working on some other project (ex, an TeX.SE answer) I do not want that pre-compiled format, so need a way to disable it. Hence decided not to use this method.

2. I already have ALL file individual files set up to \input the MySetup.tex file on the very first line. If I can just change that one file, then I can easily switch between using the pre-compiled format or not, by just changing the MySetup.tex file. And yes I could use a shell script to update all the files, to add this first comment line, but would prefer to not to do that.

-
It might be easier to set up two different compile actions in TeXShop (if it supports it), so you can use the normal pdflatex route for TeX.SE answers etc, and your own mypreamble route for everything else (though you would have to remember to press the right one). Alternatively, you could set up the compile action to be a script that checks based on the location of the document (or the first line of the file say) which command line to invoke. –  cyberSingularity Nov 8 '12 at 23:42
@cyberSingularity: Yeah thought of that, but need to also do it for TeXworks. Since it seems that what I want can not easily be done, it seems that I need to go down that route. –  Peter Grill Nov 9 '12 at 0:10

You have a slight problem in that the syntax

 \input{../../../Templates/MySetup.tex}


requires \input to be a macro with one argument which requires the format with that definition to have been loaded, so you can't use it to input a file that will load the format.

What you can do (which is what my ancient pre-latex2e) mylatex.ltx does, is redefine things so that you can have a standard latex preamble which will be read as normal but your custom format skips to \begin{document} to a custom command or comment depending on you needs.

If you want to keep with your existing file structure with \input then in the format file just define

\def\input#1{\def\input{\@ifnextchar\bgroup\@iinput\@@input}}


which will mean that if using latex your first line of

\input{foo}


will input foo.tex but if using your custom format then foo.tex won't be loaded but \input will be (re)defined to its normal latex definition so the rest of the file will work as normal.

If you do almost any tex operation it is too late to switch formats so the format specification has to be on the commandline or the magic first line comment.

-
@egreg er the def should be there but it was supposed to be defining input, will fix thanks:-) –  David Carlisle Nov 8 '12 at 23:40
Summary: I can't do what I want to do!! –  Peter Grill Nov 9 '12 at 0:14