Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Can one define a bunch of macros, include them in a LaTeX source file main.tex and after issuing

$ latex main.tex

find the preamble of main.tex in a file, say main.nem ?

It is a requirement, that the source main.tex does not have to be touched by me. So, I receive a file main.tex, that already includes the magic macros, that I ask for in this question. And all I have to do, is to compile the file and have the preamble in a file. The burden for the author of main.tex shall not be greater than to include the file with the magic macros in them. It is not allowed, to ask the author to use a special documentclass, or have to write

\begin{filecontents}...

or some such into it. The only trace of me, using his file, is

\input{magic_macros.tex}

somewhere in his preamble.

I can do a somewhat analogous sort of thing with macros that I define, and that has proven to be extremely useful in my application domain.

A stripped down example of it is:

macros.tex:

\newwrite\nemWrite
\immediate\openout\nemWrite=\jobname.nem
\newtoks\nemToks
\newcommand{\toNem}[1]{%
    \nemToks={#1}
    \immediate\write\nemWrite{\the\nemToks}
}
\newcommand{\question}[1]{%
    \toNem{#1}%
    {#1}%
}

main.tex:

\documentclass{article}
\input{macros.tex}
\begin{document}
\question{%
    What does $2+2$ evaluates to?
}

I was hoping that there exists something similar for the preamble.

share|improve this question
1  
Welcome to TeX.sx! How is this different from a document class? –  Corentin Oct 24 '12 at 16:50
    
It's not very clear. You already have the preamble in another file. –  egreg Oct 24 '12 at 16:52
    
At TX.SX we don't usually place a signature or thanks since these are already given in your profile view and whenever you upvote an answer or accept one. –  azetina Oct 24 '12 at 16:56
1  
Follow-up Question: How to write the \documentclass into a file. –  Peter Grill Oct 25 '12 at 16:15
add comment

2 Answers

up vote 5 down vote accepted

You can use the standalone package to extract the preamble for you. Given a complete document as in the main.tex below, you run pdflatex on mainPreamble.tex which is defined as follows:

mainPreamble.tex:

\documentclass{article}
\usepackage[subpreambles=true]{standalone}

\begin{document}
    \input{main}
    \typeout{Sub preamble is now in \jobname.sta.}
    Sub preamble is now in \jobname.sta.
\end{document}

This extracts the preamble to the file mainPreamble.sta. Then in you other document where you need to use this preamble you simply use:

\UsePackage{main}

and the extracted preamble from main.tex is used. So, the MWE below yields:

enter image description here

which used the definition of question as originally defined in main.tex.

Note:

  • This required me to do some hacking in \UsePackage as I am not that familiar with this particular usage of the standalone package. So, perhaps that can be simplified, but I believe this does what you require.

main.tex:

%% This is main.tex
\documentclass{article}
%% This is the preamble of main.tex
\usepackage{amsmath}

\newcommand{\question}[1]{%
    \textbf{Question:}~#1%
}

\begin{document}
\question{%
    What does $2+2$ evaluates to?
}

Code:

Following is to be run only after compiling mainPreamble.tex as it needs mainPreamble.sta:

\documentclass{article}

\newcommand*{\UsePackage}[1]{%
    \let\standalonepreambles\relax
    \let\endstandalonepreambles\relax
    \newcommand{\subpreamble}[1]{}
    \let\endsubpreamble\relax
    \input{#1Preamble.sta}
}%

\UsePackage{main}

\begin{document}
    \question{%
        What does $1+1$ evaluates to?
    }
\end{document}
share|improve this answer
    
I am not fully understanding your requirements. But I believe that the \documentclass needs to be set in the main file that you are compiling -- perhaps you should post a separate question for that to confirm. My understanding of preamble is that it is everything after \documentclass and before \begin{document},and that is what this solution achieves. Perhaps this is best done via a script outside of LaTeX, as per @AndrewStacey's comment. –  Peter Grill Oct 25 '12 at 1:40
add comment

Something like this?

\documentclass{article}
\usepackage{filecontents}   
\begin{filecontents}{\jobname.hed}
% START YOUR HEADER HERE

\usepackage{lipsum}
\usepackage{amsmath}
% whatever you want in your header
\def\WWW{World}

% END YOUR HEADER HERE
\end{filecontents}
\input{\jobname.hed}
\begin{document}

Hello \WWW!

\lipsum

\end{document}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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