2015-06-23 17 views
7

Đây là sự tiếp nối câu hỏi của tôi được đăng trước đó (Outputting character string elements neatly in Sweave in R). Tôi có một chuỗi các chuỗi, và đang cố gắng xuất mỗi phần tử vào LaTeX. Tôi có thể đạt được điều đó bằng cách sử dụng mã sau:Lặp lại thông qua mảng ký tự/chuỗi trong 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} 

này cung cấp cho tôi những kết quả chính xác mà tôi đã mong muốn đạt được:

Correct formatting

Tuy nhiên, bây giờ tôi đang cố gắng để có được lặp đi lặp lại điều này xảy ra trong một cho vòng lặp, như tôi có thể không phải lúc nào cũng có chính xác ba phần tử trong chuỗi chapter_outcomes của tôi. Tôi đã thử các biến thể sau:

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

Tuy nhiên, điều này dẫn đến lỗi "đầu vào bất ngờ" theo cú pháp trên cho chapter_outcomes [\ i].

Tôi đã thử xem các bài viết tương tự (Iteration in LaTeX, http://www.bytemining.com/2010/04/some-latex-gems-part-1-tikz-loops-and-more/) nhưng trọng tâm của chúng đủ khác nhau nên tôi không thể áp dụng nó để giải quyết vấn đề của mình ở đây.

Cảm ơn bạn đã được tư vấn.

+1

bạn đã thử [tex.stackexchange] (http://tex.stackexchange.com/)? –

+0

Cảm ơn, tôi vừa mới đăng ở đó ngay bây giờ. – LanneR

Trả lời

1

Tôi không có Latex tiện dụng và đã không thực hiện nó trong một thời gian dài, nhưng:

1) không \ foreach yêu cầu phạm vi? Nếu tôi hiểu mã của bạn một cách chính xác, \ Sexpr đánh giá chỉ để chiều dài (ví dụ. {6}), trong khi \ foreach nên có {1..6}

hoặc

2) một lỗi điển hình là chỉ số chạy {0 .. [length-1]} thay vì {1 .. [length]}, điều này có thể dẫn đến "đầu vào không mong muốn" khi trình thông dịch cố di chuyển qua phần tử cuối cùng.

0

Có vẻ như bạn nên xem expl3. Tôi không sử dụng R/Sweave, nhưng đây là một ví dụ mà sẽ giúp bạn đi.

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

Tất nhiên, điều này có thể được thực hiện với expl3 tinh khiết:

\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} 
Các vấn đề liên quan