Clarinetist - 7 months ago 49
LaTeX Question

# Adding " \\ \hline " after every component of a vector

Consider the following:

v <- letters[1:4]
v
[1] "a" "b" "c" "d"


I would like the following output using
v
:

"a \\ \hline b \\ \hline c \\ \hline d \\ \hline"


or the version with quotation marks removed. The closest I've gotten to this is

paste0(v, collapse = " \\ \\hline ")
[1] "a \\ \\hline b \\ \\hline c \\ \\hline d"


and then I
CTRL + F
for Find and Replace in RStudio using
\\hline
->
\hline
, which is simple enough, but I would like to see if there's a way to just create this string without that extra step.

This problem comes up when I want to create a
tabular
environment using a character vector in an
.Rnw
file using
LaTeX
code.

Answer

If this is for printing, we can use cat

cat(v, sep = " \\\\ \\hline ")