formatting facilities

Chevie.FormatModule

Chevie contains some extended formatting facilities for printing, displaying, formatting objects in various ways. For that Chevie uses extensively IO properties. We have sevral convenience functions which make using IO properties easier.

rio(;d...) makes an IO stream which always has the property :limit=>true, to mimic the REPL default printing, and has also the extra properties given by the d... keywords. Using this, for instance

IOContext(stdout,:limit=>true,:compact=>true) becomes rio(compact=true).

We have versions of display functions which use implicitely rio:

xprint(x...;p...) is the same as print(rio(;p...),x...). Similarly for println, display we have xprintln, xdisplay.

xrepr(x;p...) is the same as repr(x;context=IOContext(stdout,p...)). xrepr(io,x) is the same as repr(x;context=io).

julia> @Pol q;p=(q^5+1)^2
Pol{Int64}: q¹⁰+2q⁵+1

julia> print(p)
Pol([1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1])
julia> xprint(p)
q¹⁰+2q⁵+1
julia> xprint(p;varname=:x)
x¹⁰+2x⁵+1
julia> repr(2E(5,2)+2E(5,3))
"Cyc{Int64}(-1-root(5))"

julia> xrepr(2E(5,2)+2E(5,3);quadratic=false)
"Cyc{Int64}(2E(5,2)+2E(5,3))"

Most objects in Chevie use TeX for printing when given the IO property :TeX=true. This is used as the default display in IJulia and Pluto by giving the property :TeX when defining Base.show(io::IO, ::MIME"text/html", ...) for these objects. Continuing the above example:

julia> xprint(p;TeX=true)
q^{10}+2q^5+1

A model we often adopt for displaying nicely complex objects is to first write a nice display using TeX output. This can be used directly in IJulia and Pluto. For other environments, we can compute from the TeX representation a suitable one using the following function:

fromTeX(io::IO,s) takes a TeX source and tries to give the best possible rendering on a given IO. This uses unicode characters at the REPL (if get(io,:limit,false)==true). In the worse case (stdout) all TeX special characters are stripped.

julia> s="E_6[\zeta_3]:\phi_{1,6}"
"E_6[\zeta_3]:\phi_{1,6}"

julia> fromTeX(rio(),s)
"E₆[ζ₃]:φ₁‚₆"

julia> fromTeX(stdout,s)
"E6[E3]:phi1,6"

printTeX(io,s) is the same as print(io,fromTeX(io,s)).

Other functions to ease formatting are described below: see showtable, joindigits, ordinal, cut.

source
Chevie.Format.rioFunction

rio(io::IO=stdout;p...) enriches io with the attributes in p. It always enriches with limit=true to mimic display at the REPL.

Thus print(rio(),x...) is like printing x... at the REPL.

source
Chevie.Format.xreprFunction

xrepr(x;p...) is repr using as context stdout enriched by p...

source

xrepr(io,x;p...) is repr using as context io enriched by p...

source
Chevie.Format.showtableFunction

showtable(io::IO=stdout, table::AbstractMatrix; keywords)

General routine to format a table at the REPL, or in IJulia or Pluto. The elements of table and any of the labels in the keywords can be of any type and are formatted in the context of io, excepted that a string s is formatted by fromTeX(io,s). The following options can be passed as properties of the io or as keywords.

  • row_labels: labels for rows. A Vector{Any} (can be strings), default axes(table,1)
  • rows_label: label for first column of row labels (default none)
  • col_labels: labels for other columns (default none)
  • align: a character in "lcr": alignment of columns (default 'r'); then all columns will be aligned as given except the rows_labels which will always be aligned left. Or if align is a string it should be of length 1+size(table,2) where the first character is the alignment of the row_labels.
  • row_seps: line numbers after which to put a separator. A number of i means before i-th line of the table. So 0 is at the top of the table, -1 is before the col_labels. The default is [-1,0,size(table,1)].
  • col_seps: column numbers after which to put a separator. A number of i means before i-th column of the table. So 0 is at the left of the table, -1 is before the row_labels. The default is [-1,0,size(table,2)]. Alternately the col_seps can be given using an align string in LaTeX style |r|llll|. They should be given by only one of the two ways.
  • rows: show only these rows. Default all rows: axes(table,1)
  • cols: show only these columns. Default all columns: axes(table,1)
  • TeX: default false. If true, give LaTeX output (useful to give nicer output in Jupyter or Pluto)
  • column_repartition: a Vector{<:Integer}. Display in vertical pieces of sizes indicated (useful for TeX: otherwise the column_repartition is automatically computed taking in account displaysize(io,2)).
  • dotzero: if true replace a '0' by '.' in the table (default false).
julia> m=reshape(1:10:120,3,4)
3×4 reshape(::StepRange{Int64, Int64}, 3, 4) with eltype Int64:
  1  31  61   91
 11  41  71  101
 21  51  81  111

julia> showtable(m)
┌─┬────────────┐
│1│ 1 31 61  91│
│2│11 41 71 101│
│3│21 51 81 111│
└─┴────────────┘

julia> labels=["x","y","z","t"];

julia> showtable(m;cols=2:4,col_labels=labels,row_seps=[0,2,3])
    y  z   t 
┌─┬─────────┐
│1│31 61  91│
│2│41 71 101│
├─┼─────────┤
│3│51 81 111│
└─┴─────────┘

julia> showtable(m;col_labels=labels,rows_label="N",align="|r|ll|ll|")
┌─┬─────┬──────┐
│N│ x  y│ z   t│
├─┼─────┼──────┤
│1│1  31│61 91 │
│2│11 41│71 101│
│3│21 51│81 111│
└─┴─────┴──────┘
source
Chevie.Format.joindigitsFunction

joindigits(l::AbstractVector{Int},delim="()";sep=",")

print a list l of (usually small) numbers as compactly as possible: no separators if all numbers are smaller than 10.

julia> joindigits([1,9,3,5])
"1935"

julia> joindigits([1,10,3,5])
"(1,10,3,5)"

julia> joindigits([1,10,3,5],"[]";sep="-")
"[1-10-3-5]"
source
Chevie.Format.cutFunction

cut(io::IO=stdout,string;width=displaysize(io)[2]-2,after=",",before="")

This function prints to io the string argument cut across several lines for improved display. It can take the following keyword arguments:

  • width: the cutting width
  • after: cut after these chars
  • before: cut before these chars
julia> cut(string(collect(1:50)))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
source
Chevie.Format.ordinalFunction

ordinal(n::Integer)

string for an ordinal number respecting english syntax.

julia> ordinal(201)
"201st"

julia> ordinal(202)
"202nd"

julia> ordinal(203)
"203rd"

julia> ordinal(204)
"204th"

julia-repl

source