我有一个字符串数组,并尝试将每个元素输出到 LaTeX。我可以使用以下代码实现这一点:
\documentclass[12pt,english,nohyper]{tufte-handout}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackaage{hyperref}
\usepackage{graphicx}
\usepackage[space]{grffile}
\usepackage{geometry}
\usepackage{enumitem}
\usepackage{pgffor}
\makeatletter
\makeatother
\begin{document}
<<include=FALSE>>=
library(ggplot2)
library(reshape2)
library(xtable)
@
\centerline{\Large\bf This is a test}
\vspace{1cm}
\noindent
My List:
\vspace{2mm}
<<echo=FALSE,results='asis'>>=
myList = c("A. This is the first item in my list.", "B. This is the second item in my list, and it will span across two lines because it is so long. This is the second item in my list, and it will span across two lines because it is so long.", "C. This is the third item in my list")
@
\begin{enumerate}[label=\Alph*., itemsep=-1ex]
\item \Sexpr{str_trim(unlist(strsplit(chapter_outcomes[1], "[.]"))[2])}
\item \Sexpr{str_trim(unlist(strsplit(chapter_outcomes[2], "[.]"))[2])}
\item \Sexpr{str_trim(unlist(strsplit(chapter_outcomes[3], "[.]"))[2])}
\end{enumerate}
\end{document}
这为我提供了我希望实现的正确输出:
但是,我现在尝试让此迭代在 for 循环中进行,因为我的 chapter_outcomes 字符串数组中可能并不总是恰好有三个元素。我尝试了以下变体:
\begin{enumerate}[label=\Alph*., itemsep=-1ex]
\foreach \i in {\Sexpr{length(chapter_outcomes)}} {
\item \Sexpr{str_trim(unlist(strsplit(chapter_outcomes[\i], "[.]"))[2])}
}
\end{enumerate}
然而,这会导致 chapter_outcomes[\i] 的上述语法出现“意外输入”错误。
我尝试过查看类似的帖子(LaTeX 中的迭代,http://www.bytemining.com/2010/04/some-latex-gems-part-1-tikz-loops-and-more/),但他们的重点完全不同,因此我无法用它来解决我的问题。
感谢您的任何建议。
答案1
我不太了解 R 的所有内容,但如果你能以某种方式将数据放入宏(如)中\def\mydata{% <<echo=FALSE,results='asis'>>= myList = c("A. This is the first item in my list.", "B. This is the second item in my list, and it will span across two lines because it is so long. This is the second item in my list, and it will span across two lines because it is so long.", "C. This is the third item in my list") @ }
,那么就可以轻松地对其进行解析,这里使用listofitems
包即可。
\documentclass{article}
\usepackage{listofitems}
\begin{document}
\def\mydata{%
<<echo=FALSE,results='asis'>>=
myList = c("A. This is the first item in my list.", "B. This is the second item in my list, and it will span across two lines because it is so long. This is the second item in my list, and it will span across two lines because it is so long.", "C. This is the third item in my list")
@
}
\begin{itemize}
\setsepchar{(||)/"/.}%
\readlist*\mylist{\mydata}%
\foreachitem\x\in\mylist[2]{%
\expandafter\ifx\expandafter,\x\else%
\item[\textbf{{\mylist[2,\xcnt,1]}:}] \mylist[2,\xcnt,2].%
\fi%
}
\end{itemize}
\end{document}