tpapp / LaTeXTabulars.jl

Write tabular data from Julia in LaTeX format.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Longtables

dpo opened this issue · comments

Thanks for this very useful package!

I added the following to my local clone, which helps me output longtables:

diff --git a/src/LaTeXTabulars.jl b/src/LaTeXTabulars.jl
index 8df0931..a25b426 100644
--- a/src/LaTeXTabulars.jl
+++ b/src/LaTeXTabulars.jl
@@ -5,7 +5,7 @@ using DocStringExtensions
 using Parameters
 using Logging
 
-export Rule, CMidRule, MultiColumn, Tabular, latex_tabular
+export Rule, CMidRule, MultiColumn, Tabular, LongTable, latex_tabular
 
 
 # cells
@@ -196,4 +196,32 @@ function latex_tabular(filename::AbstractString, t::TabularLike, lines)
     end
 end
 
+struct LongTable <: TabularLike
+    "A column specification, eg `\"llrr\"`."
+    cols::AbstractString
+    "The table header, to be repeated at the top of each page, eg `[\"alpha\", \"beta\", \"gamma\"]`."
+    header
+end
+
+function latex_env_begin(io::IO, t::LongTable)
+    println(io, "\\begin{longtable}[c]{$(t.cols)}")
+    latex_line(io, Rule(:h))
+    latex_line(io, t.header)
+    latex_line(io, Rule(:h))
+    println(io, "\\endfirsthead")
+    println(io, "\\multicolumn{$(length(t.cols))}{l}")
+    println(io, "{{\\bfseries \\tablename\\ \\thetable{} --- continued from previous page}} \\\\")
+    latex_line(io, Rule(:h))
+    latex_line(io, t.header)
+    latex_line(io, Rule(:h))
+    println(io, "\\endhead")
+    latex_line(io, Rule(:h))
+    println(io, "\\multicolumn{$(length(t.cols))}{r}{{\\bfseries Continued on next page}} \\\\")
+    latex_line(io, Rule(:h))
+    println(io, "\\endfoot")
+    latex_line(io, Rule(:h))
+    println(io, "\\endlastfoot")
+end
+
+latex_env_end(io::IO, t::LongTable) = println(io, "\\end{longtable}")
+
 end # module

Here's an example use:

latex_tabular("/tmp/table.tx",
              LongTable("rrr", ["alpha", "beta", "gamma"]),  # <- give header here
              [rand(300,3), Rule(:h)])

The main LaTeX document should \usepackage{longtable}.

In longtables, the table header is typically repeated, so I made it part of the LongTable struct. That's a bit different from the standard tabular you implemented. Do this seem acceptable? If so I'm happy to send a pull request.

Yes, a PR would be very nice.

See #5.

Thank you for the PR 👍, closing.