ESS/AucTeX/Sweave integration

后端 未结 2 976
Happy的楠姐
Happy的楠姐 2021-02-09 09:11

I\'m using GNU/Linux distro (Arch, if that\'s relevant), Emacs v23.2.1, ESS v5.9 and AucTeX v11.86.

I want to setup AucTeX to recognize .Rnw fi

2条回答
  •  无人及你
    2021-02-09 09:31

    After brief and efficient Googling, I've found this link, and at first glance, everything seems OK, but pdf file gets garbled after Sweaving... So I tackled this problem another way around: when in doubt, go bash! I've shamelessly stolen error checking function from Dirk's Sweave bash script available here. Basically, this is a workaround: R CMD Sweave gets executed on .Rnw file, hence latex comes in, and pdflatex after that...

    I'll post a bash script that does the job for me. I must state that I'm not an advanced bash programmer, moreover I'm not even a programmer by vocation, so there's a great chance that this script can be optimized/written properly. Here goes:

    #!/bin/bash
    
    FILEBASE=${1%.*}
    FILEXT=${1##*.}
    FILEPATH=${1%/*}
    
    TEXFILE=$FILEBASE.tex
    PDFFILE=$FILEBASE.pdf
    
    # errorexit
    
    function errorexit () {
        echo "Error: $1"
        exit 1
    }
    
    # check if file exists
    if [ ! -f $1 ]; then
        errorexit "File $1 not found!"
    else
    # check for filename length
        if [ ${#1} -lt 1 ]; then
        errorexit "Need to specify argument file!"
        else
        # if filelength OK, check extension
        if [ $FILEXT != "Rnw" ]; then
            errorexit "You must pass Sweave (.Rnw) file!"
            # finally, run Sweave
        else
            cd $FILEPATH && R CMD Sweave $1
            # latex $TEXFILE
            pdflatex $TEXFILE
            # xdg-open $PDFFILE
        fi
        fi
    fi
    

    Then save/copy/move this script in any of echo $PATH folders (I keep mine in /usr/bin/), and make sure that it's named sweave, or choose whatever name you like, then put these lines in your .emacs file:

    (global-set-key (kbd "C-c s")
            (lambda ()
              (interactive)
              (shell-command (concat "sweave " buffer-file-name))))
    

    Of course, you can change keybinding to suite your needs, and be sure to change sweave with script name placed in /usr/bin/.

    Bare in mind that this is not an answer, but a workaround. If you have found a way to deal with AucTeX/ESS/Sweave integration, post it, and I'll give it a checkmark.

    Prior to this workaround, I had to do M-n s to Sweave, followed by C-c C-c which is default keybind in AucTeX for LaTeX file compilation. Produced file is erroneous, so I had to give it a try with bash. It works for me, if you have any suggestions, please let me know.

    Kind regards,
    aL3xa

    EDIT:
    Inserted cd $FILEPATH

提交回复
热议问题