├── .gitignore ├── README.md ├── examples ├── example.pdf ├── simple_table.tex ├── line_graph.tex ├── table_with_lookup.tex ├── point_graph.tex └── example.tex ├── tests └── tests.go ├── main.go └── latex └── main.go /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | *.gz 3 | *.aux 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | go-latex 2 | ===== 3 | 4 | LaTeX library for Golang 5 | -------------------------------------------------------------------------------- /examples/example.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apourchet/go-latex/HEAD/examples/example.pdf -------------------------------------------------------------------------------- /examples/simple_table.tex: -------------------------------------------------------------------------------- 1 | a & b & c & d & e \\ \hline 2 | $f$ & $g$ & $h$ & $i$ & $j$ \\ \hline 3 | -------------------------------------------------------------------------------- /examples/line_graph.tex: -------------------------------------------------------------------------------- 1 | \addplot [black] coordinates { 2 | (0.000000,0.000000) 3 | (1.000000,1.000000) 4 | (2.000000,2.000000) 5 | (3.000000,3.000000) 6 | }; -------------------------------------------------------------------------------- /examples/table_with_lookup.tex: -------------------------------------------------------------------------------- 1 | Mode/Letter & Letter 1 & Letter 1 & Letter 1 & Letter 1 & Letter 1 \\ \hline 2 | No Math Mode & a & b & c & d & e \\ \hline 3 | Math Mode & $f$ & $g$ & $h$ & $i$ & $j$ \\ \hline 4 | -------------------------------------------------------------------------------- /examples/point_graph.tex: -------------------------------------------------------------------------------- 1 | \addplot+[only marks, mark=o] coordinates{ 2 | (2.000000,2.000000) 3 | (3.000000,3.000000) 4 | }; 5 | \addplot+[only marks, mark=x] coordinates{ 6 | (0.000000,0.000000) 7 | (1.000000,1.000000) 8 | }; 9 | -------------------------------------------------------------------------------- /tests/tests.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "../../go-latex" 5 | ) 6 | 7 | func main() { 8 | entries := [][]latex.Entry{{{false, "a"}, {false, "b"}, {false, "c"}, {false, "d"}, {false, "e"}}, 9 | {{true, "f"}, {true, "g"}, {true, "h"}, {true, "i"}, {true, "j"}}} 10 | latex.MakeTable("../examples/simple_table.tex", entries) 11 | cornerValue := "Mode/Letter" 12 | table := latex.Table{cornerValue, []string{"Letter 1", "Letter 1", "Letter 1", "Letter 1", "Letter 1"}, []string{"No Math Mode", "Math Mode"}, entries} 13 | latex.MakeTableWithLookup("../examples/table_with_lookup.tex", table) 14 | 15 | linePts := []latex.Point{{"", 0., 0.}, {"", 1., 1.}, {"", 2., 2.}, {"", 3., 3.}} 16 | coloredPts := []latex.Point{{"1", 0., 0.}, {"1", 1., 1.}, {"-1", 2., 2.}, {"-1", 3., 3.}} 17 | 18 | labelToMark := make(map[string]string) 19 | labelToMark["-1"] = "o" 20 | labelToMark["1"] = "x" 21 | 22 | latex.MakeLineGraph("../examples/line_graph.tex", linePts) 23 | latex.MakeColoredPointGraph("../examples/point_graph.tex", labelToMark, coloredPts) 24 | } 25 | -------------------------------------------------------------------------------- /examples/example.tex: -------------------------------------------------------------------------------- 1 | \documentclass[]{article} 2 | 3 | \usepackage{amssymb} 4 | \usepackage{amsthm} 5 | \usepackage{amsmath} 6 | \usepackage{pgfplots} 7 | \usepackage{tikz} 8 | \usepackage[margin=0.8in]{geometry} 9 | \usepackage{scrextend} 10 | 11 | \begin{document} 12 | \begin{center} 13 | Simple table:\\ 14 | \begin{tabular}{|c|c|c|c|c|} 15 | \hline 16 | \input{./simple_table.tex} 17 | \end{tabular} 18 | \end{center} 19 | 20 | \begin{center} 21 | Table with lookup:\\ 22 | \begin{tabular}{|c|c|c|c|c|c|} 23 | \hline 24 | \input{./table_with_lookup.tex} 25 | \end{tabular} 26 | \end{center} 27 | 28 | \begin{center} 29 | \begin{tikzpicture} 30 | \begin{axis}[xlabel = $x_1$, ylabel = $x_2$, title = Point Graph, grid=both, mark size = 3, thick] 31 | \input{./line_graph.tex} 32 | \end{axis} 33 | \end{tikzpicture} 34 | \end{center} 35 | 36 | \begin{center} 37 | \begin{tikzpicture} 38 | \begin{axis}[xlabel = $x_1$, ylabel = $x_2$, title = Point Graph, grid=both, mark size = 3, thick] 39 | \input{./point_graph.tex} 40 | \end{axis} 41 | \end{tikzpicture} 42 | \end{center} 43 | 44 | \end{document} -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package latex 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "io/ioutil" 7 | ) 8 | 9 | type Point struct { 10 | Label string 11 | X, Y float64 12 | } 13 | 14 | func (pt *Point) ToString() string { 15 | return fmt.Sprintf("(%f,%f)", pt.X, pt.Y) 16 | } 17 | 18 | type Entry struct { 19 | MathMode bool 20 | Value string 21 | } 22 | 23 | func (entry *Entry) ToString() string { 24 | if entry.MathMode { 25 | return "$" + entry.Value + "$" 26 | } 27 | return entry.Value 28 | } 29 | 30 | type Table struct { 31 | CornerValue string 32 | HorizontalLookup []string 33 | VerticalLookup []string 34 | Entries [][]Entry 35 | } 36 | 37 | func MakeTable(fileName string, entries [][]Entry) { 38 | output := bytes.NewBufferString("") 39 | for _, row := range entries { 40 | str := "" 41 | for colNumber, col := range row { 42 | if colNumber == 0 { 43 | str += col.ToString() + " " 44 | } else { 45 | str += "& " + col.ToString() + " " 46 | } 47 | } 48 | output.WriteString(str + "\\\\ \\hline\n") 49 | } 50 | ioutil.WriteFile(fileName, output.Bytes(), 777) 51 | } 52 | 53 | func MakeTableWithLookup(fileName string, tbl Table) { 54 | output := bytes.NewBufferString(tbl.CornerValue + " ") 55 | for _, topVal := range tbl.HorizontalLookup { 56 | output.WriteString("& " + topVal + " ") 57 | } 58 | output.WriteString("\\\\ \\hline\n") 59 | for rowNumber, row := range tbl.Entries { 60 | str := tbl.VerticalLookup[rowNumber] + " " 61 | for _, col := range row { 62 | str += "& " + col.ToString() + " " 63 | } 64 | output.WriteString(str + "\\\\ \\hline\n") 65 | } 66 | ioutil.WriteFile(fileName, output.Bytes(), 777) 67 | } 68 | 69 | func MakeColoredPointGraph(fileName string, labelToMark map[string]string, points []Point) { 70 | output := bytes.NewBufferString("") 71 | for label, mark := range labelToMark { 72 | output.WriteString("\\addplot+[only marks, mark=" + mark + "] coordinates{\n") 73 | for _, p := range points { 74 | if p.Label == label { 75 | output.WriteString(p.ToString() + "\n") 76 | } 77 | } 78 | output.WriteString("};\n") 79 | } 80 | ioutil.WriteFile(fileName, output.Bytes(), 777) 81 | } 82 | 83 | func MakeLineGraph(fileName string, points []Point) { 84 | output := bytes.NewBufferString("\\addplot [black] coordinates {\n") 85 | for _, p := range points { 86 | output.WriteString(p.ToString() + "\n") 87 | } 88 | output.WriteString("};") 89 | ioutil.WriteFile(fileName, output.Bytes(), 777) 90 | } 91 | 92 | func SelectPoints(pts []Point, label string) []Point { 93 | newpts := []Point{} 94 | for _, p := range pts { 95 | if p.Label == label { 96 | newpts = append(newpts, p) 97 | } 98 | } 99 | return newpts 100 | } 101 | -------------------------------------------------------------------------------- /latex/main.go: -------------------------------------------------------------------------------- 1 | package latex 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "io/ioutil" 7 | ) 8 | 9 | type Point struct { 10 | Label string 11 | X, Y float64 12 | } 13 | 14 | func (pt *Point) ToString() string { 15 | return fmt.Sprintf("(%f,%f)", pt.X, pt.Y) 16 | } 17 | 18 | type Entry struct { 19 | MathMode bool 20 | Value string 21 | } 22 | 23 | func (entry *Entry) ToString() string { 24 | if entry.MathMode { 25 | return "$" + entry.Value + "$" 26 | } else { 27 | return entry.Value 28 | } 29 | } 30 | 31 | type Table struct { 32 | CornerValue string 33 | HorizontalLookup []string 34 | VerticalLookup []string 35 | Entries [][]Entry 36 | } 37 | 38 | func MakeTable(fileName string, entries [][]Entry) { 39 | output := bytes.NewBufferString("") 40 | for _, row := range entries { 41 | str := "" 42 | for colNumber, col := range row { 43 | if colNumber == 0 { 44 | str += col.ToString() + " " 45 | } else { 46 | str += "& " + col.ToString() + " " 47 | } 48 | } 49 | output.WriteString(str + "\\\\ \\hline\n") 50 | } 51 | ioutil.WriteFile(fileName, output.Bytes(), 777) 52 | } 53 | 54 | func MakeTableWithLookup(fileName string, tbl Table) { 55 | output := bytes.NewBufferString(tbl.CornerValue + " ") 56 | for _, topVal := range tbl.HorizontalLookup { 57 | output.WriteString("& " + topVal + " ") 58 | } 59 | output.WriteString("\\\\ \\hline\n") 60 | for rowNumber, row := range tbl.Entries { 61 | str := tbl.VerticalLookup[rowNumber] + " " 62 | for _, col := range row { 63 | str += "& " + col.ToString() + " " 64 | } 65 | output.WriteString(str + "\\\\ \\hline\n") 66 | } 67 | ioutil.WriteFile(fileName, output.Bytes(), 777) 68 | } 69 | 70 | func MakeColoredPointGraph(fileName string, labelToMark map[string]string, points []Point) { 71 | output := bytes.NewBufferString("") 72 | for label, mark := range labelToMark { 73 | output.WriteString("\\addplot+[only marks, mark=" + mark + "] coordinates{\n") 74 | for _, p := range points { 75 | if p.Label == label { 76 | output.WriteString(p.ToString() + "\n") 77 | } 78 | } 79 | output.WriteString("};\n") 80 | } 81 | ioutil.WriteFile(fileName, output.Bytes(), 777) 82 | } 83 | 84 | func MakeLineGraph(fileName string, points []Point) { 85 | output := bytes.NewBufferString("\\addplot [black] coordinates {\n") 86 | for _, p := range points { 87 | output.WriteString(p.ToString() + "\n") 88 | } 89 | output.WriteString("};") 90 | ioutil.WriteFile(fileName, output.Bytes(), 777) 91 | } 92 | 93 | func SelectPoints(pts []Point, label string) []Point { 94 | newpts := []Point{} 95 | for _, p := range pts { 96 | if p.Label == label { 97 | newpts = append(newpts, p) 98 | } 99 | } 100 | return newpts 101 | } 102 | --------------------------------------------------------------------------------