With Emacs and AUCTeX, it is as simple as highlighting the portion of the text you want to compile and then hitting C-c C-r
(or M-x TeX-command-region
).
The command tries to be 'smart' about it, so if you do C-c C-r RET C-c C-r RET
(i.e., run the same command on the same section twice) it will first compile, then, second, open a viewer for resultant PDF. (By default, called _region_.pdf
).
In order to selection the region, the easiest way is to move the cursor to the start (or end) or the desired region, hit C-SPC
(or M-x set-mark-command
) to set the mark, then move it to the end (or start) of the region. Then hit C-c C-r RET
to compile that selection.
Note that this also creates a file called _region_.tex
.
Consider this file (called, say, fulldoc.tex
):
\documentclass{article}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\lipsum[2]
% imagine trying to compile only this list (note you must do the whole environment)
\begin{itemize}
\item More lorem
\item More ipsum
\end{itemize}
% end of region being compiled
\lipsum[3]
\lipsum[4]
\end{document}
This would create the following file and run (in this case) pdflatex
on it:
\message{ !name(fulldoc.tex)}\documentclass{article}
\usepackage{lipsum}
\begin{document}
\message{ !name(fulldoc.tex) !offset(3) }
\begin{itemize}
\item More lorem
\item More ipsum
\end{itemize}
\message{ !name(fulldoc.tex) !offset(5) }
\end{document}
Doing C-r RET
again will open a viewer of _region_.pdf
Of course, you could also compile the _region_.tex
file from the shell, etc.
Note: if you mark a new region, it will do the same thing all over again, but use the same _region_
base filename.
Note As giordano notes, starting with version 11.89 of AUCTeX, it is now possible to compile a single section (not necessarily a single '\section
') of your file. From the manual:
Command: LaTeX-command-section
(C-c C-z
) Query the user for a command, and apply it to the current section (or part, chapter, subsection, paragraph, or
subparagraph). What makes the current section is determined by
LaTeX-command-section-level
which can be enlarged/shrunken using
LaTeX-command-section-change-level
(C-c M-z
). The given numeric prefix
arg is added to the current value of LaTeX-command-section-level
. By
default, LaTeX-command-section-level
is initialized with the current
document’s LaTeX-largest-level
. 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
TeX-command-region
for details.
In essence, what AUCTeX does is the same as in my example above. The advantage is that you don't need to explicitly mark a portion of text that you want to compile.
standalone
class to create snippets. But maybe I'm misunderstanding the question.