Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various output routine updates #3260

Merged
merged 3 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions M2/Macaulay2/m2/expressions.m2
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ expression ZZ := i -> (
else if i < 0 then new Minus from { -i }
else hold i
)
OneExpression ^ Expression :=
Holder ^ OneExpression :=
Expression ^ OneExpression := (x,y) -> x
Holder ^ ZeroExpression :=
Expand Down Expand Up @@ -855,7 +856,7 @@ net Sum := v -> (
then bigParenthesize net v#i
else net v#i))))

isNumber = method(TypicalValue => Boolean)
isNumber = method(Dispatch => Thing, TypicalValue => Boolean)
isNumber Thing := i -> false
isNumber RR :=
isNumber QQ := -- QQ never appears in an expression...
Expand Down Expand Up @@ -1104,7 +1105,7 @@ texMathSuperscript := v -> (
x := texMath v#0;
y := texMath v#1;
if precedence v#0 < p or class v#0 === Superscript or class v#0 === Power then x = "\\left(" | x | "\\right)"; -- precedence of double superscript
concatenate(x,"^{",y,"}") -- no braces around x
concatenate(x,"_{",y,"}",if class v#0===Symbol and last toString v#0=="'" then "{}") -- no braces around x
)
texMath Power := v -> if v#1 === 1 or v#1 === ONE then texMath v#0 else texMathSuperscript v
texMath Superscript := v -> if v#1 === moduleZERO then "0" else texMathSuperscript v
Expand Down Expand Up @@ -1196,7 +1197,8 @@ texMath MatrixExpression := x -> (
)

texMath VectorExpression := v -> (
concatenate(
if all(v,i->class i===ZeroExpression) then "0"
else concatenate(
"\\left(",
"\\!\\begin{array}{c}",
newline,
Expand Down
3 changes: 2 additions & 1 deletion M2/Macaulay2/m2/iterators.m2
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Iterator.synonym = "iterator"

iterator Iterator := identity
next Iterator := iter -> iter()
net Iterator := iter -> (

net Iterator := iter -> if hasAttribute(iter,ReverseDictionary) then net getAttribute(iter,ReverseDictionary) else (
x := if not (first frames iter)#?0 then () else first first frames iter;
net FunctionApplication(iterator,
(if instance(x, String) then format else identity) x))
Expand Down
108 changes: 92 additions & 16 deletions M2/Macaulay2/m2/latex.m2
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,32 @@ shorten := s -> (
-- TODO: remove as duplicate
noopts := x -> select(x,e -> class e =!= Option and class e =!= OptionTable)

texLiteralTable := new MutableHashTable
scan(0 .. 255, c -> texLiteralTable#(ascii{c}) = concatenate(///{\char ///, toString c, "}"))
scan(ascii(32 .. 126), c -> texLiteralTable#c = c)
scan("\\{}$&#^_%~|<>\"", c -> texLiteralTable#c = concatenate("{\\char ", toString (ascii c)#0, "}"))
texLiteralTable#"\n" = "\n"
texLiteralTable#"\r" = "\r"
texLiteralTable#"\t" = "\t"
texLiteralTable#"`" = "{`}" -- break ligatures ?` and !` in font \tt. See page 381 of TeX Book.
texLiteral = s -> concatenate for c in s list texLiteralTable#c
texLiteralEncode := c -> concatenate apply(ascii c,i->("\\char",toString i))
texLiteralPairs := splice {
apply(0..8|11..12|14..31|127..255|toSequence ascii "\\{}$&#^_%~|<>\"", c -> ascii c => texLiteralEncode ascii c),
"`" => "{`}" -- break ligatures ?` and !` in font \tt. See page 381 of TeX Book.
}
texLiteralTable := hashTable texLiteralPairs
texLiteral1 := t -> s -> (
flag:=false;
concatenate apply(characters s,
c -> if t#?c then (
s:=t#c;
flag=first s==="\\" and last s =!="}";
s
)
else if flag then (flag=false; " "|c) else c
) | if flag then " " else ""
)
texLiteral := texLiteral1 texLiteralTable

HALFLINE := "\\vskip 4.75pt\n"
ENDLINE := "\\leavevmode\\hss\\endgraf\n"
VERBATIM := "\\begingroup\\tt "
ENDVERBATIM := "\\endgroup{}"

texExtraLiteralTable := copy texLiteralTable
texExtraLiteralTable#" " = "\\ "
texExtraLiteral := s -> demark(ENDLINE,
apply(lines s, l -> for c in l list texExtraLiteralTable#c))
texExtraLiteralTable := hashTable append(texLiteralPairs," " => "\\ ")
texExtraLiteral := s -> demark(ENDLINE, apply(lines s, l -> apply(l, c -> if texExtraLiteralTable#?c then texExtraLiteralTable#c else c)))

--------------------------------------------
-- this loop depends on the feature of hash tables that when the keys
Expand All @@ -72,20 +79,89 @@ sectionType = sectionNumber -> (
"\\subsubsubsubsubparagraph");

-----------------------------------------------------------------------------
keywordTexMath = applyKeys(hashTable { -- both unary and binary keywords
symbol |- => "\\vdash ",
symbol .. => "\\,{.}{.}\\,",
symbol ..< => "\\,{.}{.}{<}\\,",
symbol <= => "\\le ",
symbol >= => "\\ge ",
symbol => => "\\Rightarrow ",
symbol ==> => "\\Longrightarrow ",
symbol <== => "\\Longleftarrow ",
symbol <==> => "\\Longleftrightarrow ",
symbol _> => "{}_>",
symbol _< => "{}_<",
symbol ^> => "{}^>",
symbol ^< => "{}^<",
symbol ** => "\\otimes ",
symbol ++ => "\\oplus ",
symbol != => "\\ne ",
symbol -> => "\\rightarrow ",
symbol <- => "\\leftarrow ",
symbol ===> => "{\\large\\Longrightarrow}",
symbol <=== => "{\\large\\Longleftarrow}",
symbol << => "\\ll ",
symbol >> => "\\gg ",
symbol ^! => "{}^!",
symbol _! => "{}_!",
symbol ~ => "\\sim ",
symbol ^~ => "{}^\\sim",
symbol _~ => "{}_\\sim",
--symbol ^# => "{}^\\sharp",
--symbol _# => "{}_\\sharp",
symbol _ => "\\_",
symbol { => "\\{",
symbol } => "\\}",
symbol \ => "\\backslash ",
symbol \\ => "\\backslash\\backslash ",
symbol # => "\\#",
symbol #? => "\\#?",
symbol % => "\\%",
symbol & => "\\&",
symbol ^ => "\\wedge ",
symbol ^^ => "\\wedge\\wedge ",
symbol <| => "\\langle ",
symbol |> => "\\rangle ",
symbol | => "\\mid",
symbol || => "\\mid\\mid",
symbol ^** => "{}^{\\otimes}", -- temporary solution to KaTeX issue https://github.com/KaTeX/KaTeX/issues/3576
symbol _* => "{}_*", -- temporary solution to KaTeX issue https://github.com/KaTeX/KaTeX/issues/3576
symbol ^* => "{}^*" -- temporary solution to KaTeX issue https://github.com/KaTeX/KaTeX/issues/3576
},symbolBody)

bbLetters := set characters "kABCDEFGHIJKLMNOPQRSTUVWXYZ"
-- greek letters below are only in math mode
texMathLiteralTable := merge(texLiteralTable,
hashTable {
"Α" => "\\Alpha", "Β" => "\\Beta", "Ε" => "\\Epsilon", "Ζ" => "\\Zeta", "Η" => "\\Eta", "Ι" => "\\Iota", "Κ" => "\\Kappa", "Μ" => "\\Mu", "Ν" => "\\Nu", "Ο" => "\\Omicron", "Ρ" => "\\Rho", "Τ" => "\\Tau", "Χ" => "\\Chi",
"Γ" => "\\Gamma", "Δ" => "\\Delta", "Θ" => "\\Theta", "Λ" => "\\Lambda", "Ξ" => "\\Xi", "Π" => "\\Pi", "Σ" => "\\Sigma", "Υ" => "\\Upsilon", "Φ" => "\\Phi", "Ψ" => "\\Psi", "Ω" => "\\Omega",
"ϱ" => "\\varrho", "ϵ" => "\\epsilon", "π" => "\\pi", "ρ" => "\\rho", "ς" => "\\varsigma", "σ" => "\\sigma", "τ" => "\\tau", "υ" => "\\upsilon", "φ" => "\\varphi", "χ" => "\\chi", "ψ" => "\\psi", "ω" => "\\omega", "ϑ" => "\\vartheta",
"α" => "\\alpha", "β" => "\\beta", "γ" => "\\gamma", "ϕ" => "\\phi", "δ" => "\\delta", "ε" => "\\varepsilon", "ϖ" => "\\varpi", "ζ" => "\\zeta", "η" => "\\eta", "θ" => "\\theta", "ι" => "\\iota", "κ" => "\\kappa", "λ" => "\\lambda", "μ" => "\\mu", "ν" => "\\nu", "ξ" => "\\xi", "ο" => "\\omicron",
"𝔞" => "\\mathfrak{a}","𝔟" => "\\mathfrak{b}","𝔠" => "\\mathfrak{c}","𝔡" => "\\mathfrak{d}","𝔢" => "\\mathfrak{e}","𝔣" => "\\mathfrak{f}","𝔤" => "\\mathfrak{g}","𝔥" => "\\mathfrak{h}","𝔦" => "\\mathfrak{i}","𝔧" => "\\mathfrak{j}","𝔨" => "\\mathfrak{k}",
"𝔩" => "\\mathfrak{l}","𝔪" => "\\mathfrak{m}","𝔫" => "\\mathfrak{n}","𝔬" => "\\mathfrak{o}","𝔭" => "\\mathfrak{p}","𝔮" => "\\mathfrak{q}","𝔯" => "\\mathfrak{r}","𝔰" => "\\mathfrak{s}","𝔱" => "\\mathfrak{t}","𝔲" => "\\mathfrak{u}","𝔳" => "\\mathfrak{v}",
"𝔴" => "\\mathfrak{w}","𝔵" => "\\mathfrak{x}","𝔶" => "\\mathfrak{y}","𝔷" => "\\mathfrak{z}","𝔄" => "\\mathfrak{A}","𝔅" => "\\mathfrak{B}","𝔆" => "\\mathfrak{C}","𝔇" => "\\mathfrak{D}","𝔈" => "\\mathfrak{E}","𝔉" => "\\mathfrak{F}","𝔊" => "\\mathfrak{G}",
"𝔋" => "\\mathfrak{H}","𝔌" => "\\mathfrak{I}","𝔍" => "\\mathfrak{J}","𝔎" => "\\mathfrak{K}","𝔏" => "\\mathfrak{L}","𝔐" => "\\mathfrak{M}","𝔑" => "\\mathfrak{N}","𝔒" => "\\mathfrak{O}","𝔓" => "\\mathfrak{P}","𝔔" => "\\mathfrak{Q}","𝔕" => "\\mathfrak{R}",
"𝔖" => "\\mathfrak{S}","𝔗" => "\\mathfrak{T}","𝔘" => "\\mathfrak{U}","𝔙" => "\\mathfrak{V}","𝔚" => "\\mathfrak{W}","𝔛" => "\\mathfrak{X}","𝔜" => "\\mathfrak{Y}","𝔝" => "\\mathfrak{Z}"
},last)
texMathLiteral = texLiteral1 texMathLiteralTable
-- TODO: expand and document this behavior
suffixes := {"bar","tilde","hat","vec","dot","ddot","check","acute","grave","breve"};
suffixesRegExp := "\\w("|demark("|",suffixes)|")$";
texVariable := x -> (
if x === "" then return "";
xx := separate("\\$", x); if #xx > 1 then return demark("{\\char36}", texVariable \ xx); -- avoid the use of "$" in tex output
if #x === 2 and x#0 === x#1 and bbLetters#?(x#0) then return "{\\mathbb " | x#0 | "}";
if last x === "'" then return texVariable substring(x, 0, #x-1) | "'";
if (r := regex(suffixesRegExp, x)) =!= null then return (
r = r#1; "\\" | substring(r, x) | "{" | texVariable substring(x, 0, r#0) | "}");
if #x === 1 or regex("[^[:alnum:]]", x) =!= null then x else "\\textit{" | x | "}")
texMath Symbol := x -> if keywordTexMath#?x then keywordTexMath#x else texVariable toString x
if #x === 1 or regex("[^[:alnum:]]", x) =!= null then x else "\\mathit{" | x | "}")
texMathSymbol :=
texMath Symbol := texVariable @@ texMathLiteral @@ toString
texMath Keyword := texMath @@ symbolBody
texMath SymbolBody := s -> if keywordTexMath#?s then keywordTexMath#s else texMathSymbol s

-- add augmented operators
removeLast := s -> substring(s,0,#s-1)
keywordTexMath = merge(keywordTexMath, hashTable apply(toList augmentedAssignmentOperators, s -> symbolBody s => (texMath (getGlobalSymbol ( removeLast toString s ))) | "="), last)

-----------------------------------------------------------------------------

Expand Down
1 change: 1 addition & 0 deletions M2/Macaulay2/m2/reals.m2
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ texMath RR := x -> (
printingLeadLimit,
printingTrailLimit,
"}\\cdot 10^{", x ) | "}")
texMath RRi := x -> concatenate("\\big[",texMath left x,",",texMath right x,"\\big]",if isEmpty x then "\\text{ (an empty interval)}")
withFullPrecision = f -> (
prec := printingPrecision;
acc := printingAccuracy;
Expand Down
1 change: 0 additions & 1 deletion M2/Macaulay2/m2/webapp.m2
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ Thing#{WebApp,AfterNoPrint} = x -> (

removeWebAppTags = s -> if s === null then null else replace(webAppTagsRegex,"😀",s);
if topLevelMode === WebApp then (
compactMatrixForm = false;
extractStr := x -> concatenate apply(x,y -> if instance(y,Hypertext) then extractStr y else if instance(y,String) then y);
-- the help hack: if started in WebApp mode, help is compiled in it as well
processExamplesLoop ExampleItem := (x->new LITERAL from extractStr x) @@ (lookup(processExamplesLoop,ExampleItem));
Expand Down