【发布时间】:2015-09-09 14:38:53
【问题描述】:
这是我之前发布的问题 (Outputting character string elements neatly in Sweave in R) 的延续。我有一个字符串数组,并试图将每个元素输出到 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] 语法中导致“意外输入”错误。
我曾尝试查看类似的帖子(Iteration in LaTeX、http://www.bytemining.com/2010/04/some-latex-gems-part-1-tikz-loops-and-more/),但它们的重点不同,我无法在这里解决我的问题。
感谢您的建议。
【问题讨论】:
-
你试过tex.stackexchange吗?
-
谢谢,我刚刚在那儿发帖。
标签: arrays for-loop latex enumerate