Skip to content

Commit

Permalink
Merge pull request #5 from davibarreira/article
Browse files Browse the repository at this point in the history
Adding new templates :article and :matharticle
  • Loading branch information
davibarreira authored Oct 29, 2021
2 parents 2239012 + ce2d357 commit 8e25d91
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "NotebookToLaTeX"
uuid = "4acc56a1-9938-4ee1-a82a-abdf2d4f6bfc"
authors = ["Davi Barreira <[email protected]> and contributors"]
version = "0.1.0"
version = "0.1.1"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ with an extra argument providing the target directory for the LaTeX files, e.g.:
```
This will create a `./project/` folder instead of the `./build_latex`.

If instead you just want a simple report containing the Notebook,
you can use the `:article` template.

### Templates

At the moment, the available templates are:
* `:book` - The standard LaTeX book template;
* `:mathbook` - Very similar to `:book`, but with some extra packages for mathematics;
* `:article` - Simple template using the `article` document class;
* `:matharticle` - The `article` template with extra packages for mathematics.

Read the [documentation](https://davibarreira.github.io/NotebookToLaTeX.jl/dev) for more information.


Expand Down
7 changes: 6 additions & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ with an extra argument providing the target directory for the LaTeX files, e.g.:
```
This will create a `./project/` folder instead of the `./build_latex`.

If instead you just want a simple report containing the Notebook,
you can use the `:article` template.

### Font - JuliaMono

