tables-package https://www.e-learn.cn/tag/tables-package zh-hans Simple example of using tables + knitr for latex https://www.e-learn.cn/topic/917208 <span>Simple example of using tables + knitr for latex</span> <span><span lang="" about="/user/201" typeof="schema:Person" property="schema:name" datatype="">不羁的心</span></span> <span>2019-12-01 08:54:43</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I'm struggling with a tables package, all the examples in the packable docs are so complex and none of them works for me with <code>knitr</code> and latex. Could somebody help be out and display a simple table with some formulas and multiline labels in the header? </p> <p>Something like this:</p> <pre><code>df &lt;- data.frame(matrix(1:9, nrow = 3)) colnames(df) &lt;- c("first column", "second \\frac{1}{2}", "third column first line \\ second line") </code></pre> <p>Thank you in advance</p> <br /><h3>回答1:</h3><br /><p>It is possible to create multi-line headers for tables in LaTeX using the <code>xtable</code> package. These can be compiled from either .Rmd or .Rnw files. Building on the example by mattw and using <code>add.to.row</code> in the <code>print</code> method for <code>xtable</code>:</p> <pre><code>df &lt;- data.frame(matrix(1:50, nrow = 10)) print( xtable(df), include.rownames = FALSE, include.colnames = FALSE, add.to.row = list( pos = list(0), command = c( "&amp; \\multicolumn{4}{c}{4 column header} \\\\ \\cline{2-5} col1 &amp; col2 &amp; col3 &amp; col4 &amp; col5 \\\\ " ) ) ) </code></pre> <p></p> <p>Note that <code>add.to.row</code> requires a list with two elements: pos and command. The first must be a list, the second a character string or vector, see <code>?print.xtable</code>. <code>pos</code> gives the row number for the LaTeX insertion, and <code>command</code> is the insertion. Be a bit careful with formatting this, as it is will run directly into the next cell of the first column if you don't put in spaces or <code>\n</code>.</p> <p>There are lots of options for customisation, allowing you to create quite complex tables with a bit of tweaking.</p> <pre><code>print( xtable(df), include.rownames = FALSE, include.colnames = FALSE, hline.after = c(-1,0), add.to.row = list( pos = list(0,5,10), command = c( "&amp; \\multicolumn{4}{c}{4 column header} \\\\ \\cline{2-5} col1 &amp; col2 &amp; col3 &amp; col4 &amp; col5 \\\\ ", "\\hline \\multicolumn{5}{l}{Separator in table.} \\\\ \\hline", "\\hline \\multicolumn{5}{l}{Notes at end of table.} \\\\ " ) ) ) </code></pre> <p></p> <p>In this example I change the default settings for where <code>xtable</code> puts <code>\hline</code>, allowing me to add the last <code>\hline</code> above the notes - useful for explaining superscripts in the table.</p> <p>Note also the use of <code>\cline{2-5}</code> giving me a line over columns 2 - 5.</p> <p>See gist for fully reproducible example.</p> <br /><br /><br /><h3>回答2:</h3><br /><p>I don't think that this is possible with RMarkdown if you want the table to be in LaTeX style. However, you can easily do this with the <code>xtable</code> package when you write your code in an <code>.Rnw</code> file:</p> <pre><code>\documentclass{article} \begin{document} &lt;&lt;&gt;&gt;= library("xtable") df &lt;- data.frame(matrix(1:9, nrow = 3)) colnames(df) &lt;- c("first column", "second $\\frac{1}{2}$", "third column") @ &lt;&lt;xtable, results = "asis"&gt;&gt;= print(xtable(df), floating = TRUE, sanitize.colnames.function = identity, type = "latex") @ \end{document} </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/32005471/simple-example-of-using-tables-knitr-for-latex</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/r" hreflang="zh-hans">r</a></div> <div class="field--item"><a href="/tag/latex" hreflang="zh-hans">latex</a></div> <div class="field--item"><a href="/tag/knitr" hreflang="zh-hans">knitr</a></div> <div class="field--item"><a href="/tag/tables-package" hreflang="zh-hans">tables-package</a></div> </div> </div> Sun, 01 Dec 2019 00:54:43 +0000 不羁的心 917208 at https://www.e-learn.cn