Here is the use case: you write a sample document with many demonstrations: plots and charts, tikz graphics, fancy tables, listing, algorithms, boxes, etc. This gallery display uses many unrelated packages; however they are all \usepackaged together in the preamble. This makes it difficult to cut and paste your sample document.

Ideally, a new package escalier would do it:

 \usepackage{escalier} % will be replaced by many `\usepackage`

 \section{TikZ Demo}

 ... \rowcolor{red}...

Implementation would be two pass: in the first, all commands declared within escalier, are ignored, but register a request to \usepackage in the second run.

EDIT To make this more specific: I would like to:

  1. Be able to write \usepackage where it is needed, not in the preamble
  2. I think it should be possible to implement this in a two pass compilation. In the first pass all used packages are moved collected. Their list is used in the second pass.
  3. I notice you would be getting errors in the first run from commands whose package is missing.
  4. I propose a new package, which gives a command to silence commands. In the first run, the silenced commands do nothing except for collecting packages. In the second run, these silence commands are activated.
  5. I doubt that there is anything in filecontents and standalone to help in dealing with this challenge: we want one document, but with self adjusting \usepackage, not a collection of PDF files created from distinct documents

Another edit: : Luckily, an expected and innocent TeXinician would in response throw all his engagements, and make this package per order.

The questions is, as it is usually the case here: what is a cost effective way of addressing the problem and the use case that suggests it. A full package would be nice, but also ideas, directions, etc. For example, an experienced writer could tell on how this was done in some of the TeX books describing packages.

More concretely, a decent answer would be in the form of a simple, low hanging fruit solution an environment that would do most of the stuff the package does, but not as nicely is an environment:

   \begin{tikzpicture} ... \end{tikzpicture}

(A solution via scripts might be feasible, but in my experience with scripts of these sort - they are never robust, and in general, more of a problem on their own right than a solution).

closed as too broad by Keks Dose, Schweinebacke, TeXnician, Torbjørn T., Troy Jul 3 '17 at 14:14

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • What is the question? Would filecontents and standalone being a viable replacement? – Andrew Swann Feb 15 '17 at 10:44
  • I will rephrase the question. – Yossi Gil Feb 15 '17 at 14:00
  • 7
    After the edit this still doesn't feel like a question to me, more a request for someone to do a significant amount of work writing a framework to do the job. (Presumably one doesn't need a two-pass TeX run strategy at all: you could use a script to go though the source and find the relevant lines, then to write a temporary file and do a single LaTeX pass on that.) – Joseph Wright Feb 15 '17 at 14:17
  • 2
    As far as I can tell, the package version of standalone (not the standalone class) does a lot of what you want. Perhaps you should play with that and see if it does what you want or most of what you want and then revise your question accordingly. – Alan Munn Feb 15 '17 at 17:34
  • 1
    At some extent, at a editor level, LyX solve this (e.g. writing \implies automatically addamsmath in the preamble, if necessary) but only do that with most usual packages. With thousands of packages on CTAN with many thousands of own commands, some of them defined by more than one package, seem unlikely load packages based on existing undefined commands. – Fran Feb 16 '17 at 5:40

Browse other questions tagged or ask your own question.