Note that when you run `notebooktolatex` without providing a `fontpath`,
Expand All @@ -86,7 +89,9 @@ correctly find your fonts. You can also do this manually by changing the `julia_

At the moment, the available templates are:
* `:book` - The standard LaTeX book template;
* `:mathbook` - Very similar to `:book`, but with some extra packages already imported.
* `:mathbook` - Very similar to `:book`, but with some extra packages already imported;
* `:article` - Simple template using the `article` document class.
* `:matharticle` - The `article` template with extra packages for mathematics.

### Plots and Images

Expand Down
8 changes: 5 additions & 3 deletions src/NotebookToLaTeX.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,11 @@ function plutotolatex(notebookname, targetdir="./build_latex"; template=:book, f
for i in nb[:order]
if nb[:celltype][i] == "markdown"
if startswith(strip(nb[:contents][i]), "md\"\"\"")
parsed = markdowntolatex(strip(nb[:contents][i])[7:end-3], targetdir, nb[:notebookdir])*"\n\n"
parsed = markdowntolatex(strip(nb[:contents][i])[7:end-3],
targetdir, nb[:notebookdir], template=template)*"\n\n"
elseif startswith(strip(nb[:contents][i]), "md\"")
parsed = markdowntolatex(strip(nb[:contents][i])[4:end-1], targetdir, nb[:notebookdir])*"\n\n"
parsed = markdowntolatex(strip(nb[:contents][i])[4:end-1],
targetdir, nb[:notebookdir], template=template)*"\n\n"
else
throw(DomainError("Markdown cell must start with either md\"\"\" or md\"."))
end
Expand Down Expand Up @@ -289,7 +291,7 @@ function jupytertolatex(notebook, targetdir="./build_latex"; template=:book, fon

# Checks whether the cell has markdown
if get(cell,"cell_type", nothing) == "markdown" || get(cell,"cell_type", nothing) == "raw"
parsed = markdowntolatex(strip(join(cell["source"])), targetdir, notebookdir)
parsed = markdowntolatex(strip(join(cell["source"])), targetdir, notebookdir, template=template)
write(f,parsed)

# Checks whether the cell has code and whether the code is hidden
Expand Down
30 changes: 22 additions & 8 deletions src/markdowntolatex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function parseparagraph(paragraph, targetdir, notebookdir)
return parsedparagraph
end

function markdowntolatex(md, targetdir, notebookdir)
function markdowntolatex(md, targetdir, notebookdir; template=:book)
tag = false
component = ""
parsedtext = ""
Expand All @@ -133,26 +133,40 @@ function markdowntolatex(md, targetdir, notebookdir)
end
continue
elseif startswith(l, "####")
parsedtext *= "\n\\subsubsection{" * l[6:end] * "}\n"
if template == :article || template == :matharticle
parsedtext *= "\n\\subsubsubsection{" * l[6:end] * "}\n"
else
parsedtext *= "\n\\subsubsection{" * l[6:end] * "}\n"
end
continue
elseif startswith(l, "###")
parsedtext *= "\n\\subsection{" * l[5:end] * "}\n"
if template == :article || template == :matharticle
parsedtext *= "\n\\subsubsection{" * l[5:end] * "}\n"
else
parsedtext *= "\n\\subsection{" * l[5:end] * "}\n"
end
continue
elseif startswith(l, "##")
parsedtext *= "\n\\section{" * l[4:end] * "}\n"
if template == :article || template == :matharticle
parsedtext *= "\n\\subsection{" * l[4:end] * "}\n"
else
parsedtext *= "\n\\section{" * l[4:end] * "}\n"
end
continue
elseif startswith(l, "#")
parsedtext *= "\n\\chapter{" * l[3:end] * "}\n"
if template == :article || template == :matharticle
parsedtext *= "\n\\section{" * l[3:end] * "}\n"
else
parsedtext *= "\n\\chapter{" * l[3:end] * "}\n"
end
continue
end


if tag
parsedtext *= "\t" * l * "\n"
elseif !tag
#= if !startswith(l, "#") =#
parsedtext *= parseparagraph(l, targetdir, notebookdir)
#= end =#
parsedtext *= parseparagraph(l, targetdir, notebookdir)
end
end
return parsedtext
Expand Down
45 changes: 28 additions & 17 deletions src/templates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,49 @@ function createtemplate(path=".", template=:book)

elseif template == :mathbook
tex = "%! TeX program = lualatex\n\\documentclass[12pt, oneside]{book}\n\n\\usepackage{listings}\n\n\\pagestyle{plain}\n\\usepackage{pdfpages}\n\\usepackage{titlesec}\n\n%%%% MATH PACKAGES %%%%\n\n\\usepackage{amsfonts, amsthm,amsmath,amssymb,mathtools}\n\\usepackage{bbm}\n\\usepackage{bm}\n\\usepackage{mathtools}\n\\usepackage{thmtools} % List of Theorems\n\n%%%%%%%%%%%%%%%%%%%%%%%\n\n\\usepackage[square,numbers]{natbib}\n\\usepackage[pdftex,bookmarks=true,bookmarksopen=false,bookmarksnumbered=true,colorlinks=true,linkcolor=blue]{hyperref}\n\\usepackage[utf8]{inputenc}\n\\usepackage{float}\n\\usepackage{enumerate}\n\n%%%%%%% JULIA %%%%%%%%%%\n\\input{julia_font}\n\\input{julia_listings}\n\\input{julia_listings_unicode}\n\n\\lstdefinelanguage{JuliaLocal}{\n language = Julia, % inherit Julia lang. to add keywords\n morekeywords = [3]{thompson_sampling}, % define more functions\n morekeywords = [2]{Beta, Distributions}, % define more types and modules\n}\n%%%%%%%%%%%%%%%%%%%%%%%%\n\n\n%%%%%%%%%% BOOK INFORMATION %%%%%%%%%%\n\\newcommand{\\authorname}{Name}\n\\newcommand{\\booktitle}{Title}\n\\newcommand{\\subtitle}{Subtitle}\n\\newcommand{\\publisher}{TBD}\n\\newcommand{\\editionyear}{2021}\n\\newcommand{\\isbn}{XYZ} % replace this with your own ISBN\n\n\\title{\\booktitle}\n\\author{\\authorname}\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n%%%%%%%%%%%% MATH STYLE %%%%%%%%%%%%%\n\\newtheoremstyle{bfnote}%\n {}{}\n {}{}\n {\\bfseries}{.}\n { }{\\thmname{#1}\\thmnumber{ #2}\\thmnote{ (#3)}}\n\\theoremstyle{bfnote}\n\\newenvironment{prf}[1][Proof]{\\textbf{#1.} }{\\qed}\n\\newtheorem{theorem}{Theorem}[section]\n\\newtheorem{definition}[theorem]{Definition}\n\\newtheorem{exer}{Exercise}[section]\n\\newtheorem{lemma}[theorem]{Lemma}\n\\newtheorem{corollary}[theorem]{Corollary}\n\\newtheorem{proposition}[theorem]{Proposition}\n\n\\newtheorem{note}{Note}[section]\n\\newtheorem{example}{Example}[section]\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n\\begin{document}\n\n% \\includepdf{cover.pdf}\n\n\\frontmatter\n\\input{frontmatter/titlepage}\n\\input{frontmatter/copyright}\n% \\include{preface}\n\n\\newpage\n\\tableofcontents\n\n%\\listoftheorems[onlynamed]\n\n\\mainmatter\n\\newpage\n% INCLUDE NOTEBOOKS HERE %\n\n\\bibliography{ref}\n\n\\bibliographystyle{plainnat}\n\n\\include{appendix}\n\n\\end{document}\n\n"


elseif template == :article
tex = "%! TeX program = lualatex\n\\documentclass[12pt]{article}\n\n\\usepackage{listings}\n\n\\pagestyle{plain}\n\\usepackage{pdfpages}\n\n\\usepackage[square,numbers]{natbib}\n\\usepackage[pdftex,bookmarks=true,bookmarksopen=false,bookmarksnumbered=true,colorlinks=true,linkcolor=blue]{hyperref}\n\\usepackage[utf8]{inputenc}\n\\usepackage{float}\n\\usepackage{enumerate}\n\n%%%%%%% JULIA %%%%%%%%%%\n\\input{julia_font}\n\\input{julia_listings}\n\\input{julia_listings}\n\\input{julia_listings_unicode}\n\n\\lstdefinelanguage{JuliaLocal}{\n language = Julia, % inherit Julia lang. to add keywords\n}\n%%%%%%%%%%%%%%%%%%%%%%%%\n\n\n\\begin{document}\n\n\\newpage\n% INCLUDE NOTEBOOKS HERE %\n\n\n\\bibliography{ref}\n\n\\bibliographystyle{plainnat}\n\n\\include{appendix}\n\n\\end{document}\n"

elseif template == :matharticle
tex = "%! TeX program = lualatex\n\\documentclass[12pt]{article}\n\n\\usepackage{listings}\n\n\\pagestyle{plain}\n\\usepackage{pdfpages}\n\n\\usepackage[square,numbers]{natbib}\n\\usepackage[pdftex,bookmarks=true,bookmarksopen=false,bookmarksnumbered=true,colorlinks=true,linkcolor=blue]{hyperref}\n\\usepackage[utf8]{inputenc}\n\\usepackage{float}\n\\usepackage{enumerate}\n\n%%%% MATH PACKAGES %%%%\n\n\\usepackage{amsfonts, amsthm,amsmath,amssymb,mathtools}\n\\usepackage{bbm}\n\\usepackage{bm}\n\\usepackage{mathtools}\n\\usepackage{thmtools} % List of Theorems\n\n%%%%%%% JULIA %%%%%%%%%%\n\\input{julia_font}\n\\input{julia_listings}\n\\input{julia_listings}\n\\input{julia_listings_unicode}\n\n\\lstdefinelanguage{JuliaLocal}{\n language = Julia, % inherit Julia lang. to add keywords\n}\n%%%%%%%%%%%%%%%%%%%%%%%%\n\n%%%%%%%%%%%% MATH STYLE %%%%%%%%%%%%%\n\\newtheoremstyle{bfnote}%\n {}{}\n {}{}\n {\\bfseries}{.}\n { }{\\thmname{#1}\\thmnumber{ #2}\\thmnote{ (#3)}}\n\\theoremstyle{bfnote}\n\\newenvironment{prf}[1][Proof]{\\textbf{#1.} }{\\qed}\n\\newtheorem{theorem}{Theorem}[section]\n\\newtheorem{definition}[theorem]{Definition}\n\\newtheorem{exer}{Exercise}[section]\n\\newtheorem{lemma}[theorem]{Lemma}\n\\newtheorem{corollary}[theorem]{Corollary}\n\\newtheorem{proposition}[theorem]{Proposition}\n\n\\newtheorem{note}{Note}[section]\n\\newtheorem{example}{Example}[section]\n\n\n\\begin{document}\n\n\\newpage\n% INCLUDE NOTEBOOKS HERE %\n\n\n\\bibliography{ref}\n\n\\bibliographystyle{plainnat}\n\n\\include{appendix}\n\n\\end{document}\n"
end

maintex = path * "/main.tex"
if !isfile(maintex)
open(maintex, "w") do f
write(f, tex)
end
end

preface = "\\newpage\n\\chapter*{Preface}\n\\addcontentsline{toc}{chapter}{Preface}\n"
prefacetex = path * "/preface.tex"

if !isfile(prefacetex)
open(prefacetex, "w") do f
write(f, preface)
if template == :book || template == :mathbook
preface = "\\newpage\n\\chapter*{Preface}\n\\addcontentsline{toc}{chapter}{Preface}\n"
prefacetex = path * "/preface.tex"

if !isfile(prefacetex)
open(prefacetex, "w") do f
write(f, preface)
end
end
end

titlepage = "% \\pagestyle{empty}\n\n% % Half title page\n% {\n% \\centering\n\n% ~\n\n% \\vspace{24pt}\n% {\\scshape\\Huge \\booktitle \\par}\n% }\n% \\cleardoublepage\n\n% Title page\n\\begin{titlepage}\n\t\\centering\n\n\t~\n\n\t\\vspace{24pt}\n\t{\\scshape\\Huge \\booktitle\\par}\n\t\\vspace{6pt}\n\t{\\scshape\\large \\subtitle\\par}\n\t\\vspace{\\stretch{1.25}}\n\t{\\itshape\\large by\\par}\n\t\\vspace{6pt}\n\t{\\itshape\\Large \\authorname\\par}\n\t\\vspace{\\stretch{6}}\n\t{\\large \\publisher\\par}\n\\end{titlepage}\n"
titlepage = "% \\pagestyle{empty}\n\n% % Half title page\n% {\n% \\centering\n\n% ~\n\n% \\vspace{24pt}\n% {\\scshape\\Huge \\booktitle \\par}\n% }\n% \\cleardoublepage\n\n% Title page\n\\begin{titlepage}\n\t\\centering\n\n\t~\n\n\t\\vspace{24pt}\n\t{\\scshape\\Huge \\booktitle\\par}\n\t\\vspace{6pt}\n\t{\\scshape\\large \\subtitle\\par}\n\t\\vspace{\\stretch{1.25}}\n\t{\\itshape\\large by\\par}\n\t\\vspace{6pt}\n\t{\\itshape\\Large \\authorname\\par}\n\t\\vspace{\\stretch{6}}\n\t{\\large \\publisher\\par}\n\\end{titlepage}\n"

titlepagetex = path * "/frontmatter/titlepage.tex"
if !isfile(titlepagetex)
open(titlepagetex, "w") do f
write(f, titlepage)
titlepagetex = path * "/frontmatter/titlepage.tex"
if !isfile(titlepagetex)
open(titlepagetex, "w") do f
write(f, titlepage)
end
end
end

copyright = "% Copyright page\n\n{\\small\n\\setlength{\\parindent}{0em}\\setlength{\\parskip}{1em}\n\n~\n\n\\vfill\n\nCopyright \\copyright{} 2021 \\authorname\n\nAll rights reserved. No part of this publication may be reproduced, stored or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning, or otherwise without written permission from the publisher. It is illegal to copy this book, post it to a website, or distribute it by any other means without permission.\n\nFirst edition, \\editionyear{}\n\nISBN \\isbn{} % see main.tex\n\nPublished by \\publisher{}\n}\n"
copyright = "% Copyright page\n\n{\\small\n\\setlength{\\parindent}{0em}\\setlength{\\parskip}{1em}\n\n~\n\n\\vfill\n\nCopyright \\copyright{} 2021 \\authorname\n\nAll rights reserved. No part of this publication may be reproduced, stored or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning, or otherwise without written permission from the publisher. It is illegal to copy this book, post it to a website, or distribute it by any other means without permission.\n\nFirst edition, \\editionyear{}\n\nISBN \\isbn{} % see main.tex\n\nPublished by \\publisher{}\n}\n"

copyrighttex = path * "/frontmatter/copyright.tex"
if !isfile(copyrighttex)
open(copyrighttex, "w") do f
write(f, copyright)
copyrighttex = path * "/frontmatter/copyright.tex"
if !isfile(copyrighttex)
open(copyrighttex, "w") do f
write(f, copyright)
end
end
end
end
Expand Down
39 changes: 39 additions & 0 deletions templates/article.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
%! TeX program = lualatex
\documentclass[12pt]{article}

\usepackage{listings}

\pagestyle{plain}
\usepackage{pdfpages}

\usepackage[square,numbers]{natbib}
\usepackage[pdftex,bookmarks=true,bookmarksopen=false,bookmarksnumbered=true,colorlinks=true,linkcolor=blue]{hyperref}
\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage{enumerate}

%%%%%%% JULIA %%%%%%%%%%
\input{julia_font}
\input{julia_listings}
\input{julia_listings}
\input{julia_listings_unicode}

\lstdefinelanguage{JuliaLocal}{
language = Julia, % inherit Julia lang. to add keywords
}
%%%%%%%%%%%%%%%%%%%%%%%%


\begin{document}

\newpage
% INCLUDE NOTEBOOKS HERE %


\bibliography{ref}

\bibliographystyle{plainnat}

\include{appendix}

\end{document}
65 changes: 65 additions & 0 deletions templates/matharticle.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
%! TeX program = lualatex
\documentclass[12pt]{article}

\usepackage{listings}

\pagestyle{plain}
\usepackage{pdfpages}

\usepackage[square,numbers]{natbib}
\usepackage[pdftex,bookmarks=true,bookmarksopen=false,bookmarksnumbered=true,colorlinks=true,linkcolor=blue]{hyperref}
\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage{enumerate}

%%%% MATH PACKAGES %%%%

\usepackage{amsfonts, amsthm,amsmath,amssymb,mathtools}
\usepackage{bbm}
\usepackage{bm}
\usepackage{mathtools}
\usepackage{thmtools} % List of Theorems

%%%%%%% JULIA %%%%%%%%%%
\input{julia_font}
\input{julia_listings}
\input{julia_listings}
\input{julia_listings_unicode}

\lstdefinelanguage{JuliaLocal}{
language = Julia, % inherit Julia lang. to add keywords
}
%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%% MATH STYLE %%%%%%%%%%%%%
\newtheoremstyle{bfnote}%
{}{}
{}{}
{\bfseries}{.}
{ }{\thmname{#1}\thmnumber{ #2}\thmnote{ (#3)}}
\theoremstyle{bfnote}
\newenvironment{prf}[1][Proof]{\textbf{#1.} }{\qed}
\newtheorem{theorem}{Theorem}[section]
\newtheorem{definition}[theorem]{Definition}
\newtheorem{exer}{Exercise}[section]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{proposition}[theorem]{Proposition}

\newtheorem{note}{Note}[section]
\newtheorem{example}{Example}[section]


\begin{document}

\newpage
% INCLUDE NOTEBOOKS HERE %


\bibliography{ref}

\bibliographystyle{plainnat}

\include{appendix}

\end{document}
42 changes: 42 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,48 @@ using Test
rm(jupyterpath, recursive=true)
end

@testset "Template :article and :matharticle" begin
jupyterpath = "./jupyter/build_latex/"

for template in [:article, :matharticle]
notebooktolatex("./jupyter/jupyternotebook.ipynb", jupyterpath, template=template)
@test isfile(jupyterpath * "main.tex")
@test !isfile(jupyterpath * "preface.tex")
@test !isfile(jupyterpath * "frontmatter/copyright.tex")
@test !isfile(jupyterpath * "frontmatter/titlepage.tex")
@test isfile(jupyterpath * "julia_font.tex")
@test isfile(jupyterpath * "julia_listings.tex")
@test isfile(jupyterpath * "julia_listings_unicode.tex")
@test isfile(jupyterpath * "/notebooks/jupyternotebook.tex")
@test isfile(jupyterpath * "/fonts/JuliaMono_Bold.ttf")
@test isfile(jupyterpath * "/fonts/JuliaMono_Medium.ttf")
@test isfile(jupyterpath * "/fonts/JuliaMono_Regular.ttf")
@test isfile(jupyterpath * "/figures/figure.pdf")
@test isfile(jupyterpath * "/figures/jupyternotebook_figure1.pdf")
@test isfile(jupyterpath * "/figures/jupyternotebook_figure1.svg")
@test isfile(jupyterpath * "/figures/jupyternotebook_figure2.png")
@test isfile(jupyterpath * "/figures/plotexample.png")
@test contains(read(jupyterpath * "/notebooks/jupyternotebook.tex", String), "\\section{My Notebook}")
@test contains(read(jupyterpath * "/notebooks/jupyternotebook.tex", String), "\\subsection{Start importing}")
@test contains(read(jupyterpath * "/notebooks/jupyternotebook.tex", String), "\\subsubsection{Some theory}")

@test contains(read(jupyterpath * "/notebooks/jupyternotebook.tex", String), "\\subsubsection{Some theory}")

if template == :matharticle
mathspecific = ["\\usepackage{amsfonts, amsthm,amsmath,amssymb,mathtools}",
"\\usepackage{bbm}",
"\\usepackage{bm}",
"\\usepackage{mathtools}",
"\\usepackage{thmtools}"]
for mathpackage in mathspecific
@test contains(read(jupyterpath * "main.tex", String), mathpackage)
end
end

rm(jupyterpath, recursive=true)
end
end

rm(path, recursive=true)
rm("./build_latex/", recursive=true)
end

0 comments on commit 8e25d91

Please sign in to comment.