我想使用这样的循环生成学生反馈报告:
for student in studentList
# Report/feedback content goes here:
# Here I want to use text with variables for example
student * " received " * xPoints
"Q1"
"Good effort but missing units"
"Q2"
"More text ..."
# end of the feedback
end
我的目标是为所有学生生成30多个PDF文件,并为每个问题评分,并为每个学生提供一些免费的文本。我想到的一种方法是写入多个TeX文件,最后将它们编译为PDF。
如果有更好的方法可以在Julia中以编程方式生成多个人类可读的报告,则我不愿输出PDF。
答案 0 :(得分:5)
就目前而言,我们可以从基础开始,并输出一个更快的HTML文件。您可以使用模板库,在这种情况下,我们可以使用胡须。模板是硬编码的,但是将其包含在外部文件中很简单。
记住要安装模板库Mustache
:
import Pkg; Pkg.add("Mustache")
基本思想如下:
{{ ... }}
卫兵中您可以添加一些代码以直接向学生发送邮件,甚至不保存文件,如果您的计算机配置为这样做(只要您不包括外部CSS,邮件将被相应地格式化为HTML指令)。
using Mustache
students = [
Dict( "name" => "John", "surname" => "Smith", "mark" => 30 ),
Dict( "name" => "Elisa", "surname" => "White", "mark" => 100 )
]
tmpl = mt"""
<html>
<body>
Hello <b>{{name}}, {{surname}}</b>. Your mark is {{mark}}
</body>
</html>
"""
for student in students
rendered = render(tmpl, student)
filename = string("/tmp/", student["name"], "_", student["surname"], ".html")
open(filename, "w") do file
write(file, rendered)
end
end
一个学生的成绩是这样的:
<html>
<body>
Hello <b>Elisa, White</b>. Your mark is 100
</body>
</html>
如果您喜欢PDF ,我认为更快的方法是使用一块LaTeX作为模板(代替HTML模板),将Mustache的结果导出到文件中,然后通过系统调用从脚本进行编译:
using Mustache
students = [
Dict( "name" => "John", "surname" => "Smith", "mark" => 30 ),
Dict( "name" => "Elisa", "surname" => "White", "mark" => 100 )
]
tmpl = mt"""
\documentclass{standalone}
\begin{document}
Hello \textbf{ {{name}}, {{surname}}}. Your mark is ${{mark}}$.
\end{document}
"""
for student in students
rendered = render(tmpl, student)
filename = string("/tmp/", student["name"], "_", student["surname"], ".tex")
open(filename, "w") do file
write(file, rendered)
end
run(`pdflatex $filename`)
end
结果类似:
对Mustache.jl的引用,您可以在其中找到有关如何使用单行模板遍历不同问题的一些说明。这是一个示例,其中标记是值的数组(同样是tex):
using Mustache
students = [
Dict( "name" => "John", "surname" => "Smith", "marks" => [25, 32, 40, 38] ),
Dict( "name" => "Elisa", "surname" => "White", "marks" => [40, 40, 36, 35] )
]
tmpl = """
\\documentclass{article}
\\begin{document}
Hello \\textbf{ {{name}}, {{surname}} }. Your marks are:
\\begin{itemize}
{{#marks}}
\\item Mark for question is {{.}}
{{/marks}}
\\end{itemize}
\\end{document}
"""
for student in students
rendered = render(tmpl, student)
filename = string("/tmp/", student["name"], "_", student["surname"], ".tex")
open(filename, "w") do file
write(file, rendered)
end
run(`pdflatex $filename`)
end
结果为: