From 998090a3ba8dfa4a03bec9d54ebe38dc6fe248ed Mon Sep 17 00:00:00 2001 From: David Gustavsson Date: Tue, 29 Mar 2022 11:52:34 +0200 Subject: [PATCH 1/3] Adjustment Allow vectors of adjustment symbols, and make adjustment affect mdtable. --- docs/src/table_generator.jl | 2 +- src/latexarray.jl | 10 ++++++++-- src/latextabular.jl | 10 ++++++++-- src/mdtable.jl | 24 ++++++++++++++++++++++-- test/latexarray_test.jl | 25 +++++++++++++++++++++++++ test/latextabular_test.jl | 10 ++++++++++ test/mdtable_test.jl | 12 ++++++++++++ 7 files changed, 86 insertions(+), 7 deletions(-) diff --git a/docs/src/table_generator.jl b/docs/src/table_generator.jl index 3a9baf8c..0385db81 100644 --- a/docs/src/table_generator.jl +++ b/docs/src/table_generator.jl @@ -27,7 +27,7 @@ keyword_arguments = [ KeywordArgument(:double_linebreak, [:array, :align, :arrow], "`Bool`", "`false`", "Add an extra `\\\\` to the end of the line.", [:Any]), KeywordArgument(:bracket, [:align], "`Bool`", "`false`", "Surround variables with square brackets.", [:ParameterizedFunction, :ReactionNetwork]), KeywordArgument(:noise, [:align], "`Bool`", "`false`", "Display the noise function instead of the deterministic one.", [:ReactionNetwork]), - KeywordArgument(:adjustment, [:tabular, :array], "`:c` for centered, `:l` for left, `:r` for right", "`:c`", "Set the adjustment of text within the table cells.", [:Any]), + KeywordArgument(:adjustment, [:tabular, :array, :mdtable], "`:c` for centered, `:l` for left, `:r` for right, or a vector with one such symbol per column.", "`:c`", "Set the adjustment of text within the table cells.", [:Any]), KeywordArgument(:expand, [:arrow, :align], "`Bool`", "`true`", "Expand functions such as `hill(x, v, k, n)` to their mathematical expression.", [:ReactionNetwork]), KeywordArgument(:mathjax, [:arrow], "`Bool`", "`true`", "Add `\\require{mhchem}` to tell MathJax to load the required module.", [:ReactionNetwork]), KeywordArgument(:latex, [:mdtable, :tabular], "`Bool`", "`true`", "Toggle latexification of the table elements.", [:Any]), diff --git a/src/latexarray.jl b/src/latexarray.jl index 624885a6..3df7d462 100644 --- a/src/latexarray.jl +++ b/src/latexarray.jl @@ -14,15 +14,21 @@ latexarray(arr) """ latexarray(args...; kwargs...) = process_latexify(args...;kwargs...,env=:array) -function _latexarray(arr::AbstractArray; adjustment::Symbol=:c, transpose=false, double_linebreak=false, +function _latexarray(arr::AbstractArray; adjustment=:c, transpose=false, double_linebreak=false, starred=false, kwargs...) transpose && (arr = permutedims(arr)) rows, columns = axes(arr, 1), axes(arr, 2) eol = double_linebreak ? " \\\\\\\\\n" : " \\\\\n" + if adjustment isa AbstractArray + adjustmentstring = join(adjustment) + else + adjustmentstring = string(adjustment)^length(columns) + end + str = "\\left[\n" - str *= "\\begin{array}{" * "$(adjustment)"^length(columns) * "}\n" + str *= "\\begin{array}{$adjustmentstring}\n" arr = latexraw.(arr; kwargs...) for i in rows, j in columns diff --git a/src/latextabular.jl b/src/latextabular.jl index 71607170..5e093f79 100644 --- a/src/latextabular.jl +++ b/src/latextabular.jl @@ -1,6 +1,6 @@ latextabular(args...; kwargs...) = process_latexify(args...; kwargs..., env=:tabular) -function _latextabular(arr::AbstractMatrix; latex::Bool=true, booktabs::Bool=false, head=[], side=[], adjustment::Symbol=:c, transpose=false, kwargs...) +function _latextabular(arr::AbstractMatrix; latex::Bool=true, booktabs::Bool=false, head=[], side=[], adjustment=:c, transpose=false, kwargs...) transpose && (arr = permutedims(arr, [2,1])) if !isempty(head) @@ -14,7 +14,13 @@ function _latextabular(arr::AbstractMatrix; latex::Bool=true, booktabs::Bool=fal end (rows, columns) = size(arr) - str = "\\begin{tabular}{" * "$(adjustment)"^columns * "}\n" + + if adjustment isa AbstractArray + adjustmentstring = join(adjustment) + else + adjustmentstring = string(adjustment)^columns + end + str = "\\begin{tabular}{$adjustmentstring}\n" if booktabs str *= "\\toprule\n" diff --git a/src/mdtable.jl b/src/mdtable.jl index e7bdf02f..4c10931a 100644 --- a/src/mdtable.jl +++ b/src/mdtable.jl @@ -55,7 +55,7 @@ julia> mdtable(M; head=latexinline(head)) """ function mdtable end -function mdtable(M::AbstractMatrix; latex::Bool=true, escape_underscores=false, head=[], side=[], transpose=false, kwargs...) +function mdtable(M::AbstractMatrix; latex::Bool=true, escape_underscores=false, head=[], side=[], transpose=false, adjustment=nothing, kwargs...) transpose && (M = permutedims(M, [2,1])) if latex M = _latexinline.(M; kwargs...) @@ -74,9 +74,14 @@ function mdtable(M::AbstractMatrix; latex::Bool=true, escape_underscores=false, M = hcat(side, M) end + if adjustment isa AbstractArray + headerrules = get_header_rule.(adjustment) + else + headerrules = fill(get_header_rule(adjustment), size(M, 2)) + end t = "| " * join(M[1,:], " | ") * " |\n" - size(M, 1) > 1 && (t *= "| --- "^(size(M,2)-1) * "| --- |\n") + size(M, 1) > 1 && (t *= "| " * join(headerrules, " | ") * " |\n") for i in 2:size(M,1) t *= "| " * join(M[i,:], " | ") * " |\n" end @@ -91,3 +96,18 @@ mdtable(v::AbstractArray; kwargs...) = mdtable(reshape(v, (length(v), 1)); kwarg mdtable(v::AbstractArray...; kwargs...) = mdtable(safereduce(hcat, v); kwargs...) mdtable(d::AbstractDict; kwargs...) = mdtable(collect(keys(d)), collect(values(d)); kwargs...) mdtable(arg::Tuple; kwargs...) = mdtable(safereduce(hcat, [collect(i) for i in arg]); kwargs...) + +function get_header_rule(adjustment) + if isnothing(adjustment) + headerrule = "------" + elseif adjustment === :c + headerrule = ":----:" + elseif adjustment === :l + headerrule = ":-----" + elseif adjustment === :r + headerrule = "-----:" + else + error("Unknown `adjustment` argument \"$adjustment\"") + end +end + diff --git a/test/latexarray_test.jl b/test/latexarray_test.jl index 34428e7a..611cade9 100644 --- a/test/latexarray_test.jl +++ b/test/latexarray_test.jl @@ -62,6 +62,28 @@ raw"\left[ \end{array} \right]", "\r\n"=>"\n") +@test latexify(arr; adjustment=:r) == replace( +raw"\begin{equation} +\left[ +\begin{array}{rr} +1 & 2 \\ +3 & 4 \\ +\end{array} +\right] +\end{equation} +", "\r\n"=>"\n") + +@test latexify(arr; adjustment=[:l, :r]) == replace( +raw"\begin{equation} +\left[ +\begin{array}{lr} +1 & 2 \\ +3 & 4 \\ +\end{array} +\right] +\end{equation} +", "\r\n"=>"\n") + using OffsetArrays @test latexify(OffsetArray(arr, -1:0, 3:4)) == latexify(arr) @@ -140,3 +162,6 @@ raw"$x = \left[ 4 \\ \end{array} \right]$", "\r\n"=>"\n") + + + diff --git a/test/latextabular_test.jl b/test/latextabular_test.jl index 60ca17bb..af6819f7 100644 --- a/test/latextabular_test.jl +++ b/test/latextabular_test.jl @@ -37,6 +37,16 @@ symb & symb\\ \end{tabular} ", "\r\n"=>"\n") +@test latexify(M; env=:table, head=1:2, adjustment=[:c, :r], latex=false, transpose=true) == replace( +raw"\begin{tabular}{cr} +1 & 2\\ +x/(y-1) & x/(y-1)\\ +1.0 & 1.0\\ +3//2 & 3//2\\ +x - y & x - y\\ +symb & symb\\ +\end{tabular} +", "\r\n"=>"\n") @test latexify(M; env=:table, head=1:2, adjustment=:l, transpose=true) == replace( raw"\begin{tabular}{ll} diff --git a/test/mdtable_test.jl b/test/mdtable_test.jl index c7bbb5e2..881ffeba 100644 --- a/test/mdtable_test.jl +++ b/test/mdtable_test.jl @@ -71,6 +71,18 @@ side = ["row$i" for i in 1:size(M, 1)] | $\frac{x}{y - 1}$ | $1.0$ | $\frac{3}{2}$ | $x - y$ | $symb$ | " +@test mdtable(M; adjustment=:c) == Markdown.md" +| $\frac{x}{y - 1}$ | $1.0$ | $\frac{3}{2}$ | $x - y$ | $symb$ | +| :----------------:| :----:| :------------:| :------:| :-----:| +| $\frac{x}{y - 1}$ | $1.0$ | $\frac{3}{2}$ | $x - y$ | $symb$ | +" + +@test mdtable(M; adjustment=[:l :c :r :l nothing]) == Markdown.md" +| $\frac{x}{y - 1}$ | $1.0$ | $\frac{3}{2}$ | $x - y$ | $symb$ | +| :-----------------| :----:| -------------:| :-------| ------:| +| $\frac{x}{y - 1}$ | $1.0$ | $\frac{3}{2}$ | $x - y$ | $symb$ | +" + @test mdtable(M, head=head) == Markdown.md" | col1 | col2 | col3 | col4 | col5 | | -----------------:| -----:| -------------:| -------:| ------:| From 13739de171b0e603e58c13c6a9f1b2b497fed0f3 Mon Sep 17 00:00:00 2001 From: David Gustavsson Date: Tue, 29 Mar 2022 19:02:00 +0200 Subject: [PATCH 2/3] Cleaner get_header_rule --- src/mdtable.jl | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/mdtable.jl b/src/mdtable.jl index 4c10931a..268dc777 100644 --- a/src/mdtable.jl +++ b/src/mdtable.jl @@ -97,17 +97,11 @@ mdtable(v::AbstractArray...; kwargs...) = mdtable(safereduce(hcat, v); kwargs... mdtable(d::AbstractDict; kwargs...) = mdtable(collect(keys(d)), collect(values(d)); kwargs...) mdtable(arg::Tuple; kwargs...) = mdtable(safereduce(hcat, [collect(i) for i in arg]); kwargs...) -function get_header_rule(adjustment) - if isnothing(adjustment) - headerrule = "------" - elseif adjustment === :c - headerrule = ":----:" - elseif adjustment === :l - headerrule = ":-----" - elseif adjustment === :r - headerrule = "-----:" - else - error("Unknown `adjustment` argument \"$adjustment\"") - end +get_header_rule(::Nothing) = "-------" +function get_header_rule(adjustment::Symbol) + adjustment === :c && return ":----:" + adjustment === :l && return ":-----" + adjustment === :r && return "-----:" + error("Unknown `adjustment` argument \"$adjustment\"") end From e0d3aa23058cf66e0ebf0f414b3b2f796e9e4918 Mon Sep 17 00:00:00 2001 From: David Gustavsson Date: Tue, 29 Mar 2022 19:03:15 +0200 Subject: [PATCH 3/3] Bump version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index e5b59d48..bd170843 100644 --- a/Project.toml +++ b/Project.toml @@ -2,7 +2,7 @@ name = "Latexify" uuid = "23fbe1c1-3f47-55db-b15f-69d7ec21a316" authors = ["Niklas Korsbo "] repo = "https://github.com/korsbo/Latexify.jl.git" -version = "0.15.13" +version = "0.15.14" [deps] Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0"