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

error while using knitr with papeR #35

Closed
iZsh opened this issue Apr 26, 2017 · 5 comments
Closed

error while using knitr with papeR #35

iZsh opened this issue Apr 26, 2017 · 5 comments

Comments

@iZsh
Copy link

iZsh commented Apr 26, 2017

Hello,

I have compiling error while trying to use knitr and papeR to produce a pdf (through LaTeX).

The latex comments such as
%% Output requires \usepackage

seem to be escaped to %% during the rmardown/knitr processing, and LaTeX therefore fails to compile. I cannot see any option in your code to prevent such comments from being generated, and I fail to understand/find how to prevent those % to be escaped.

with the following code:

xtable(summarize(data, type = "factor", variables = "Type"))

here is the part where LaTeX fails during pandoc generation:

! LaTeX Error: Can be used only in preamble.

See the LaTeX manual or LaTeX Companion for explanation.
Type H for immediate help.
...

l.122 %% Output requires \usepackage

pandoc: Error producing PDF

I'm using RStudio 1.0.143, rmarkdown 1.5, knitr 1.15.1, R 3.3.2 on a Mac OS X 10.10.5

@hofnerb
Copy link
Owner

hofnerb commented Apr 26, 2017

It seems you are using markdown to produce a PDF. With markdown you cannot use LaTeX syntax and hence cannot use xtable as you do. In order to obtain tables (not as nice though) in markdown, you can e.g. use kable. Please see [https://cran.r-project.org/web/packages/papeR/vignettes/papeR_introduction.html](Vignette: Using papeR with Markdown).

Does this solve your problem?

I realized that the part on xtable is confusing in this Vignette and hence should be removed.

@iZsh
Copy link
Author

iZsh commented Apr 26, 2017

yes I'm using rmarkdown, and the code generated by xtable works just fine if I copy/paste it (after generating it with results="tex" to get it instead of using results="asis")

in fact, since you don't really use the booktabs option for anything else than printing this latex comment, using the following code is a workaround:
options(xtable.booktabs = FALSE)

then I can use those xtable commands it seems (I've just figured this out)

NB: but of course in my rmarkdown header I'm adding, just in case:
header-includes:

  • \usepackage{booktabs}

So I'm confused about why you think I cannot use rmarkdown for LaTeX. (rmarkdown != markdown)
I can directly add LaTeX commands in rmarkdown just fine, it's just that %% are not allowed for some obscure reason, but anything else is. In fact you could use \begin{comment} ... \end{comment} instead of %%, and that would work just fine too.

@hofnerb
Copy link
Owner

hofnerb commented Apr 26, 2017

Ok, I see. That is great. I wasn't aware that one can combine markdown and latex by using rmarkdown. Funny enough, however, that all LaTeX commands are ignored if one switches to another output...

I'll try to fix your issue [> easy way should be to remove one %]. Actually, it might even make more sense to issue a note in R in some way. However, these can be suppressed in knitr...

Of note, one also has to switch print.xtable( , comment = FALSE) as otherwise xtable adds latex comments (e.g. % latex table generated in R 3.3.3 by xtable 1.8-2 package % Wed Apr 26 14:56:30 2017) which will be printed.

Finally, don't you think this is actually a bug in rmarkdown? Why would one want to escape % signs in results = "asis" and why should %% throw an error.

@iZsh
Copy link
Author

iZsh commented Apr 26, 2017

I have no clue, hence my "obscure reason" comment about rmarkdown escaping %%.
It seems there is a bug report about this on rmarkdown ongoing, but it has been filed and discussed for quite a while and I was therefore more hopeful going your way for a quicker fix than waiting years for rmarkdown to act upon it.

Given how quickly you answered, I guess I did make the right choice ;-)

side note: rmarkdown is actually quite pleasant apart from this bug, it helps generating "LaTeX" papers quite nicely without the hassle of LaTeX's syntax (well it isn't really a hassle when you are used to programming, but markdown texts are way easier to (naturally) read than LaTeX's). One of the only things missing in rmarkdown (imho) is that they didn't port complex table syntax yet, hence you're back to LaTeX code for complex tables (unless you can use xtable or alike of course).

With rmarkdown you can generate also html, or presentation etc. But yes, if you include specific codes (LaTeX code or html code), it gets ignored if you switch the output, but I guess that's ok for now. I hardly ever use specific language code, and more and more packages now support both generating html and LaTeX code.

@hofnerb
Copy link
Owner

hofnerb commented Apr 26, 2017

Ok, changing the output to a message seems to work. See latest github commit.

However, any message regarding booktabs might be nonsense in knitr as knitr would print all messages and warnings in the PDF but cannot do this as the PDF cannot be created without \usepackage{booktabs}. Hence, the user won't see that message unless he included booktabs (in which case it is useless). Do you have any ideas how one could solve this?

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

No branches or pull requests

2 participants