# How to use the standalone package with emacs

This is a follow-up question from A simpleton's guide to (...)TeX workflow with emacs.

The main answer stated that it is possible to use emacs together with standalone: the question is how?

The main problem is that when you try to compile a standalone file with C-c C-c, the commands are run on the TeX-master file, which you really want to define as the master file, to keep all of emacs, AUCTeX and RefTex's benefits.

-

There are two ways of achieving it. The first one uses standalone, the second one uses emacs only. See the end of this answer for a brief (and biased) comparison between the two methods. In both examples, I'll use this master file :

## Master file

% File main_standalone.tex
\documentclass{article}

\usepackage{standalone}

\newcommand{\blahblah}{Blahblah!}

\begin{document}

\input{input_standalone}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:


## Solution with standalone

% File input_standalone.tex
\documentclass{standalone}

\newcommand{\blahblah}{Blahblah!}

\begin{document}

Some text, \blahblah

\end{document}

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


If you try to compile it with C-c C-c, it will run (more or less)

%(latex) %t


which expands to (something like)

pdflatex main_standalone.tex


Obviously not what we want. There probably is a way to recover the real file name, but I couldn't find it. The best fallback solution I can propose is to select Other instead of LaTeX and type in pdflatex input_standalone.tex, but you will have to type it at every compilation.

Now if we could find a way to tell emacs how to find the real file name, we would get rid of that problem. However, the second solution shows that there's no need for such complicated things, so I wasn't really interested in searching further.

## Solution using emacs mechanics

% File input_standalone.tex
Some text, \blahblah

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


This time, we provide the file as a regular input file : no preamble, no documentclass. We don't need to use the package standalone, either.

Here is the emacs magic : instead of compiling with C-c C-c, we use C-c C-b. From the AUCTeX manual :

Command: TeX-command-buffer

(C-c C-b) Query the user for a command, and apply it to the contents of the current buffer. The buffer contents are written into the region file, after extracting the header and trailer from the master file. The command is then actually run on the region file. See above for details.

So our file is compiled and we can find the result in _region_.pdf.

## Comparison

There are several advantages, in my opinion, to the "emacs" option instead of standalone :

• it works out-of-the-box : there's no need to fix the problem we met before when trying to compile with the document class standalone;
• it's easy to adapt : let's say you have a compiling commands for LaTeX, one for XeLaTeX, one for a Make associated with a makefile, and two customized viewers. You will be prompted for a command with C-c C-b, as if you were using C-c C-c. On the other hand, assuming we could fix the previous problem, we'd need to create one standalone-equivalent to each of these commands if we wanted to use them on a standalone file;
• it doesn't need standalone : it's not a real argument because as far as I'm concerned, it is a pretty stable package. However, I feel my editor is more fit for this job than a package of the typesetting engine;
• from the texing point of view as well, it's less work : basically, when using standalone, you have to maintain one preamble per standalone file. With the "emacs" method, there is only one preamble to maintain.

The counterpart is that you miss the precompilation features of standalone, but there are other solutions available, for example with tikz external library.

-
Maybe this could be solved via latexmk too? Just guessing. –  N.N. Nov 30 '12 at 18:48
@N.N. I assume you mean the 2nd item of the comparison? That's absolutely true of course. And the 4th could be solved by putting the relevant parts of the preamble in an auxiliary file and inputting it in every standalone file. But in both cases, that's still unnecessary work. :) –  T. Verron Nov 30 '12 at 19:00