2

When writing a document, I like to use the following format.

%Settings for document
%Packages I need
\newcommand{\insertdocument}{
%Everything that would ordinarily be put between \begin{document} and \end{document}
}

\begin{document}
\insertdocument
\end{document}

There's something that appeals to me about producing a complicated document with only 15 characters between \begin{document} and \end{document} (although I could just do \q and do it in 2 characters, but that's beside the point).

Does this method of creating a document have any advantages or disadvantages in comparison to the standard way of making a document in terms of compilation speed, ease of use, or other measures?

Arcturus
  • 558
  • 3
    You have \include and \input for such jobs. –  Aug 30 '15 at 02:43
  • Below there are many solutions showing disadvantages. By the other hand, I don't see any advantage in what you want to do. – Sigur Aug 30 '15 at 09:19

2 Answers2

7

Disadvantages:

  • \insertdocument for long documents would be an extremely long macro, which may have repercussions on memory use. (minor issue)

  • Defining a macro freezes category codes of the input, so all features involve verbatim and similar will fail badly if attempted:

    \documentclass{article}
    \newcommand\insertdocument{%
      \begin{verbatim}
      real x;
      x = 1.0;
      \end{verbatim}
    }
    \begin{document}
    \insertdocument
    \end{document}
    

A commenter above recommended \include and \input. To achieve that in the same style as you're asking, I recommend the following usage:

\begin{filecontents}{\jobname-document.tex}
\section{Introduction}
Hello
\section{Conclusion}
The end.
\end{filecontents}

\documentclass{article}
\begin{document}
\input{\jobname-document}
\end{document}

Or for a multi-file book, say:

\begin{filecontents}{\jobname-intro.tex}
\chapter{Introduction}
Hello
\end{filecontents}

\begin{filecontents}{\jobname-concl.tex}
\chapter{Conclusion}
The End.
\end{filecontents}

\documentclass{book}
\begin{document}
\include{\jobname-intro}
\include{\jobname-concl}
\end{document}

But you'll probably rather break things up into separate files by that point.

4

Perhaps one of the biggest disadvantage would be around category code changes, since macro replacement text have their category codes fixed at the time of definition. This disadvantage is not part of most people average document, so might not be a game-changer.

One thing that's probably neither good nor bad, probably just annoying: If you make any macro (re)definitions that takes an argument(s), within the document environment, you will necessarily have to double the #s:

\documentclass{article}

\newcommand{\abc}[1]{abc-#1}
\newcommand{\insertdocument}{%
  \abc{def} \par
  \renewcommand{\abc}[1]{ghi-##1}%
  \abc{jkl}
}
\begin{document}
\insertdocument
\end{document}
Werner
  • 603,163