From c681ffb3b31734240b104d63bd3f5c6592da6044 Mon Sep 17 00:00:00 2001 From: Bruno Freitas Tissei <bft15@inf.ufpr.br> Date: Wed, 15 May 2019 00:29:12 -0300 Subject: [PATCH] Improve notebook generator Signed-off-by: Bruno Freitas Tissei <bft15@inf.ufpr.br> --- algorithms/graph/bellman_ford.cpp | 9 ++-- algorithms/graph/centroid_decomposition.cpp | 6 +-- gen_notebook | 3 +- notebook/gen_latex.py | 37 ++++++++++------ notebook/header.tex | 48 ++++++++++++++++----- 5 files changed, 69 insertions(+), 34 deletions(-) diff --git a/algorithms/graph/bellman_ford.cpp b/algorithms/graph/bellman_ford.cpp index 0b21186..b3b740a 100644 --- a/algorithms/graph/bellman_ford.cpp +++ b/algorithms/graph/bellman_ford.cpp @@ -24,12 +24,15 @@ struct BellmanFord { for (int i = 0; i < N; ++i) for (auto e : graph) - if (dist[e.u] != inf && dist[e.u] + e.w < dist[e.v]) + if (dist[e.u] != inf && + dist[e.u] + e.w < dist[e.v]) dist[e.v] = dist[e.u] + e.w; - // Check for negative cycles, return -inf if there is one + // Check for negative cycles, return -inf if + // there is one for (auto e : graph) - if (dist[e.u] != inf && dist[e.u] + w < dist[e.v]) + if (dist[e.u] != inf && + dist[e.u] + w < dist[e.v]) return -inf; return dist[d]; diff --git a/algorithms/graph/centroid_decomposition.cpp b/algorithms/graph/centroid_decomposition.cpp index 536a833..74a6d82 100644 --- a/algorithms/graph/centroid_decomposition.cpp +++ b/algorithms/graph/centroid_decomposition.cpp @@ -34,7 +34,6 @@ struct CentroidDecomposition { build(0); } - // Builds the centroid decomposition of the tree recursively. void build(int x, int p = -1) { int n = dfs(x); int centroid = get_centroid(x, n); @@ -47,7 +46,7 @@ struct CentroidDecomposition { build(i, centroid); } - // Calculates size of every subtree (in the original tree). + // Calculates size of every subtree. int dfs(int x, int p = -1) { size[x] = 1; for (auto i : graph[x]) @@ -57,8 +56,7 @@ struct CentroidDecomposition { return size[x]; } - // Finds centroid by recursively searching for it in the subtree - // with more than n / 2 nodes in it. + // Finds centroid. int get_centroid(int x, int n, int p = -1) { for (auto i : graph[x]) if (i != p && size[i] > n / 2 && !marked[i]) diff --git a/gen_notebook b/gen_notebook index 9366504..6f639f7 100755 --- a/gen_notebook +++ b/gen_notebook @@ -7,8 +7,7 @@ else tex_file=$(mktemp) python3 notebook/gen_latex.py --header=notebook/header.tex --output=$tex_file - #pdflatex $tex_file -halt-on-error -output-directory . && - #pdflatex $tex_file -halt-on-error -output-directory . + # Lualatex must be used in order to work with the correct font lualatex $tex_file -halt-on-error -output-directory . && lualatex $tex_file -halt-on-error -output-directory . diff --git a/notebook/gen_latex.py b/notebook/gen_latex.py index 8327fb8..6b8bf33 100644 --- a/notebook/gen_latex.py +++ b/notebook/gen_latex.py @@ -38,21 +38,26 @@ class Tree: class LatexGenerator: + class SourceFile: + def __init__(self, path): + pass + + def _parse(): + pass + def __init__(self, tree, output, header): self.output = open(output, 'w') self.header = open(header, 'r') self.tree = tree self.files = {} - #self.hierarchy = ['part', 'chapter'] + \ - # [i*'sub' + 'section' for i in range(3)] - self.hierarchy = ['chapter'] + \ - [i*'sub' + 'section' for i in range(3)] + self.hierarchy = [i*'sub' + 'section' for i in range(3)] self._add_header() - self.gen_latex(tree) - self._write('\\end{document}') + self._gen_latex(tree) + self._write('\\end{document}\n') + # Print text to output file def _write(self, text): self.output.write(text) @@ -68,15 +73,15 @@ class LatexGenerator: self._write('\n') # Prints section title in Latex format. - def gen_title_latex(self, content, depth): + def _gen_title_latex(self, content, depth): self._write('\\%s{%s}\n' % (self.hierarchy[depth], content)) # Prints code in Latex format. def gen_code_latex(self, path): - # TODO: Add code parsing funcion to extract text from comments suffix = path.split('.')[-1] self._write('\\begin{lstlisting}[style=custom%s]\n' % suffix) + # TODO: Add code parsing funcion to extract text from comments with open(path) as f: lines = f.readlines() for i in lines: @@ -86,17 +91,21 @@ class LatexGenerator: self._write('\n') # Generates Latex for entire tree recursively - def gen_latex(self, sub, path = '', depth = 0): + def _gen_latex(self, sub, path = '', depth = 0): if type(sub) == list: - self._write('\\begin{multicols}{2}\n') for i in sub: - self.gen_title_latex(self._parse_file_name(path + i), depth) + self._gen_title_latex(self._parse_file_name(path + i), depth) self.gen_code_latex(path + i) - self._write('\\end{multicols}\n') else: + if depth == 1: + self._write('\\begin{multicols}{3}\n') + for i in sub: - self.gen_title_latex(self._parse_dir_name(i), depth) - self.gen_latex(sub[i], path + i + '/', depth + 1) + self._gen_title_latex(self._parse_dir_name(i), depth) + self._gen_latex(sub[i], path + i + '/', depth + 1) + + if depth == 1: + self._write('\\end{multicols}\n') self._write('\n') diff --git a/notebook/header.tex b/notebook/header.tex index 8a182fe..e03cfb0 100644 --- a/notebook/header.tex +++ b/notebook/header.tex @@ -1,34 +1,60 @@ -\documentclass[oneside]{book} +\documentclass{article} + +\usepackage[a4paper,landscape]{geometry} \usepackage{listings} \usepackage{titlesec} \usepackage{fontspec} \usepackage{multicol} -\usepackage[a4paper,landscape,hmargin={0.5cm,0.5cm},vmargin={1.0cm,0.4cm}]{geometry} +\usepackage{titlesec} +\usepackage{fancyhdr} +% 'Fancy pages' adds header with page number and subsection title +\pagestyle{fancy} +\renewcommand{\sectionmark}[1]{\markboth{#1}{}} +\headheight 25pt +\lhead{\textbf{\leftmark}} +\rhead{\thepage} +\lfoot{}\cfoot{}\rfoot{} +\fancyheadoffset{0pt} +\fancyfootoffset{0pt} +\setlength\headsep{5pt} + +% Ubuntu mono for source code \setmonofont{Ubuntu Mono} + +% Set column separator to be visible and black \setlength{\columnseprule}{1pt} \def\columnseprulecolor{\color{black}} + +% Set margins of pages \geometry{ a4paper, - left=20mm, - right=20mm, - top=1in, - bottom=1in, + left=5mm, + right=5mm, + top=20mm, + bottom=10mm, } -\titleformat*{\subsubsection}{\large\bfseries} + +% Set sizes for *[sub]section +\titleformat*{\subsubsection}{\normalsize\bfseries} \titleformat*{\subsection}{\large\bfseries} -\titleformat*{\section}{\large\bfseries} +\titleformat*{\section}{\Large\bfseries} + +% Set style for C++ code \lstdefinestyle{customcpp}{ language=C++, - basicstyle=\footnotesize\ttfamily, + basicstyle=\scriptsize\ttfamily, tabsize=2, + frame=b, breaklines=true, morekeywords={int32\_t, ll} } + +% Set style for vimconfig file \lstdefinestyle{customvim}{ - frame=single, - basicstyle=\footnotesize\ttfamily, + basicstyle=\scriptsize\ttfamily, tabsize=2, + frame=b, breaklines=true, morekeywords={set} } -- GitLab