[Tex/LaTex] Iterating through a character/string array in LaTeX

loopsprintingrsweave

I have an array of strings, and am trying to output each element into LaTeX. I am able to achieve that using the following code:

\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}

This provides me the correct output that I had wished to achieve:

Current and correct output

However, I am now trying to get this iteration to occur in a for-loop, as I may not always have exactly three elements in my chapter_outcomes string array. I have tried variants of the following:

\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}

However, this leads to an error of "unexpected input" at the above syntax for chapter_outcomes[\i].

I have tried looking at similar posts (Iteration in LaTeX, http://www.bytemining.com/2010/04/some-latex-gems-part-1-tikz-loops-and-more/) but their focus is different enough that I cannot apply it to solve my problem here.

Thank you for any advice.

Best Answer

I don't get all the R stuff, but if you can somehow get the data into a macro such as \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") @ }, it can be easily parsed, here using the listofitems package.

\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}

enter image description here