5

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')
user20650
  • 24,654
  • 5
  • 56
  • 91
bioglp
  • 75
  • 5
  • 4
    You can paste the latex command around the column you want in italics. So, for example, `iris$Species <- paste0("\\textit{", iris$Species, "}") ; print(xtable(head(iris)), sanitize.text.function = identity)` (but i guess that counts as manual intervention) – user20650 Sep 06 '15 at 22:18
  • but it works perfectly! thanks – bioglp Sep 06 '15 at 23:52
  • 1
    The advantage (in my use case) of @user20650's solution is that it applies formatting to the column entries but **not** to the header. i.e. it gives results like those posted in the answer below, except that the column header "Species" is not italicized. – Josh O'Brien Jun 16 '16 at 20:05

1 Answers1

2

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.

CL.
  • 14,577
  • 5
  • 46
  • 73
  • (+1)Good answer, I wasnt aware of this way. Some quick searching, i see you can use `bfseries`sto make bold. Is there an equivalent way to change colour? (A side note - the output is slightly different as `itshape` doesn't correct the spacing [discussed here](http://tex.stackexchange.com/questions/41681/correct-way-to-bold-italicize-text/41684#41684) – user20650 Sep 07 '15 at 11:27
  • 1
    @user20650 Yes, changing the color is also possible. I updated my answer to show this. I also inclded how to use `textit` instead of `itshape`. In the first version of the answer I avoided this because the syntax is a little bit more complicated. – CL. Sep 07 '15 at 12:11