Skip to content

Commit

Permalink
Merge pull request #218 from gustaphe/mdtablealign
Browse files Browse the repository at this point in the history
Adjustment
  • Loading branch information
gustaphe committed Mar 29, 2022
2 parents d1845a5 + e0d3aa2 commit 1067732
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "Latexify"
uuid = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
authors = ["Niklas Korsbo <niklas.korsbo@gmail.com>"]
repo = "https://github.com/korsbo/Latexify.jl.git"
version = "0.15.13"
version = "0.15.14"

[deps]
Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0"
Expand Down
2 changes: 1 addition & 1 deletion docs/src/table_generator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down
10 changes: 8 additions & 2 deletions src/latexarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/latextabular.jl
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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"
Expand Down
18 changes: 16 additions & 2 deletions src/mdtable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand All @@ -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
Expand All @@ -91,3 +96,12 @@ 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...)

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

25 changes: 25 additions & 0 deletions test/latexarray_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -140,3 +162,6 @@ raw"$x = \left[
4 \\
\end{array}
\right]$", "\r\n"=>"\n")



10 changes: 10 additions & 0 deletions test/latextabular_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
12 changes: 12 additions & 0 deletions test/mdtable_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
| -----------------:| -----:| -------------:| -------:| ------:|
Expand Down

2 comments on commit 1067732

@gustaphe
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator.register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/57572

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.15.14 -m "<description of version>" 1067732f2ca697c28d22025e447680bfd625e0f4
git push origin v0.15.14

Please sign in to comment.