2015-06-23 27 views
7

Questa è una continuazione della mia domanda pubblicata in precedenza (Outputting character string elements neatly in Sweave in R). Ho una serie di stringhe e sto provando a produrre ogni elemento in LaTeX. Sono in grado di ottenere quello usando il seguente codice:Iterazione attraverso un array di caratteri/stringa in 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} 

Questo mi fornisce l'output corretto che avevo voluto realizzare:

Correct formatting

Tuttavia, ora sto cercando di ottenere questa iterazione a verificarsi in un ciclo for, come posso non sempre ho esattamente tre elementi nel mio array di archi di capitoli chapter_outcomes. Ho provato varianti della seguente:

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

Tuttavia, questo porta ad un errore di "input inaspettato" presso la sintassi di cui sopra per chapter_outcomes [\ i].

Ho provato a guardare post simili (Iteration in LaTeX, http://www.bytemining.com/2010/04/some-latex-gems-part-1-tikz-loops-and-more/) ma la loro attenzione è abbastanza diversa da non poterla applicare per risolvere il mio problema qui.

Grazie per qualsiasi consiglio.

+1

Hai provato [tex.stackexchange] (http://tex.stackexchange.com/)? –

+0

Grazie, ho appena postato lì ora. – LanneR

risposta

1

non ho lattice a portata di mano e non lo hanno fatto a lungo, ma:

1) non \ foreach richiedono una gamma? Se ho ben capito il codice correttamente, \ sEspr valuta solo per la lunghezza (ad es. {6}), mentre \ foreach dovrebbe avere {1..6}

o

2) un altro errore tipico è indici di corsa {0 .. [length-1]} invece di {1 .. [length]}, che potrebbe portare a "input inaspettato" mentre l'interprete tenta di superare l'ultimo elemento.

0

Sembra che dovresti dare un'occhiata a expl3. Non uso R/Sweave, ma ecco un esempio che dovrebbe farti andare.

\documentclass{article} 
\usepackage{xparse,enumitem} 

\ExplSyntaxOn 

\cs_new_protected:Nn \lanner_sweave_wrap:n 
    { \item \Sexpr { str_trim(unlist(strsplit(#1, "[.]"))[2]) } } 

\cs_new_protected:Nn \lanner_sweave_enumlist:nn 
    { 
    \begin{enumerate}[#2] 
     \seq_set_split:Nnn \l_tmpa_seq { ;;; } { #1 } 
     \seq_map:NN \l_tmpa_seq \lanner_sweave_wrap:n 
    \end{enumerate} 
    } 
\cs_generate_variant:Nn \lanner_sweave_enumlist:nn { f } 

\NewDocumentCommand \EnumerateIt { O{label=\Alph*., itemsep=-1ex} m } 
    { \lanner_sweave_enumlist:fn {#1} {#2} } 

\ExplSyntaxOff 

\begin{document} 

<<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") 
@ 

\EnumerateIt{\Sexpr{join(myList,";;;")}} % join myList with ";;;" -- I don't know the exact syntax 

\end{document} 

Naturalmente, questo può essere fatto con puro expl3:

\documentclass{article} 
\usepackage{xparse,enumitem} 

\ExplSyntaxOn 

\tl_new:N \l_lanner_tmp_tl 
\seq_new:N \l_lanner_tmp_seq 
\cs_new_protected:Nn \lanner_sweave_enumlist:nn 
    { 
    \begin{enumerate}[#2] 
     \tl_map_inline:nn {#1} 
     { 
      \seq_set_split:Nnn \l_lanner_tmp_seq { .~ } {##1} 
      \seq_pop_left:NN \l_lanner_tmp_seq \l_lanner_tmp_tl 

      \tl_set:Nf \l_lanner_tmp_tl 
      { \seq_use:Nn \l_lanner_tmp_seq { .~ } } 

      \tl_trim_spaces:N \l_lanner_tmp_tl 

      \item \l_lanner_tmp_tl 
     } 
    \end{enumerate} 
    } 
\cs_generate_variant:Nn \lanner_sweave_enumlist:nn { f } 

\NewDocumentCommand \EnumerateIt { O{label=\Alph*., itemsep=-1ex} >{ \SplitList { ;; } } m } 
    { 
    \tl_show:n{#2} 
    \lanner_sweave_enumlist:fn {#2} {#1} } 

\ExplSyntaxOff 

\begin{document} 

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

\end{document} 
Problemi correlati