6

In LaTeX it is customary to put the content of different chapters into different files which are then pulled into a master file using \include{FILENAME} statements. One can then use the \includeonly{SPEC} command to compile only those chapters which are specified by SPEC.

The TeX.SO question Typeset single chapter from book shows how to do this. Here comes my question:

Is it possible to tell AUCTeX to parse through the master file and compile each of the \include'd files to its own pdf file?

Say I have this:

\documentclass{book}

\begin{document}
\include{Chap1}
\include{Chap2}
\include{Chap3}
\end{document}

in a file called Master.tex. The function I'm looking for would then compile Chap1 to Master_Chap1.pdf, Chap2 to Master_Chap2.pdf and so on [By using \includeonly I'd assume].

  • AUCTeX does not provide any command for this purpose. You'll need to write your own. I fail to see a use case for this, though. Why do you want separate files per chapter, rather than just one PDF, if you're processing all chapters anyway? – lunaryorn Oct 6 '14 at 10:43
  • This is would be a very useful feature during the proofreading process. – elemakil Oct 6 '14 at 10:48
  • I can't see how proof-reading was eased by having the document split into per-chapter PDFs? You could just as well read through the entire PDF. – lunaryorn Oct 6 '14 at 10:50
  • 1
    Unless you send different chapters to different reviewers or at different stages of the proofreading process. – elemakil Oct 6 '14 at 10:53
  • 5
    LaTeX can parse its files better than any custom elisp. So you should look for a LaTeX package IMO. The subfiles LaTeX package should do what you are looking for. – Vamsi Oct 6 '14 at 17:41
3

EDIT: Totally didn't read all the comments on the answer. Looks like the subfiles LaTeX package might be a cleaner way. Leaving this here because options.

There is not a built-in way to do this. After a bit of elisp hacking, I came up with this solution:

(defun TeX-compile-all-chapters-from-master (cmd prefix)
  "Compile chapters using `CMD' with prefix `PREFIX'.

Each \include{foo} will be processed by running `CMD' formatted with args `PREFIX', `NAME' (foo in this example) and `TMP-FILE-NAME'."
  (let ((text (buffer-string)))
    (save-excursion
      (goto-char 0)
      (while (search-forward-regexp "\\(include\\){\\(.+\\)}" (buffer-size) t)
        (let* ((name (match-string 2))
               (tmp-file-name (concat "/tmp/tex-compile-all-" name))
               (incl-end (match-end 1)))
          (with-temp-file tmp-file-name
            (insert text)
            (goto-char incl-end)
            (insert "only"))
          (TeX-run-compile nil (format cmd prefix name tmp-file-name) nil)
          (delete-file tmp-file-name))))))

Example usage:

(TeX-compile-all-chapters-from-master "pdflatex -jobname %s_%s %s" "Master")

To bind this to a key, use define-key and either a lambda containing the above or apply-partially. Note to ELispers: Fairly sure my code isn't idiomatic in several areas ((insert text), tmp file name generation). Please help.

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.