通过LaTeX的字符/字符串数组迭代 [英] Iterating through a character/string array in LaTeX

查看:294
本文介绍了通过LaTeX的字符/字符串数组迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是较早公布我的问题的延续(<一个href=\"http://stackoverflow.com/questions/30982161/outputting-character-string-elements-neatly-in-sweave-in-r\">Outputting字符串元素整齐Sweave R中)。我有一个字符串数组,并正尝试输出的每个元素转换成乳胶。我能够做到这一点使用下面的code:

This is a continuation of my question posted earlier (Outputting character string elements neatly in Sweave in R). 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:

不过,我现在试图让这个迭代的for循环发生,因为我可能不会永远在我chapter_outcomes字符串数组正好是三要素。我曾尝试以下的变体:

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}

然而,这将导致意外输入的误差在用于chapter_outcomes上述语法[\\ I]。

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

我试图寻找类似的帖子(迭代的乳胶,<一个href=\"http://www.bytemining.com/2010/04/some-latex-gems-part-1-tikz-loops-and-more/\">http://www.bytemining.com/2010/04/some-latex-gems-part-1-tikz-loops-and-more/)但他们的重点是不同的不够,我不能将其应用到这里解决我的问题。

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.

推荐答案

我没有乳胶得心应手,还没有做到很长一段时间,但是:

I don't have Latex handy and have not done it a long time, but :

1)不\\的foreach要求的范围内?如果我没有理解你的code,\\ Sexpr评估只是长度(如:{6}),而\\的foreach应该有{} 1..6

1) doesn't \foreach require a range? If I understand your code correctly, \Sexpr evaluates just to the length (eg. {6}), whereas \foreach should have {1..6}

2)另一个典型的错误是在运行索引{0 .. [长度-1]}代替{1 .. [长度]},这可能导致意外输入作为帧间preTER试运行过去的最后一个元素。

2) another typical error is indexes running {0..[length-1]} instead of {1..[length]} , which could lead to "unexpected input" as the interpreter tries to move past the last element.

这篇关于通过LaTeX的字符/字符串数组迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