sagemath / sage-shell-mode

Emacs front end for SageMath
GNU General Public License v3.0
102 stars 16 forks source link

Suggestion : add the possibility of making sage-shell-mode work on AUCTeX's "TeX-master" #31

Open EmmanuelCharpentier opened 7 years ago

EmmanuelCharpentier commented 7 years ago

This is a bit of a specialized use-case, so it might not be of help to a great many people, but nevertheless...

I'm a biostatistician, and I work often wit .Rnw files, that merge R "chunks" in \LaTeX source. Such files are pre-processed by an R utility ("Sweave, or, in my case, knitr)that execute the code (caching is possible and damn useful !) and create a \LaTeX file, which gets compiled to get the final document, that comprises the original text and statistical tables/figures/inline insertions computed by R. In short ASCII art :

+-------+ knitr  +-------+ xxxtex  +-------+
|foo.Rnw+------->|foo.tex+-------->|foo.pdf|
+-------+        +-------+         +-------+

Setting (usually in a Local Variables: directive at the end of the file) the TeX-master AUCTeX variable as a string containing the file name sans extension ("foo", in our case) and positioning an option I can't retrieve at the moment, but is accessible through the "LaTeX" menu of AUCTeX, instructs AUCTeX to execute its commands not on the buffer file (foo.Rnw), but on the TeX-master file (foo.tex). This alows to use a .Rnw fiole (almost) transparently.

In contrast, requiring a compilation or a Sage execution from the .Rnw buffer tries to use the .Rnw file, which, of course, fails. Things work when one uses the version of commands that prompt for a file name : one can edit the proposed name (foo.Rnw) as the name of the master file (foo.tex), ant things work well.

Could sage_shell_mode use the AUCTeX mechanism to determine if it must wotrk on the current buffer or the master file ?

stakemori commented 7 years ago

Sorry for the late reply (I overlooked the notification). And thank you for the suggestion. But I am not sure if I understand the issue correctly since I am not familiar with Sweave nor knitr.

In contrast, requiring a compilation or a Sage execution from the .Rnw buffer tries to use the .Rnw file, which, of course, fails.

I do not quite understand this sentence. What Emacs command do you use for pre-processing? How is it related to sage-shell-mode?

Could sage_shell_mode use the AUCTeX mechanism to determine if it must wotrk on the current buffer or the master file ?

I guess it would be possible to determine if pre-processing is necessary by computing the hash value of R codes in the .Rnw buffer. Perhaps ess or knitr itself might have such a feature.

EmmanuelCharpentier commented 7 years ago

I am much more later that you ever were in answering you : I can't but apologize, pleading attenuating circumstances in "RealLife(TM)".

I guess it would be possible to determine if pre-processing is necessary by computing the hash value of R codes in the .Rnw buffer. Perhaps ess or knitr itself might have such a feature.

Indeed, knitr does that (it fact, it implements the management of R chunk interdependencies, and run only modified chunks or chunks depending on modified chunks, à la make).

What I mean is that the "correct" core processes are :

That's what sage_shell_mode does presently.

The idea is to never edit foo.tex directly ; when working on a .Rnw buffer, the AUCTeX functions you wrote should knit it to update the master file and work on this master file.

A further refinement might be to search somehow if any sage "chunk" (including inlines...) has changed and do not run Sage if the current foo.sagetex.sage is identical to the present one (modulo the timestamps inserted by Sagetex). But that's probably non-trivial to implement correctly.

Is that clearer ?

stakemori commented 7 years ago

Thank you for the explanation. I think I understand.

As a first attempt, I have just improved sage-shell-sagetex:run-latex-and-load-file and related commands. Those commands now use TeX-master as a default file name if it is set.

To run sage-shell-sagetex:run-latex-and-load-file after ess-swv-knit, we need something like callbacks for ess-swv-knit. I'm not sure if such a feature exists for ess-swv-knit. I asked here.