1

In ShareLaTeX when I write a single document with the preamble in it, then no matter which is the main document I just press compile and the output is produced.

On the other hand when I place the preamble in a separate file and use the \input command then I have to set that document as the main one in order to compile it successfully.

I can't understand why is that necessary. Is there a way to avoid changing the main document every time, even though I am using a separate preamble file?

6
  • The 'that' in your second paragraph has an unclear antecedent. Is it the file being \input-ed or the main file that has the \input command in it? And I have to wonder whether Share LaTeX has a help section because I would imagine that many sophisticated editors -- even Emacs! -- wants you to set the 'main' document if you are going to compile from an included/subsidiary file. – jon Apr 6 '16 at 3:40
  • @jon the \input command is in the document that the preamble will be imported. I don't know if I understood correct what you re saying so feel free to ask me to clarify further. This question is more about why in the first case I don't have to set a main document yet in the second I have. – Adam Apr 6 '16 at 14:34
  • I know where the \input command would have to be: but from which document are you compiling? I also suggest not using \input in this way, but \usepackage. The structure is \documentclass{article} \usepackage{mypreamble} \begin{document} text \end{document}. If you are doing \input{mypreamble} \begin{document} text \end{document}, then the best advice is: don't. If you are trying to compile from 'within' mypreamble, then I'd assume you'd need to inform any reasonable editor that you are not in the 'main' document. – jon Apr 6 '16 at 23:33
  • Also: if you want to separate text and 'code', the best structure is to create a 'shell' file and a 'content' file. So: \documentclass{article} \usepackage{mypreamble} \begin{document} \input{content} \end{document} where content.tex is the file that contains the text that will be typeself. Again, however, I think content.tex will (and should) have a flag marking which file is the 'master'. – jon Apr 6 '16 at 23:44
  • @jon I don't compile from the preamble. I found in the ShareLaTeX's documentation that creating a package with the preamble (as you propose yourself) is the best solution so I started using this one and indeed is great but I am just trying to understand why it doesn't compile in the second case. – Adam Apr 6 '16 at 23:54
2

If you to separate your preambles from the 'body' of their document, you might think this is a good idea: create a preamble (say, preamble.tex) and their main file (say, main.tex). For example,

% preamble.tex
\documentclass{article}
\usepackage{enumitem}
\newcommand\myname{XXXX YYYYY}

and then use it in:

% main.tex
\input{preamble}
\begin{document}

My name is \myname.

\end{document}

My advice is: do not do this. I understand the temptation to separate out the two 'parts', especially if it is a long document, etc. However, if you want to separate the parts, there is a better way to do it:

% main.tex
\documentclass{article}
\usepackage{enumitem}
\newcommand*{\myname}{XXXX YYYY}
% ... other preamble stuff
\begin{document}

\input{content}
% or \include{content}, though there are differences between the two;
% see: https://tex.stackexchange.com/q/246/8528

\end{document}

And then you do your 'writing' in content.tex

% content.tex
My name is \myname.

Note that you might have to 'flag' this as a subsidiary file and point it to the 'master'. Every editor probably does this differently. In Emacs it is easy. At the bottom of your file, you put:

%%% Local Variables: 
%%% mode: latex  
%%% TeX-master: "main"  
%%% End: 

(Really, only the %%% TeX-master: "main" is doing the relevant work here.)

Although I cannot speak about ShareLaTeX, which I have never used, it is entirely possible that if it cannot see the line \documentclass it will assume that it is not actually a file that can be processed by LaTeX. (This would be another reason to avoid the first method I described above and which I strongly encourage people to avoid.) Presumably the website allows you to mark 'main' and 'subsidiary' files in the way (e.g.) Emacs does, but I do not know how it does....

This method also allows you to rely on a common preamble. If you are going to do this, then it is best to name it with the .sty extension and load it via \usepackage. For more information on how to do this, see:

4
  • Thank you for your answer and your suggestion is great although I placed my preamble in a .sty file and I use it with a usepackage command, but my main question is why I need to set a main document when I use the input command? When I compile your example without setting it as a main document then everything works fine so from what I see is that you must have the documentclass and the begin{document}/end{document}commands in the file that you compile. If that is the case add it to your answer to be full. – Adam Apr 7 '16 at 15:04
  • On a sidenote do you believe that is better to use your solution for the input or the solution when the the preamble is in a .sty file? – Adam Apr 7 '16 at 15:05
  • If you have a 'common preamble', then i'd ues the main.tex file as described above in the 'better way' and just use \usepackage{mypreamble}. So: \documentclass{article} \usepackage{mypreamble} \begin{document} \input{content} \end{document}. That means you have three files: preamble.sty, main.tex, and content.tex. – jon Apr 7 '16 at 19:44
  • Thank you for the clarification. I only want to get the preamble out of the way so the input command is not really necessary for me, but it is still a great way. – Adam Apr 7 '16 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.