1

Im using Hmisc in rmarkdown file. when I create a table this is what I do

---
output: pdf_document
---

```{r Arrests Stats, results ='asis', message = FALSE, warning = FALSE, echo = FALSE}

# render the table

options(digits=1)
library(Hmisc)
latex(head(mtcars), file="")

```

The latex output has the first row showing as below

%latex.default(cstats, title= title....
\begin{table}...
.
.
.
\end{tabular}

Notice the '%' I need to figure out to remove the first line as it shows on the PDF document when its weaved

user3357059
  • 1,122
  • 1
  • 15
  • 30
  • I've done a test with `stargazer` too and there is a line of code with `%` as well. The very strange thing it that `%` is a comment for LaTeX and so with `asis` chunk option it should be ignored. Maybe it is a bug. Are you using knitr in Rstudio? – SabDeM Jul 16 '15 at 00:51
  • Anyway, there is a solution [here](http://stackoverflow.com/questions/24400308/how-to-remove-the-lines-in-xtable-table-output-by-knitr) with `xtable`. – SabDeM Jul 16 '15 at 00:59

1 Answers1

3

Looks like that's hard-coded into latex.default (cat("%", deparse(sys.call()), "%\n", file = file, append = file != "", sep = "") is in the body, with no conditional surrounding it).

I think your best guess then would be to capture.output the cat-d output and strip the comment yourself.

cat(capture.output(latex(head(mtcars), file=''))[-1], sep='\n')

The capture.output catches all the stuff that latex(...) cats, the [-1] removes the first line (being the '%latex.default'), the cat prints out everything else, with newline separator.

You might define your own mylatex to do this, and be a little more clever (e.g. instead of blindly stripping the first line of the output, you could only strip it if it started with '%').

mylatex <- function (...) {
    o <- capture.output(latex(...))
    # this will strip /all/ line-only comments; or if you're only
    #  interested in stripping the first such comment you could
    #  adjust accordingly
    o <- grep('^%', o, inv=T, value=T)
    cat(o, sep='\n')
}
mylatex(head(mtcars), file='')
mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194