作者:mobiledu2502852923 | 来源:互联网 | 2022-12-24 21:14
ImattemptingtocreateabuildsystemforknitrSweaveinSublimeText2.Mycurrent,simple(and
I'm attempting to create a build system for knitr/Sweave in Sublime Text 2. My current, simple (and working) build system is as follows:
我正在尝试在Sublime Text 2中为knitr / Sweave创建一个构建系统。我当前的简单(和工作)构建系统如下:
{
"cmd": ["bash", "-c", "/usr/bin/R64 CMD Sweave '${file_name}' && pdflatex '${file_base_name}.tex' -interaction=nonstopmode -synctex=1 %S -f -pdf && /Applications/Skim.app/Contents/MacOS/Skim '${file_base_name}.pdf'"],
"path": "$PATH:/usr/texbin:/usr/local/bin",
"selector": "text.tex.latex.sweave","shell":false,
"file_regex": "^(...*?):([0-9]+): ([0-9]*)([^\\.]+)"
}
(The text.text.latex.sweave
context is defined in the Sweave Textmate bundle, which kind of works in Sublime Text)
(text.text.latex.sweave上下文在Sweave Textmate包中定义,哪种在Sublime Text中有效)
The build system takes a .Rnw
file, converts it to TeX, and then runs pdflatex
on it. This build system works, but it is fairly limited in how it opens Skim (it just opens the PDF—that's all). The LaTeXTools Sublime Text package is far more robust and opens/refreshes Skim while highlighting modified lines and providing Skim's magic reverse search.
构建系统采用.Rnw文件,将其转换为TeX,然后在其上运行pdflatex。这个构建系统可以工作,但它在打开Skim的方式上是相当有限的(它只是打开PDF - 这就是全部)。 LaTeXTools Sublime Text包更加强大,可以在突出显示修改后的线条并提供Skim魔术反向搜索的同时打开/刷新Skim。
I don't want to rewrite the LaTeXTools build system, especially since it does most of the heavy lifting (and Skim magic) with a separate Python script. However, I would really like to be able to use it to build a TeX file generated from Sweave.
我不想重写LaTeXTools构建系统,特别是因为它使用单独的Python脚本完成了大部分繁重的工作(和Skim魔术)。但是,我真的希望能够用它来构建一个从Sweave生成的TeX文件。
Ideally, I'd love to somehow nest a build system—convert an .Rnw
file to TeX and then immediately run the LaTeXTools build system that already exists. In pseudocode:
理想情况下,我喜欢以某种方式嵌套构建系统 - 将.Rnw文件转换为TeX,然后立即运行已存在的LaTeXTools构建系统。在伪代码中:
{
[CONVERT RNW TO ${file_name}.tex && RUN THE LATEXTOOLS BUILD SYSTEM ON ${file_name}.tex]
}
Is it possible to access a build system from inside another build system (or alternatively, access a build system from bash)?
是否可以从另一个构建系统内部访问构建系统(或者从bash访问构建系统)?
2 个解决方案
4
This is a patch to two files in the LatexTools plugin in order to deal with Rnw files, and one patch to the Latex plugin in order to make Rnw files to behave like LaTeX files.
这是LatexTools插件中的两个文件的补丁,用于处理Rnw文件,以及Latex插件的一个补丁,以使Rnw文件的行为类似于LaTeX文件。
First the patch to the Latex plugin, in specific to the file LaTeX.tmLanguage:
首先是Latex插件的补丁,具体到文件LaTeX.tmLanguage:
fileTypes
tex
Rnw
Observe how I added an element to the array in order to deal with Rnw extensions.
观察我如何向数组添加元素以处理Rnw扩展。
Now the patch to makePDF.py
现在补丁makePDF.py
look for a a line like this
寻找像这样的一条线
if self.tex_ext.upper() != ".TEX":
sublime.error_message("%s is not a TeX source file: cannot compile." % (os.path.basename(view.file_name()),))
return
and replace it with
并替换它
if (self.tex_ext.upper() != ".TEX") and (self.tex_ext.upper() != ".RNW"):
sublime.error_message("%s is not a TeX or Rnw source file: cannot compile." % (os.path.basename(view.file_name()),))
return
Then look for a line like
然后寻找像这样的一条线
os.chdir(tex_dir)
CmdThread(self).start()
print threading.active_count()
and replace it with
并替换它
os.chdir(tex_dir)
if self.tex_ext.upper() == ".RNW":
# Run Rscript -e "library(knitr); knit('" + self.file_name + "')"
os.system("Rscript -e \"library(knitr); knit('"+ self.file_name +"')\"")
self.file_name = self.tex_base + ".tex"
self.tex_ext = ".tex"
CmdThread(self).start()
print threading.active_count()
The last patch is to the file jumpToPDF.py
最后一个补丁是文件jumpToPDF.py
look for a line
寻找一条线
if texExt.upper() != ".TEX":
sublime.error_message("%s is not a TeX source file: cannot jump." % (os.path.basename(view.fileName()),))
return
and replace it with
并替换它
if (texExt.upper() != ".TEX") and (texExt.upper() != ".RNW"):
sublime.error_message("%s is not a TeX or Rnw source file: cannot jump." % (os.path.basename(view.fileName()),))
return
Good luck!
祝你好运!
1
Thanks for the detailed description of the required changes Herberto!
感谢Herberto所需更改的详细说明!
I just went ahead and changed the mentioned files. Everything works like a charm! One thing is though, not sure if it is required, but I recompiled both python files to .pyc after editing them.
我刚才继续改变了上面提到的文件。一切都像魅力一样!但有一点是,不确定是否需要,但我在编辑后将两个python文件重新编译为.pyc。
python -m py_compile makePDF.py
does the job. Should anyone run into an "invalid syntax error" at the line
做的工作。如果有人在该行遇到“无效的语法错误”
print threading.active_count()
while recompiling, just replace it with:
重新编译时,只需将其替换为:
print(threading.active_count())
Also, since the log parser of LaTeXTools only displays errors from the log files, we might be interested to see the knitr output as well. You can store it in a separate log file by replacing:
此外,由于LaTeXTools的日志解析器仅显示日志文件中的错误,我们可能也有兴趣看到knitr输出。您可以通过替换来将其存储在单独的日志文件中:
os.system("Rscript -e \"library(knitr); knit('"+ self.file_name +"')\"")
with:
有:
knitcmd = "/usr/bin/Rscript -e \"library(knitr); knit('"+ self.file_name +"')\""
process = subprocess.Popen(knitcmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
#Launch the shell command:
knit_output, knit_error = process.communicate()
#store results in a log
knit_log = open(self.tex_base + "_knitrbuild.log", "w")
knit_log.write(knit_output)
knit_log.write(knit_error)
knit_log.close()
Before I was using a simple bash script to build the documents (Mac specific):
在我使用简单的bash脚本构建文档之前(特定于Mac):
#!/bin/bash
[ $# -eq 0 ] && { echo "Usage: $0 file.Rnw for knitting"; exit 1; }
rnw="library(knitr);knit("\'"$1.Rnw"\'")"
echo "Rscript executing:" $rnw
tex="$1.tex"
pdf="$1.pdf"
Rscript -e $rnw && pdflatex $tex && pdflatex $tex && open -a Preview $pdf
retval=$?
[ $retval -eq 0 ] && echo "$rnw knitted and $pdf ready"
but being able to customize LaTeXTools and run it directly from ST2 with Skim support is very nice.
但能够自定义LaTeXTools并直接从ST2运行它与Skim支持是非常好的。
Is there any reasons why you wouldn't want to add the changes you outlined directly into your package source? (maybe my it's only my version that's too old.)
您是否有任何理由不想将您概述的更改直接添加到包源中? (也许我只是我的版本太旧了。)