Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single-file knitr projects do not build properly #42

Closed
mathuin opened this issue Apr 2, 2017 · 10 comments · Fixed by #44
Closed

Single-file knitr projects do not build properly #42

mathuin opened this issue Apr 2, 2017 · 10 comments · Fixed by #44

Comments

@mathuin
Copy link

mathuin commented Apr 2, 2017

I have a project which has a single knitr file named weightpaper.Rnw. There are no other .tex or .Rnw files in the directory. I have created a .latexcfg with the following contents:

{
    "latex_ext": [
        ".tikz",
        ".Rnw"
    ]
}

I open the knitr file, and click the build icon at the bottom. I get a blue notification that this file has been set as the root file, and a red notification reporting a failure to build. The contents of the red notification:

Command failed: latexmk -synctex=1 -interaction=nonstopmode -file-line-error -pdf weightpaper.Rnw
Latexmk: This is Latexmk, John Collins, 1 January 2015, version: 4.41.
Rule 'pdflatex': File changes, etc:
   Non-existent destination files:
      'weightpaper.pdf'
------------
Run number 1 of rule 'pdflatex'
------------
------------
Running 'pdflatex  -synctex=1 -interaction=nonstopmode -file-line-error -recorder  "weightpaper.Rnw"'
------------
Latexmk: Log file says output to 'weightpaper.pdf'
Collected error summary (may duplicate other messages):
  pdflatex: Command for 'pdflatex' gave return code 256
Latexmk: Use the -f option to force complete processing,
 unless error was exceeding maximum runs of latex/pdflatex.

I had expected something like this to be executed before the call to latexmk: Rscript -e "library(knitr); knit('$<')"

Am I doing something wrong? What should I be doing instead?

@ashthespy
Copy link
Owner

ashthespy commented Apr 3, 2017

Latexmk as far as I know doesn't have inbuilt support for kniter.
To compile, you could either set a custom command such as:

Rscript -e "library(knitr); knit('%DOC')" && latexmk -synctex=1 -interaction=nonstopmode -file-line-error -pdf %DOC

Another option would be arara - and a custom rule ( arara should already be available with an installation of TexLive or MikTex)

I'd be interested in knowing how you solve this, and maybe even update the readme with what works for you!

@James-Yu James-Yu closed this as completed Apr 3, 2017
@ashthespy
Copy link
Owner

ashthespy commented Apr 3, 2017

@James-Yu maybe should re-open this until confirmation that it works?

@James-Yu James-Yu reopened this Apr 3, 2017
@mathuin
Copy link
Author

mathuin commented Apr 4, 2017

I'm using LaTeX for a bunch of stuff, including my thesis, so I ended up using the Docker container which builds my thesis to build this paper. It worked well, but went way outside the scope of this package so I think it's probably a bad thing to recommend to others. :-)

Were I to recommend a fix or write a PR, I would rewrite the "latex_ext" object to be a hash, with file types as keys and one-line commands as values. I would then have had something like this:

{
    "latex_ext": [
        ".tikz": "some command that converts foo.tikz to foo.tex",
        ".Rnw": "Rscript -e "library(knitr); knit('%DOC')""
    ]
}

The package could then run the pre-processing commands whenever those files were modified and saved, and in my case the LaTeX root would then be pointed at the output of the pre-processing command for my source file.

@mathuin
Copy link
Author

mathuin commented Apr 4, 2017

On further reflection, I think a better way to handle this would be to combine toolchain and latex_ext into one object: toolchains. toolchains would have the key-value definitions mentioned above for latex_ext but with the existing toolchain used as the value for the key .tex. This allows for the deprecation of the toolchain object and its associated bits while not breaking existing code. I haven't written CoffeeScript, but I am willing to give it a try.

@James-Yu
Copy link
Collaborator

James-Yu commented Apr 5, 2017

As main functionality for the extension is to provide LaTeX support, the variants are not considered as important. I'd recommend you create customized .latexcfg files to control the local behavior of different files. E.g., you may put all .tikz files in a same subfolder and create a .latexcfg file with the specific toolchain. In this case whenever you edit a file or changed one you may run build/build here to refresh.

I personally do not use either .tikz or .Rnw so these are only my guesses.

@ashthespy
Copy link
Owner

ashthespy commented Apr 5, 2017

Option 1

Am I oversimplifying this, or wouldn't a .latexcfg like this work fine?

{
  "root": "path/to/my/file.Rnw",
  "toolchain": "Rscript -e \"library(knitr); knit('%DOC')\" && latexmk -synctex=1 -interaction=nonstopmode -file-line-error -pdf %DOC",
  "latex_ext": [".Rnw"]
}

The builder should parse the toolchain key and split them into sequential commands at &&

Once issue I foresee though is that the %DOC placeholder by default expects the root file to be .tex so the the second %DOC in the toolchain would be expanded to file.Rnw in this case and not file.tex
This can be fixed easily, but first need to confirm if this approach would work!

Option 2

Repeating from my last comment, but I'd highly recommend arara for this case!
With a rule such as:

!config
identifier: knitr
name: knitr
commands: 
- <arara> RScript -e "library(knitr); knit('"@{source}"', output='"@{target}"')"
- <arara> xelatex "@{target}"
arguments: 
- identifier: source
  flag: <arara> "@{parameters.source}"
  default: <arara> "@{getBasename(file)}".Rnw
- identifier: target
  flag: <arara> "@{parameters.target}"
  default: <arara> "@{getBasename(file)}".tex

Then with the directive in the .Rnw file as % arara: knitr
and setting your toolchain to arara %DOC -v it should produce the pdf directly by running build :-)

Unfortunately, I don't use R, so can't test this!

@James-Yu
Copy link
Collaborator

James-Yu commented Apr 5, 2017

@ashthespy Thanks a lot for the summary! I will add a reference to this answer in the readme.

@mathuin Can you please check if the methods suggested by @ashthespy above work for you? Thanks.

@mathuin
Copy link
Author

mathuin commented Apr 5, 2017

The first option works, with some minor sed magic which replaces the Rnw with tex.

{
    "root": "example.Rnw",
    "toolchain": "Rscript -e \"library(knitr); knit('%DOC')\" && latexmk -synctex=1 -interaction=nonstopmode -file-line-error -pdf $(echo %DOC | sed -e \"s/Rnw/tex/\")",
    "latex_ext": [".Rnw"]
}

I was unable to get arara to do anything useful after half an hour of tweaking -- the configuration it requires is a bit too much work for something I'm never going to use, so maybe someone else can get it to work.

@James-Yu
Copy link
Collaborator

James-Yu commented Apr 6, 2017

Glad at least it works for you!

@James-Yu James-Yu closed this as completed Apr 6, 2017
@ashthespy
Copy link
Owner

ashthespy commented Apr 6, 2017

With the PR merged, I'd reckon the following configuration would work for .Rnw files

{
  "root": "path/to/my/file.Rnw",
  "toolchain": "Rscript -e \"library(knitr); knit('%DOC.Rnw')\" && latexmk -synctex=1 -interaction=nonstopmode -file-line-error -pdf %DOC",
  "latex_ext": [".Rnw"]
}

Cheers! :-)
PS: Sorry for the clutter, I need to understand git flow better. :-(
But, that should fix it, will submit a PR soon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants