Subject: Support for subfiles in `reftex-TeX-master-file'

From: Arash Esbati <[email-masked]>
Date: Fri, 10 Feb 2017 09:48:43 +0100

Hi all,

the function `reftex-TeX-master-file' in `reftex.el' has support for
subfiles package, but I think the regexp there does not match all
cases.  Please consider a main file "subfile-main.tex" and a sub-file
"subfile-sub1.tex" like these:

--8<---------------cut here---------------start------------->8---
\documentclass{article}
\usepackage{subfiles}
\begin{document}

\section{Section 1}
\label{sec:section-1}

\subfile{subfile-sub1.tex}
\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
--8<---------------cut here---------------end--------------->8---

--8<---------------cut here---------------start------------->8---
\documentclass[subfile-main.tex]{subfiles}
\begin{document}

\section{Subsection 1}
\label{sec:subsection-1}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
--8<---------------cut here---------------end--------------->8---

`reftex-TeX-master-file' fails to parse the first line and find out the
main file due to first part of it:

--8<---------------cut here---------------start------------->8---
(defun reftex-TeX-master-file ()
  ;; Return the name of the master file associated with the current buffer.
  ;; When AUCTeX is loaded, we will use it's more sophisticated method.
  ;; We also support the default TeX and LaTeX modes by checking for a
  ;; variable tex-main-file.
  (let
      ((master
        (cond
         ;; Test if we're in a subfile using the subfiles document
         ;; class, e.g., \documentclass[main.tex]{subfiles}.  It's
         ;; argument is the main file, however it's not really the
         ;; master file in `TeX-master-file' or `tex-main-file's
         ;; sense.  It should be used for references but not for
         ;; compilation, thus subfiles use a setting of
         ;; `TeX-master'/`tex-main-file' being themselves.
         ((save-excursion
            (goto-char (point-min))
            (re-search-forward
             "^[[:space:]]*\\\\documentclass\\[\\([[:word:].]+\\)\\]{subfiles}"
             nil t))
          (match-string-no-properties 1))
--8<---------------cut here---------------end--------------->8---

The regexp looks for [:word:] character class and ignores anything
else.  Using `-', `_' or digits in main file name will not work.  I
think the regexp can be reduced to

--8<---------------cut here---------------start------------->8---
            (re-search-forward
             "^[[:space:]]*\\\\documentclass\\[\\([^]]+\\)\\]{subfiles}"
             nil t))
--8<---------------cut here---------------end--------------->8---

as `reftex-TeX-master-file' checks if .tex extension is given or not.
With this change, one can use `C-c )' over all files in a project and
`C-c C-c' compiles a only subfile when issued in it.

Any comments?  I would prepare a patch after confirmation here.

Best, Arash