23

A friend of mine asked me whether TeXShop is able to compile just a snippet of a .tex file or not. More generally, one could ask which TeX editors are able to compile just a snippet of a .tex file? This question:

Update

More precisely, I'm looking for pure TeX editors solutions that let the user select a region of a .tex file and compile just this region from the current .tex file (with its class, packages, personal macros, more generally its preamble). Just the names of such editors would be nice, how it is done would be the icing on the cake.

  • WinEdt certainly has or had this concept, but one has to start by deciding what the 'master' document for a snippet is, which itself sounds tricky. – Joseph Wright Nov 6 '16 at 17:59
  • 5
    Emacs + AUCTeX is available also on Windows. – giordano Nov 6 '16 at 18:02
  • I'm not quite sure what you're asking. For the Mac, there is LaTeXit which is effectively a snippet editor. And of course you can use the standalone class to create snippets. But maybe I'm misunderstanding the question. – Alan Munn Nov 6 '16 at 18:09
  • 2
    any editor that has programmable customisation should be able to do this, basically copy the preamble of the current document, the current region and \end{document} into a temporary file and run latex. – David Carlisle Nov 6 '16 at 18:34
  • 1
    Are you asking for people simply to name editors where it is possible, or how it is done in the relevant editor? (It is pretty easy with Emacs + AUCTeX....) – jon Nov 6 '16 at 19:12
28

Amazingly enough, TeXShop does in fact have such a facility. The problem is that long-time users of it like myself are probably unaware of it.

It can be found under the Edit Menu -> Experiment

This menu will open a window which you can copy a snippet of the currently open document. When you press the Typeset button on the window, it will show the result in a small preview window:

TeXShop Experiment sample

  • 1
    Nice to having excavated this feature! Nevertheless, too bad it requires a copy-paste: I guess it should be possible for TeXShop to do the same job just by selecting the region and running Edit Menu → Experiment. I'll post a feature request to Richard Koch. – Denis Bitouzé Nov 7 '16 at 6:12
  • Richard Koch nicely accepted my feature request: many thanks to him! :) No need to copy paste anymore (when TeXShop update will be available, starting tomorrow): if you select text and then go to Edit Menu → Experiment, the selected text will be automatically copied in the Experiment (text) window, the layout of which can be controlled with "Typeset" button which brings up the typeset window. If nothing is selected when you go to Edit Menu → Experiment, you'll get the previous contents (or an empty page if this is its first use in the session). – Denis Bitouzé Nov 8 '16 at 13:32
  • @DenisBitouzé Great! Yes Dick is extremely responsive to user suggestions. – Alan Munn Nov 8 '16 at 17:02
22

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.

  • 2
    It's worth mentioning that you can also automatically run LaTeX on the current section without even selecting it, using C-c C-z: gnu.org/software/auctex/manual/auctex/… – giordano Nov 7 '16 at 9:21
  • Whoa! Never even noticed that option before! Will edit. – jon Nov 7 '16 at 15:35
12

Kile has this feature. It is called QuickPreview. I've never used it before, but I tested it in a minimal example and it seems to work.

I tested with my answer to another question:

\documentclass[tikz,border=10pt]{standalone}
\usepackage{circuitikz}
\standaloneenv{circuitikz}
\usetikzlibrary{fit}
\begin{document}
\tikzset{
  pics/multimeter/.style={
    code={
      \tikzset{
        multi meter/.cd,
        #1,
      }
      \begin{scope}[]
        \coordinate (\multimetername-o) at (0,0);
        \foreach \i/\j/\k/\l in {A/o/south/A,V/o/north/V,TL/mA.west/east/HI,BL/mV.west/east/HI,TR/mA.east/west/LO,BR/mV.east/west/LO} \node (\multimetername-m\i) [multi meter/label, anchor=\k] at (\multimetername-\j) {\l};
        \foreach \i in {TL.west,TR.east,BL.south,BR.south} \node [circle, fill, inner sep=1.25pt] at (\multimetername-m\i) {};
        \node [draw, fit=(\multimetername-mTR) (\multimetername-mBL)] {};
      \end{scope}
    }
  },
  multi meter/.search also={/tikz,/circuitikz},
  multi meter/.cd,
  label/.style={font=\sffamily, align=center, transform shape, pic actions},
  name/.store in=\multimetername,
  name=multimeter,
}
\begin{circuitikz}[american, cute inductors]
  \draw
  (0,0) node[anchor = east] {C} coordinate (C)
  (0,2) node[anchor = east] {B} coordinate (B)
  (0,4) node[anchor = east] {A} coordinate (A)
  pic [rotate=-90] at (3,0) {multimeter={name=M}}

  (A) to [short] ++(2,0)
  (B) to [short] ++(6,0)
  (C) to [short] ++(2,0)
  (1.5,0) to[short,*-] ++(0,1.2)
  to[short] ++(1,0)
  -| (M-mTL.west)
  (M-mBR.south) to[short] ++(-1.25,0) coordinate (c) to [short,-*] (B -| c)
  ;
\end{circuitikz}
\end{document}

Highlighting the contents of the document environment (but not the environment itself or the preamble) and choosing Build > QuickPreview > Selection typeset the selection.

As far as I can ascertain, the way this works is that Kile tries to identify the preamble by looking for the first line containing \documentclass for the start of the preamble and the \begin{document} line to figure out where the preamble ends. It copies the preamble to a file in a temporary directory called preview.tex, along with the selection, wrapped in a document environment. It then typesets this document and displays the result, if successful.

I tested the QuickPreview > Selection option. Other QuickPreview options include Environment, Subdocument and Maths group.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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