4

I need to make many images wich uses the same preamble. To make those images, I use the document class standalone. My aim would be to create a file preamble.tex and to include it in the other files, like this :

%preamble.tex
\usepackage[frenchb]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{graphicx}
\usepackage{tikz,pgf}

%drawing tools

and

%image.tex
\documentclass{standalone}

\include{preamble}

\begin{document}
\begin{tikzpicture}
%draw my picture
\end{tikzpicture}
\end{document}

But it doesn't seem to work for me.

patxiku
  • 43
  • 4
  • 2
    Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. 'Doesn't work' is not terribly informative. Do you get an error? Does your computer explode? Does it compile but with blank output? You probably want \input rather than \include, by the way. – cfr Oct 21 '14 at 16:04
  • Actually, it works fine for me with \input rather than \include. – cfr Oct 21 '14 at 16:09

1 Answers1

7

You should use \input rather than \include for cases such as this where it is not a whole part of a document which might be compiled alone (e.g. a chapter or section or something). See this question for details. Note, in particular, that you cannot use \include in the preamble.

\documentclass{standalone}

\input{preamble}

\begin{document}
  \begin{tikzpicture}
    \node {ABC};
  \end{tikzpicture}
\end{document}

Alternatively, do as David Carlisle recommends and make the preamble into a package:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mypreamble}

\usepackage[frenchb]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}    
\usepackage{graphicx}
\usepackage{tikz}% loads pgf

\endinput

And then just load it as with any other package:

\documentclass{standalone}

\usepackage{mypreamble}

\begin{document}
  \begin{tikzpicture}
    \node {ABC};
  \end{tikzpicture}
\end{document}

I don't really understand why this is 'more logical' than using \input but, then, I am far from expert.

cfr
  • 198,882
  • 2
    even better would be to rename preamble.tex to mysetup.sty and use \usepackage{mysetup} instead of \input (well actually it makes no difference to TeX but it's logically better) – David Carlisle Oct 21 '14 at 16:40
  • @DavidCarlisle Why? I'm interested partly because I tend to use \input in this kind of case i.e. where what I'm feeding in is a fairly rigid list of packages and configurations intended for use in a single project involving a number of documents needing a consistent look. (For example, all sets of slides or all draft handouts for a module.) If it is generic enough that I want it in ~/texmf, then I tend to make it a package or a .cfg or whatever. If it is staying in the project's directories, I tend not to. What is the proper way to draw the distinction? – cfr Oct 21 '14 at 17:55
  • I think it's best to view the preamble as essentially an "anonymous package" where a small amount of local setup can be placed, so once that becomes big enough to be a a separate file a package is the natural choice. it's just a name really, you could use \makeatletter\input{longtable.sty} there's no difference really except for \ProvidesPackage which is just a message feature. So I'd always use \usepackage in the preamble and \input or \include in the main document, for document content rather than macros. – David Carlisle Oct 21 '14 at 18:06
  • @DavidCarlisle Don't you find it confusing to have packages which are not in a texmf tree? – cfr Oct 21 '14 at 18:11
  • I have copies of longtable, color, blkarray, .... not in the main tree for some reason, a few more don't seem odd at all, no. If the configuration is applicable to several of your documents put it in the local texmf if it is just for one put it with the document. – David Carlisle Oct 21 '14 at 18:27