How to use TeX comments to save yourself from always re-configuring TeXstudio

This posts documents an easy to miss feature supported by several LaTeX IDEs, TeXstudio in my case. They allow to cut short some otherwise tedious steps when switching between documents, by embedding a part of this variability into comments within the source files. This increases the portability of a document between TeX installations, increasing the chance that a simple Open and Compile of the source document succeeds on the first try, without first having to read the error log…

Root document

If you write longer documents, you better place your chapters into separate files, which you then \include from a main file. While editing such a chapter file, you can set up a root document which is translated by the TeX compiler (as only it provides the document class, custom commands and includes all used packages). To safe yourself from manual having to tell the IDE which file contains the document root, declare it in each chapter file:

% !TeX root = filename

So, if your main document is named mypaper.tex, place this in a chapter located within file tex/introduction.tex:

% !TeX root = ../mypaper.tex

This setting overrides the automatic root document detection in TeXstudio. The statement is ignored, however, if an explicit root document is set in the main menu under Options/Root Document.

TeX compiler

I often write documents which cannot be compiled with any TeX compiler (pdflatex being the most common one). For these cases, have a look at:

% !TeX program = pdflatex
% !TeX program = xetex
% !TeX program = lualatex

One of these statements defines the compiler to be used for the document. To be precise, it overrides the default compiler (used by command txs:///compile) which is used in the actions Build & View (F5) as well as Compile (F6).

Bibliography processor

Maybe you are in the train of switching your favourite BibTeX processing technology from the good old BibTeX program to the new and hot biber, but do not want to switch the setting for each document by hand to make them compile properly. Then simply add this line to your main document header:

% !TeX TXS-program:bibliography = txs:///biber

This is slightly less portable than the following declaration, which is also understood by TeXworks (in case you care):

% !BIB program = biber

Spell-checking

The following comment defines the language used for spell checking of the containing document. This overrides the global spellchecking settings. An appropriate dictionary has to be installed in your editor/IDE.

% !TeX spellcheck = en_US
% !TeX spellcheck = en_GB
% !TeX spellcheck = de_DE
% !TeX spellcheck = fr_FR

File encoding

I rarely use the following comment, but if you work on different machines or are not sure that an ASCII-only document might default to another encoding once you start adding des caractères spéciaux, safe yourself a headache by adding (in case you use UTF-8, of course):

% !TeX encoding = utf8

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.