Hi Ikumi (et al),

On Mon, Mar 30, 2026 at 20:47 (+0900), Ikumi Keita wrote:

>>>>>> Jim <[email protected]> writes:
>> Again, thanks for your thoughts.  In the interest of improving AUCTeX a
>> bit, I'd be happy to think about how to do this for ConTeXt, but I want to
>> understand what must happen with LaTeX, so that any proposed changes won't
>> break anything there.

> Thanks for your thoughts on this.

>> I think there are 6 cases of interest (but please correct me right away if
>> this is incorrect):

>> (1) C-c C-c and TeX-master = t
>> (2) C-c C-r and TeX-master = t
>> (3) C-c C-b and TeX-master = t

>> (4) C-c C-c and TeX-master = <some file>
>> (5) C-c C-r and TeX-master = <some file>
>> (6) C-c C-b and TeX-master = <some file>

>> [[ I realize there are other values of TeX-master, but according to mu
>> understanding TeX-master's doc string, the other values attempt to set
>> TeX-master to a file name and then it is really the "TeX-master = <some
> file> " case, or if it can't get a master file, presumably we end up in
>> the "TeX-master = t" case.  Does that seem correct? ]]

> I think so.

OK, let's assume that is so and carry on for now.


>> What should AUCTeX do in these case?  Please comment on these.
>> (I fear you are going to tell me that because of bibtex of other things my
>> speculation is too simple...)

>> (1) Compile the current file as is, where is.

>> (2) Hunt in the current buffer for the header and trailer parts,

>> (3) Hunt in the current buffer for the header and trailer parts,

> OK for these three cases.

Great.

>> The TeX-master = <some file> case is a bit trickier, I think.
>> But let me try.

>> (4) compile TeX-master in its directory.  No _region_.tex file created.

> OK

So far, so good!


>> (5) What I think should happen:
>> Hunt in the master file for the header and trailer parts;
>> create _region_.tex (in the master file's directory) with
>> header part
>> region selected in current buffer, *excluding its header and trailer*
>>                 (which LaTeX sub-files should not have anyway?)

> Basically, yes. (When the document uses "standalone" package, the sub
> files can have their own \documentclass, \begin{document} and
> \end{document}.  But this is exceptional case and I think we can ignore
> such documents in our current discussion.)

Maybe, we can, but I don't want to suggest anything that would break the
processing for this case.

>> trailer part
>> and then compile _region_.tex in the master files' directory.

>> What actually happens:
>> If the region includes a header or trailer 
>>         (Q: should this ever happen in LaTeX when we are editing a file
>> with TeX-master = <some file> ?)

> See above.

>>     - the header from the master file is included
>>     - the trailer from the buffer is included
>>     - the trailer from the master file is *excluded*

> These aren't right.

Do you mean "this is not what happens", or do you mean "this is not what
should happen"?

> - If the region includes a header, it is used as-is and the header from
> the master is ignored.  If not, the header from the master is prepended
> to the selected region.

I have tested the LaTeX standalone case, and what you said is not what
happens for me.

Here are my files:

latex-standalone-main.tex:
---------- cut here -------------
\documentclass{article}
\usepackage{standalone}
\usepackage{amsmath} % Load necessary packages in the main file
\begin{document}
\section{My Document}
Here is my first picture:
\includestandalone{latex-standalone-subfile}
\end{document}
---------- cut here -------------

latex-standalone-subfile.tex
---------- cut here -------------
     \documentclass{standalone} % This is in the subfile
     \usepackage{amsmath} % This is in the subfile
      \begin{document}
% Content of the picture/figure
An equation $a+b=c$.
      \end{document} % This is in the subfile
---------- cut here -------------

I have indented and commented the LaTeX control sequences in the subfile so
we can see (I hope!) where each line comes from.

Now I edit the subfile, and set TeX-master to the main file.  And I mark
the whole buffer, and then C-c C-r.  This is the _region_.tex file that
gets compiled:

_region_.tex after marking the whole buffer and doing C-c C-r:
---------- cut here -------------
\message{ !name(latex-standalone-main.tex)}\documentclass{article}
\usepackage{standalone}
\usepackage{amsmath} % Load necessary packages in the main file
\begin{document}

\message{ !name(latex-standalone-subfile.tex) !offset(-4) }

% Content of the picture/figure
An equation $a+b=c$.
      \end{document} % This is in the subfile

\message{ !name(latex-standalone-main.tex) !offset(-12) }
---------- cut here -------------

So to me, the header of the _region_.tex file certainly came from the main
file, but the trailer comes from the sub-file.

So, as I was saying, I'm not sure whether you meant "this is not what
happens" or "this is not what is supposed to happen", but if you meant
"this is not what happens" I think I must (very respectfully) disagree.

> - If the region includes a trailer, it is used as-is and the trailer
> from the master is ignored.

I agree that is what happens.

> If not, the trailer from the master is appended to the selected region.
> See the following portions of `TeX-region-create': 
>> ,----
>>          ;; We search for the header from the master file, if it is
>>          ;; not present in the region.
>>          (header (if (string-match header-end region)
>>                      ""
>>                    (save-excursion
>>                      (save-restriction
>>                        (set-buffer master-buffer)

I see the code, I read the code, and yet in my example here, would you not
agree that what I see disagrees?

(Maybe someone would be so kind as to repeat my experiment, in case there
is something broken with my emacs ?)

I had previously done similar experiments with ConTeXt files, but I thought
since most people reading this (2 out of 3? ;-) are not ConTeXt users, I'd
dip my toes into the LaTeX pool.

> `----
> ,----
>>          ;; We search for the trailer from the master file, if it is
>>          ;; not present in the region.
>>          (trailer-offset 0)
>>          (trailer (if (string-match trailer-start region)
>>                       ""
>>                     (save-excursion
>>                       (save-restriction
>>                         (set-buffer master-buffer)
> `----

>> (6) What I think should happen:
>> Hunt in the master file for the header and trailer parts,
>> create _region_.tex with
>> header part
>> visible part of buffer (in case the user narrowed-to-region),
>>                 *excluding* its header and trailer, if it has them
>> trailer part
>> and then compile _region_.tex.

>> What actually happens:

> The same with (5).

>> I don't understand why the current code base is preferring the trailer from
>> the buffer in cases (5) and (6).

> As I wrote above, the header and the trailer are treated symmetrically
> actually.

Not for me.  :-(

>> Q: Can someone explain this to me?

>> If that is a bug, then maybe it is worth fixing, if for no other reason
>> than to facilitate Doing The Right Thing for ConTeXt.

> I'd say that it's a bug considering that preamble-like stuff in subfiles
> is common in ConTeXt documents.

Certainly.  And before attempting to wrap my mind around what needs to be
done for ConTeXt, I really want to understand the current code.  But the
current code doesn't seem to agree with the comments.


Questions:
(1) Is the code Doing The Right Thing for LaTeX?
(1a) If not, should the code be fixed, or the comment changed?

Bonus question:
(B) Would it be nicer if the first \message had a newline after it, so that
    the first line of the included header was more obvious?

    I.e., Replace
        (insert first-line
                    "\\message{ !name(" master-name ")}"
    with
        (insert first-line
                    "\\message{ !name(" master-name ")}\n"

Thanks for any thoughts on this.

                                Jim

Reply via email to