使用 LaTex 中的循环生成多个文档

使用 LaTex 中的循环生成多个文档

我想在 LaTeX 中生成多个文档。每个应创建的文档都有一个值列表,在示例中为\mylist。我的第一种方法是使用\foreach函数定义变量,并在循环体中使用占位符编写我的文档。当然这不是一个好的解决方案,但它显示了我想要做的事情。

我正在寻找一种方法来设置序言中的变量或类似内容。目标是运行代码并获取多个 PDF 文件,每个循环运行一个,并带有目录等。

\documentclass{article}
\usepackage{pgffor}
\usepackage{graphicx} 

\newcommand{\mylist}{A/first/red, B/second/green}

\begin{document}

\foreach \i / \j / \k in \mylist {
    \clearpage

    \section{The letter \i}

    The letter \i ~is the \j ~letter of the alphabet. 

    \section{The coulor of the letter \i}

    It is represented by the coulor \k.

    \includegraphics{./\k.png}

    \clearpage
    }

\end{document}

如果无法获取多个 PDF 文件,也许有办法将一个文件生成为文档链。这当然也会有所帮助。

谢谢

编辑

受 Roland Smith 评论的启发,我使用 R 循环遍历 tex 代码并生成多个 PDF 文档。我仍然对纯 tex 代码解决方案感兴趣。

在 R 中也有用于连接 R 和 tex 的knitr包或sweave函数,但我使用了 tools 包,因为它的工作方式与它们相反。虽然knitrsweave允许将 R 代码插入 tex 代码,但 tools 包可用于将 tex 代码插入 R 代码。这是我的方法:

(要运行代码,您需要 R 脚本、一个名为 tex 文件empty.tex和一个文件夹中的两个 *.png(“红色”和“绿色”)。)

## set working directory to source file location
this.dir <- dirname(parent.frame(2)$ofile)
setwd(this.dir)

require(tools)

## data frame with values used in the documents
## each row is for one document ( == one lopp run)

values <- data.frame(letter = c("A", "B"),
                     position = c("first", "second"),
                     color = c("red", "green"),
                     stringsAsFactors = FALSE)

## generating Textcode
## use "\\" instead of "\" in the tex code since R quotes "\\" as "\"
## write "\n" after every line of tex code for better legibility
## "\n" means newline in R

## preamble code which is constant for all documents

preamble.const <- paste0("\\documentclass{article}", "\n",
                         "\\usepackage{graphicx}", "\n")

## define the document environment commands
begin <- "\\begin{document} \n"
end   <- "\\end{document} \n"

for (i in 1:nrow(values)) {

  ## set the variables for the current document
  ## as the variable part of the preamble

  preamble.var <- paste0(
    "\\newcommand{\\letter}{", values$letter[i], "}", "\n",
    "\\newcommand{\\position}{", values$position[i], "}", "\n",
    "\\newcommand{\\coulor}{", values$color[i], "}", "\n")

  ## the document body

  body <- paste0(
    "\\section{The letter \\letter}",
    "\n", "\n",
    "The letter \\letter ~is the \\position ~letter of the alphabet.",
    "\n", "\n",
    "\\section{The coulor of the letter \\letter}",
    "\n", "\n",
    "It is represented by the coulor \\coulor.",
    "\n", "\n",
    "\\includegraphics{./\\coulor.png}",
    "\n"
  )

  ## join to comlete Tex code

  tex_code <- paste0(
    preamble.const,
    preamble.var,
    "\n", ## blank line after preamble
    begin,
    "\n", ## blank line after \begin{document}
    body,
    "\n", ## blank line before \begin{document}
    end
  )

  ## the name of the emerging document (name.pdf)
  name <- paste0("letter_", values$letter[i])

  ## generate the temporary *.tex file used in this loop run 
  file.copy(
    from = paste0(getwd(), "/empty.tex"),
    to = paste0(getwd(), "/",name , ".tex") )


  ## set the path of the *.tex file to be used
  tex.path <- paste0(getwd(), "/",name , ".tex")

  ## write code to *.tex file
  cat(tex_code, file = tex.path)

  ## compile *.tex file
  texi2pdf(tex.path, clean = TRUE)

  ## delete the temporary *.tex file used in this loop run
  file.remove(paste0(getwd(), "/",name , ".tex") )

}

## empty the empty.tex file (not really necessary, just to get the initial state) )
cat( "", file = paste0(getwd(), "/empty.tex") )

相关内容