Character vector in R as itemized list with knitr

回眸只為那壹抹淺笑 提交于 2019-12-10 15:27:04

问题


I have a vector (e.g. letters) that I want to incorporate into my .Rnw knitr document as an itemized list. How do I do this?

I have tried \Sexpr{letters} but it merely places commas between each:

a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z

Something like this is just as unhelpful:

\begin{itemize}
\item[\Sexpr{letters}]
\end{itemize}

What is the proper way to do this?

I'm looking for a result like:

  • a
  • b
  • c
  • d
  • e
  • f

etc.


回答1:


\documentclass{article}
\begin{document}
\begin{itemize}
  \item
<<results="asis", echo=FALSE>>=
  lets = letters[1:5]
  cat(lets, sep="\n\\item ")
@
\end{itemize}
\end{document}

Or, more elegantly, with a dummy entry instead of the explicit \item:

<<results="asis", echo=FALSE>>=
  lets = letters[1:5]
  cat("", lets, sep="\n\\item ")
@



回答2:


Or

\documentclass{article}
\begin{document}

Here is a list.

\begin{itemize}
<<results="asis", echo=FALSE>>=
cat(sprintf('\\item{%s}', letters[1:5]), sep = '\n')
@
\end{itemize}

That was a list.

\end{document}



来源:https://stackoverflow.com/questions/29656476/character-vector-in-r-as-itemized-list-with-knitr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!