Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xtable italics format for a column

I need to insert the species names in a table created by xtable in my Rnw file and I want to convert the relative column to italics format. Is it possible without any manual intervention?

My call is:

xtable(cklist, caption="Checklist...", align='lllc',label = 'tab:ckzygo')
like image 932
bioglp Avatar asked Oct 31 '22 19:10

bioglp


1 Answers

To typeset a column in italics (or any other non-standard font shape), you should use the >{\cmd} syntax for column specification.

Assigning the column type >{\itshape}l generates a left-justified column in italics.

This is a better solution than iris$Species <- paste0("\\textit{", iris$Species, "}") as suggested in the comments because you neither have to modify your data nor you need to disable text sanitizing.

Small illustration:

\documentclass{article}
\usepackage{array}
\begin{document}
<<xtableItalics, results = "asis">>=
library(xtable)

print(xtable(head(iris), align = c(rep("l", 5), ">{\\itshape}l")))
@
\end{document}

The PDF looks like: enter image description here

Please note that you need to use the array package for this to work.

EDIT: To show the flexibility of this approach, two more examples:

print(xtable(head(iris), align = c(rep("l", 5), ">{\\textit\\bgroup}l<{\\egroup}")))
print(xtable(head(iris), align = c(rep("l", 5), ">{\\textcolor{red}\\bgroup}l<{\\egroup}")))

The first line uses \textit{} instead of \itshape to typeset the italics. As \textit{} requires the text to modify as an argument, we need a slightly more complex syntax. (It's described in the wikibooks.org article linked above.)

This syntax can also be used to change for example the color of the text. In more complex cases, lrbox is required, as described in the linked article.

like image 171
CL. Avatar answered Nov 08 '22 05:11

CL.