在LaTeX中迭代遍历字符/字符串数组

7
这是我之前发布的问题(在R中整洁地输出Sweave中的字符字符串元素)的延续。我有一个字符串数组,并尝试将每个元素输出到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}

这为我提供了我所希望实现的正确输出:

Correct formatting

然而,我现在尝试将此迭代过程放入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
你有尝试过tex.stackexchange吗? - dingo_d
谢谢,我刚刚在那里发布了。 - user1830307
2个回答

1
我手头没有Latex并且已经很久没用了,但是:
1)\foreach不需要一个范围吗?如果我对你的代码理解正确,\Sexpr仅仅评估长度(例如{6}),而\foreach应该有{1..6}
或者
2)另一个典型错误是索引运行{0..[length-1]}而不是{1..[length]},这可能会导致“意外输入”,因为解释器试图移动到最后一个元素之后。

0
听起来你应该看看 expl3。我不使用 R/Sweave,但这里有一个例子可以帮助你入门。
\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}

当然,这可以使用纯粹的 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}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接