-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathREADME.Rmd
155 lines (100 loc) · 4.26 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
Convert `sjPlot::tab_model()` html tables to tex and pdf with `html2pdf()` and use them in Rmarkdown documents with `tex2Rmd()`.
## Installation
html2latex is currently only available on Github.
```{r}
# remotes::install_github("gorkang/html2latex")
library("html2latex")
```
## Requirements
Fully tested on Linux. Partially tested on Mac and Windows.
1) You need a working [Libreoffice](https://www.libreoffice.org/) installation to convert `html` to
`odt`
2) We include [Writer2latex](https://sourceforge.net/projects/writer2latex/files/writer2latex/)
in this package for the `odt` to `tex` step
3) You will need a working Java installation
4) You will also need a TeX compiler if you want to use the integrated pdf compilation (you can try installing `tinytex` and `tinytex::install_tinytex()`)
`html2pdf()` function uses a *Writer2latex* script which is sourced from
the extdata folder.
## Issues
If something does not work, make sure to have all the requirements above. You can use `silent = FALSE` in `html2pdf()` to see in which step things are failing. If nothing works, do send an issue including a reproducible example.
Important: **There should be no spaces in the path to your table, or in the table file name**.
## Example
Create a `sjPlot::tab_model()` and save it as html.
```{r message=FALSE, warning=FALSE}
library(html2latex)
library(lme4)
library(sjPlot)
# This is a terrible model
model = lmer(mpg ~ cyl * disp + (1|vs), mtcars)
# We save the sjPlot table to an .html file
sjPlot::tab_model(
model,
show.r2 = TRUE,
show.icc = FALSE,
show.re.var = FALSE,
p.style = "scientific",
emph.p = TRUE,
file = "temp.html")
```
![](man/figures/sjplot.png)
Using the `html2pdf()` we can transform the html output of `sjPlot::tab_model()` to .tex. We can also compile to pdf in one step with the parameter `build_pdf = TRUE`.
```{r message=FALSE, warning=FALSE}
# Create tex and pdf
html2pdf(filename = "temp.html",
table_width = 13,
silent = FALSE,
style = TRUE,
build_pdf = TRUE,
clean = TRUE,
name_table = "table1")
#> pdf file created in: /.../html2latex/temp.pdf
#> tex file created in: /.../html2latex/temp.tex
```
![](man/figures/html2latex.png)
## tex2Rmd
You can include `sjPlot::tab_model()` html tables in a Rmarkdown document that outputs as pdf in three steps:
### 1\. YAML header
The YAML heather of the .Rmd document must include this:
header-includes:
- \usepackage{array}
- \usepackage{longtable}
- \newcommand\textstyleStrongEmphasis[1]{\textbf{#1}}
- \makeatletter
- \newcommand\arraybslash{\let\\\@arraycr}
---
### 2\. Extract the table bit from the tex file
The `tex2Rmd()` function creates a `.txt` file getting rid of the parts of the `.tex` code that cause conflicts when rendering the Rmd document.
```{r message=FALSE, warning=FALSE}
# Create table.txt to be able to use it in Rmd documents
tex2Rmd("temp.tex")
#> File with table code created in: table.txt
```
The tex file created with html2pdf can be rendered as a pdf by opening the tex file in RStudio and using the `Compile PDF` button. But if you want to use the table specific code inside a Rmd file (from `\begin{longtable}` to `\end{longtable}`), we need to extract it first. This is automatically done by the ´tex2Rmd()´ function.
### 3\. Use this code in the Rmd document.
Finally, you need to insert the latex code below outside of a chunk in your Rmd file.
\newcommand{\myinput}[1]{%
\begingroup%
\renewcommand\normalsize{\small}% Specify your font modification
\input{#1}%
\endgroup%
}
\begin{centering}
\myinput{table.txt}
\end{centering}
-----
To see the result, please check the example.pdf in the github repository (will not be installed alongside the package)
(You might need to then call pdflatex in a terminal separately, if compiling via RStudio fails)
## Manually input latex code
Alternatively, you can manually insert the contents of table.txt in a chunk staring with ` ```{=latex} `
See: <https://bookdown.org/yihui/rmarkdown-cookbook/raw-latex.html>