|
| 1 | +# How To: Use Tectonic with AucTeX |
| 2 | + |
| 3 | +This section is a guide aimed at [GNU Emacs](https://www.gnu.org/software/emacs/) users for setting up [AucTeX](https://www.gnu.org/software/auctex/) with Tectonic as the TeX/LaTeX distribution. |
| 4 | + |
| 5 | +## Basic Prerequisites |
| 6 | + |
| 7 | +To follow this section you will need Tectonic and GNU Emacs installed on your system. Additionally, you will require the AucTeX emacs package to be installed before |
| 8 | +following along. |
| 9 | + |
| 10 | +> Note: This section makes use of the V2 tectonic CLI, invoked using the `nextonic` command. |
| 11 | +
|
| 12 | +## Setup |
| 13 | + |
| 14 | +All the code displayed in this section should go into your `init.el` file (or `config.el` if you are using [Doomemacs](https://github.com/doomemacs/)). |
| 15 | + |
| 16 | +First, load the AucTeX package. |
| 17 | + |
| 18 | +```lisp |
| 19 | +(require 'latex) |
| 20 | +``` |
| 21 | + |
| 22 | +You will need to set the default TeX engine AucTeX uses to figure out the build commands to use Tectonic instead of traditional TeX distributions. Therefore we have to modify the `TeX-engine-alist` varible. |
| 23 | +* The first element of the list is the symbol that AucTeX recognizes. |
| 24 | +* The second element is a string with the name of the TeX distribution. |
| 25 | +* The third element is the shell command for compiling plain TeX documents. |
| 26 | +* The fourth element is the shell command for compiling LaTeX documents. Here we are assuming the user is using a Tectonic project (generated using `nextonic new <proj-name>`). |
| 27 | +* The last element is the shell command for compiling ConTeXt documents, left unconfigured for now. |
| 28 | + |
| 29 | +```lisp |
| 30 | +(setq TeX-engine-alist '((default |
| 31 | + "Tectonic" |
| 32 | + "nextonic compile -f plain %T" |
| 33 | + "nextonic watch" |
| 34 | + nil))) |
| 35 | +``` |
| 36 | + |
| 37 | +Next, modify the `LaTeX-command-style` so that AucTex doesn't add extra options to it that Tectonic does not recognize. We simply set it to the `%(latex)` expansion (from `TeX-expand-list-builtin`), removing any other extra options. |
| 38 | + |
| 39 | +```lisp |
| 40 | +(setq LaTeX-command-style '(("" "%(latex)"))) |
| 41 | +``` |
| 42 | + |
| 43 | +We need to set the `TeX-check-TeX` variable to `nil` since AucTeX will try to find a traditional distibution like `TeXLive` or others, and will fail since Tectonic doesn't meet it's criteria. |
| 44 | + |
| 45 | +Additionally, we should also set `TeX-process-asynchronous` to `t`, so that running Tectonic in watch mode doesn't hang up Emacs. |
| 46 | + |
| 47 | +We'll also just ensure that the `TeX-engine` is set to `default`. |
| 48 | + |
| 49 | +```lisp |
| 50 | +(setq TeX-process-asynchronous t |
| 51 | + TeX-check-TeX nil |
| 52 | + TeX-engine 'default) |
| 53 | +``` |
| 54 | + |
| 55 | +Finally, modify the `TeX-command-list` to use the appropriate commands, and not pass in extra metadata and options to Tectonic which cause it to error out. This needs to be done in place. |
| 56 | + |
| 57 | +```lisp |
| 58 | +(let ((tex-list (assoc "TeX" TeX-command-list)) |
| 59 | + (latex-list (assoc "LaTeX" TeX-command-list))) |
| 60 | + (setf (cadr tex-list) "%(tex)" |
| 61 | + (cadr latex-list) "%l")) |
| 62 | +``` |
| 63 | + |
| 64 | +And that is all! You should now be able to |
| 65 | +1. Compile plain TeX files. |
| 66 | +2. Build Tectonic LaTeX projects in watch mode. |
| 67 | + |
| 68 | +## Additional Configuration and Usage Suggestions |
| 69 | + |
| 70 | +### Compile LaTeX outside a Tectonic project |
| 71 | + |
| 72 | +To do this, you can simply invoke `M-x TeX-command-master`, and the select the `Other` option, passing in the compile command `nextonic compile -f latex <name of file>`. |
| 73 | +> **Caution**: Compiling a document with multiple LaTeX files in this manner isn't extensively tested, as using a Tectonic project is the better way in that case. Any bug reports are welcome. |
| 74 | +
|
| 75 | +### Live PDF Preview in Tectonic projects |
| 76 | + |
| 77 | +AucTeX expects the output PDF after compiling to be in the same directory as the input file. So it will error out when that is not the case, since Tectonic places the output in a build directory. |
| 78 | + |
| 79 | +This behavior can be controlled by using the `TeX-output-dir` variable on a per porject basis. |
| 80 | + |
| 81 | +This configuration assumes you are using `project.el`, although porting this code to `projectile.el` should be trivial. |
| 82 | + |
| 83 | +```lisp |
| 84 | +(add-hook 'after-change-major-mode-hook |
| 85 | + (lambda () |
| 86 | + (when-let ((project (project-current)) |
| 87 | + (proot (project-root project))) |
| 88 | + (when (file-exists-p (expand-file-name "Tectonic.toml" proot)) |
| 89 | + (setq-local TeX-output-dir (expand-file-name "build/index" proot)))))) |
| 90 | +``` |
| 91 | + |
| 92 | +We are basically looking for `Tectonic.toml` file in the project root, and if it exists, setting the `TeX-output-dir` to the appropriate path to the build directory. You may replace the `"build/index"` path to wherever your PDF file is placed after it is generated by Tectonic. |
0 commit comments