2021-02-06 15:50:02 +01:00
|
|
|
# lib_gen_main_tex.rb
|
|
|
|
# -- Library ruby script to generate main.tex.
|
|
|
|
|
2021-04-12 15:06:26 +02:00
|
|
|
def gen_main_tex directory, texfilenames, appendixfilenames=nil
|
2021-02-06 15:50:02 +01:00
|
|
|
# parameter: directory: the destination directory to put generated files.
|
|
|
|
# texfilenames: an array of latex files. Each of them is "secXX.tex" where XX is digits.
|
|
|
|
|
|
|
|
# ------ Generate helper.tex ------
|
|
|
|
# Get preamble from a latex file generated by pandoc.
|
2021-03-03 15:45:54 +01:00
|
|
|
# 1. Generate sample latex file by `pandoc -s --listings -o sample.tex sample.md`
|
2021-02-06 15:50:02 +01:00
|
|
|
# 2. Extract the preamble of sample.tex.
|
|
|
|
# 3. Add geometry package.
|
|
|
|
|
|
|
|
sample_md = <<'EOS'
|
|
|
|
# title
|
|
|
|
|
|
|
|
line1
|
|
|
|
|
|
|
|
~~~C
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
}
|
|
|
|
~~~
|
2021-03-03 15:45:54 +01:00
|
|
|
|
|
|
|
|English|Japanese|
|
|
|
|
|:-----:|:------:|
|
|
|
|
|potato|jagaimo|
|
|
|
|
|carrot|ninjin|
|
|
|
|
|onion|tamanegi|
|
|
|
|
|
2021-02-06 15:50:02 +01:00
|
|
|
EOS
|
|
|
|
|
2021-02-09 10:16:52 +01:00
|
|
|
File.write "sample.md", sample_md
|
2021-03-03 15:45:54 +01:00
|
|
|
if (! system("pandoc", "-s", "--listings", "-o", "sample.tex", "sample.md"))
|
2021-02-06 15:50:02 +01:00
|
|
|
raise ("pandoc retuns error status #{$?}.\n")
|
|
|
|
end
|
|
|
|
sample_tex = File.read("sample.tex")
|
|
|
|
sample_tex.gsub!(/\\documentclass\[[^\]]*\]{[^}]*}/,"")
|
|
|
|
sample_tex.gsub!(/\\usepackage\[[^\]]*\]{geometry}/,"")
|
|
|
|
sample_tex.gsub!(/\\usepackage\[[^\]]*\]{graphicx}/,"")
|
2021-02-09 10:16:52 +01:00
|
|
|
sample_tex.gsub!(/\\setcounter{secnumdepth}{-\\maxdimen} % remove section numbering/,"")
|
2021-02-06 15:50:02 +01:00
|
|
|
sample_tex.gsub!(/\\author{[^}]*}/,"")
|
|
|
|
sample_tex.gsub!(/\\date{[^}]*}/,"")
|
|
|
|
preamble = []
|
|
|
|
sample_tex.each_line do |l|
|
2021-02-09 10:16:52 +01:00
|
|
|
if l =~ /\\begin{document}/
|
|
|
|
break
|
|
|
|
elsif l != "\n"
|
|
|
|
preamble << l
|
2021-02-06 15:50:02 +01:00
|
|
|
end
|
|
|
|
end
|
2021-02-09 10:16:52 +01:00
|
|
|
preamble << "\\usepackage[margin=2.4cm]{geometry}\n"
|
|
|
|
preamble << "\\usepackage{graphicx}\n"
|
2021-03-03 15:45:54 +01:00
|
|
|
preamble << "\\lstdefinelanguage[]{turtle}{\n"
|
|
|
|
preamble << " keywords={pu, pd, pw, fd, tr, bc, fc, if, rt, rs, dp},\n"
|
|
|
|
preamble << " comment=[l]\\#\n"
|
|
|
|
preamble << "}\n"
|
|
|
|
preamble << "[keywords, comments]\n"
|
|
|
|
preamble << "\\lstset {\n"
|
|
|
|
preamble << " extendedchars=true,\n"
|
|
|
|
preamble << " basicstyle=\\small\\ttfamily,\n"
|
|
|
|
preamble << " keywordstyle=\\color{red},\n"
|
|
|
|
preamble << " commentstyle=\\color{gray},\n"
|
|
|
|
preamble << " stringstyle=\\color{blue},\n"
|
|
|
|
preamble << " breaklines=true,\n"
|
|
|
|
preamble << " breakatwhitespace=true\n"
|
|
|
|
preamble << "}\n"
|
2021-02-06 15:50:02 +01:00
|
|
|
File.write("#{directory}/helper.tex",preamble.join)
|
|
|
|
File.delete("sample.md")
|
|
|
|
File.delete("sample.tex")
|
|
|
|
|
|
|
|
# ------ Generate main.tex ------
|
|
|
|
|
|
|
|
main = <<'EOS'
|
|
|
|
\documentclass[a4paper]{article}
|
|
|
|
\include{helper.tex}
|
|
|
|
\title{Gtk4 tutorial for beginners}
|
|
|
|
\author{Toshio Sekiya}
|
|
|
|
\date{}
|
|
|
|
\begin{document}
|
|
|
|
\maketitle
|
|
|
|
\begin{abstract}
|
|
|
|
\input{abstract.tex}
|
|
|
|
\end{abstract}
|
|
|
|
\newpage
|
|
|
|
\tableofcontents
|
|
|
|
\newpage
|
|
|
|
EOS
|
|
|
|
|
|
|
|
texfilenames.each do |filename|
|
|
|
|
main += " \\input{#{filename}}\n"
|
|
|
|
end
|
2021-02-09 10:16:52 +01:00
|
|
|
main += "\\newpage\n"
|
|
|
|
main += "\\appendix\n"
|
|
|
|
appendixfilenames.each do |filename|
|
|
|
|
main += " \\input{#{filename}}\n"
|
|
|
|
end
|
2021-02-06 15:50:02 +01:00
|
|
|
main += "\\end{document}\n"
|
|
|
|
IO.write("#{directory}/main.tex", main)
|
|
|
|
end
|