Skip to content

Commit

Permalink
fix for better line breaking with breqn
Browse files Browse the repository at this point in the history
The packages breqn and mleftright do not work nicely together.
Inside \mleft( ... \mright) breqn seems to be unable to break lines.
This commit removes the need for mleftright and adjusts the
by adding \mathopen{} before and \mathclose{} after the parentheses.
The formatters export a function formatFunction.
  • Loading branch information
hemmecke committed Jun 22, 2024
1 parent e1644e6 commit 24a23cb
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 105 deletions.
33 changes: 27 additions & 6 deletions src/algebra/fmt.spad
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,17 @@ decimal expansion. In the default implementation of
++ \spad{formatFunctionSymbol(s)} formats an otherwise unknown
++ function symbol.

)if LiterateDoc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Formatting a function might need special treatment.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
)endif

formatFunction: (BOX, List BOX) -> BOX
++ formatFunction(b, args) is supposed to create a box for
++ "b(args)", i.e. format the list of arguments given by args an
++ put it into a format that represents a function application.

)if LiterateDoc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
The following functions are only exported to aid with changing
Expand Down Expand Up @@ -590,6 +601,10 @@ argument in a context with precedence \spad{p}.
++ then prefixes this box by \spad{box s}.
++ Outer parentheses are added if \spad{p < prec}.

function: (S, Z, H) -> H -- has default
++ \spad{function(s, p, hh)} is meant to typeset function
++ applications. It defaults to prefix(s,p,bracket("(",")",hh)).

binary: (H, H) -> H --has default
++ \spad{binary(h1, h2)} returns a handler \spad{h} such that
++ \spad{h(prec, args)} formats the first argument according to
Expand Down Expand Up @@ -1377,10 +1392,12 @@ of a \spad{ROW} operator without the wrapping \spad{MATRIX} operator.
formatString(s: S): BOX == box s
formatSymbol(s: S): BOX == box s
formatFunctionSymbol(s: S): BOX == box s
formatFunction(b: BOX, args: LE): BOX ==
m: Z := minPrecedence()
a: BOX := nary(", ", maxPrecedence(), formatExpression m)(m, args)
hconcat [b, parenthesize("(", ")", a)]
formatFunction(b: BOX, lb: List BOX): BOX ==
import from OutputFormTools
empty? lb => hconcat [b, parenthesize("(", ")", empty()$BOX)]
bx: BOX := first lb
for b in rest lb repeat bx := hconcat [bx, box ", ", b]
hconcat [b, parenthesize("(", ")", bx)]

parenthesizeIf(needParen?: Boolean, bx: BOX): BOX ==
needParen? => parenthesize("(", ")", bx)
Expand All @@ -1407,7 +1424,8 @@ of a \spad{ROW} operator without the wrapping \spad{MATRIX} operator.
opex: E := operator expr
args: LE := arguments expr
not symbol? opex =>
formatFunction(formatExpression(opex, minPrecedence()), args)
bargs: List BOX := [formatExpression a for a in args]
formatFunction(formatExpression(opex, minPrecedence()), bargs)

nargs: Z := #args
op: S := string symbol opex
Expand All @@ -1421,7 +1439,8 @@ of a \spad{ROW} operator without the wrapping \spad{MATRIX} operator.

-- We format as a function with function name op and argument
-- give by args.
formatFunction(formatFunctionSymbol op, args)
bargs: List BOX := [formatExpression a for a in args]
formatFunction(formatFunctionSymbol op, bargs)

formatExpression(expr: E): BOX == formatExpression(expr, minPrecedence())

Expand All @@ -1440,6 +1459,8 @@ of a \spad{ROW} operator without the wrapping \spad{MATRIX} operator.
prefix(op: S, p: Z, hh: H): H == (prec: Z, args: LE): BOX +->
parenthesizeIf(p < prec, hconcat [box op, hh(prec, args)])

function(op: S, p: Z, hh: H): H == prefix(op, p, bracket("(", ")", hh))

infix(op: S, p: Z, h1: H, h2: H): H == (prec: Z, args: LE): BOX +->
b1: BOX := h1(prec, [args.1])
b2: BOX := h2(prec, [args.2])
Expand Down
74 changes: 34 additions & 40 deletions src/algebra/fmt1d.spad
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,6 @@ i.e., an underscore character.
parenthesize(left: S, right: S, b: BOX): BOX ==
hconcat [box left, b, box right]

-- local funnction
formatFunction(s: S, lb: List BOX): BOX ==
empty? lb => box concat(s,"()")
bx: BOX := first lb
for b in rest lb repeat bx := hconcat [bx, box ", ", b]
hconcat [box s, parenthesize("(", ")", bx)]

-- Replace x by y in expression z.
substitute(x: E, y: E, z: E): E ==
import from OutputFormTools
Expand Down Expand Up @@ -214,7 +207,7 @@ i.e., an underscore character.
empty? bu =>
lb: List BOX := [formatExpression(args.3, MIN)]
if not empty? bl then lb := concat(lb, bl)
return formatFunction("int", lb) -- int operator form OutputForm
return formatFunction(box "int", lb) -- int operator form OutputForm

--assert(not empty? bu)
b2: BOX := bu
Expand All @@ -234,20 +227,20 @@ i.e., an underscore character.
-- limits are given by x=bl..bu
ba := formatExpression(a, MIN)
b2 := hconcat [bx, box "=", bl, box "..", bu]
formatFunction("integral", [ba, b2])
formatFunction(box "integral", [ba, b2])

-- local
operatorWithLimits(s: S, p: Z): H == (prec: Z, args: LE): BOX +->
-- We can assume #args>=2, but the first argument can be empty
bl: BOX := formatExpression(args.1, MIN) -- lower limit
ba: BOX := formatExpression(args.2, MIN) -- upper limit or arg
#args = 2 =>
empty? bl => formatFunction(s, [ba])
formatFunction(s, [ba, bl])
empty? bl => formatFunction(box s, [ba])
formatFunction(box s, [ba, bl])
bu: BOX := ba -- upper limit
ba := formatExpression(args.3, MIN) -- arg (no parens needed)
b2: BOX := hconcat [bl, box " .. ", bu]
formatFunction(s, [ba, b2])
formatFunction(box s, [ba, b2])

sum(p: Z): H == operatorWithLimits("sum", p)
product(p: Z): H == operatorWithLimits("product", p)
Expand All @@ -264,16 +257,16 @@ i.e., an underscore character.
parenthesize("theMap(", ")", box s(p1+1..p2-1))

overbar(p: Z, hh: H): H == (prec: Z, args: LE): BOX +->
formatFunction("overbar", [hh(p, args)])
formatFunction(box "overbar", [hh(p, args)])

box(hh: H): H == (prec: Z, args: LE): BOX +->
formatFunction("box", [hh(prec, [args.1])])
formatFunction(box "box", [hh(prec, [args.1])])

-- \sqrt[n]{x}
nthRoot(p: Z, h1: H, h2: H): H == (prec: Z, args: LE): BOX +->
bx: BOX := h1(p, [args.1])
one?(# args) => formatFunction("sqrt", [bx])
formatFunction("nthRoot", [bx, h2(p, [args.2])])
one?(# args) => formatFunction(box "sqrt", [bx])
formatFunction(box "nthRoot", [bx, h2(p, [args.2])])

emptyArgument?(a: E): Boolean ==
import from OutputFormTools
Expand All @@ -290,12 +283,12 @@ i.e., an underscore character.
bx := hconcat [bx, box ", empty()$OutputForm"]
else
bx := hconcat [bx, box ", ", formatExpression(a, MIN)]
formatFunction("scripts", [b1, parenthesize("[", "]", bx)])
formatFunction(box "scripts", [b1, parenthesize("[", "]", bx)])

subscript(p: Z): H == (prec: Z, args: LE): BOX +->
b1: BOX := formatExpression(args.1, MIN)
b2: BOX := formatExpression(args.2, MIN)
formatFunction("subscript", [b1, parenthesize("[", "]", b2)])
formatFunction(box "subscript", [b1, parenthesize("[", "]", b2)])


)if LiterateDoc
Expand All @@ -320,15 +313,15 @@ representation in \spadtype{OutputForm} back into the above form.
bx := hconcat [bx, box ", empty()$OutputForm"] --$
else
bx := hconcat [bx, box ", ", formatExpression(a, MIN)]
formatFunction("supersub", [b1, parenthesize("[", "]", bx)])
formatFunction(box "supersub", [b1, parenthesize("[", "]", bx)])

-- we must treat the special format of a prime expression
prime(p: Z): H == (prec: Z, args: LE): BOX +->
b1: BOX := formatExpression(args.1, p+1)
n: Z := numberOfPrimes(args.2)
n < 0 => error "error in PRIME expression"
b2: BOX := box(convert(n)@String)
formatFunction("prime", [b1, b2])
formatFunction(box "prime", [b1, b2])

power(p: Z, h1: H, h2: H): H == (prec: Z, args: LE): BOX +->
b1: BOX := h1(p+1, [args.1])
Expand All @@ -345,12 +338,12 @@ representation in \spadtype{OutputForm} back into the above form.
binomial(prec: Z, args: LE): BOX ==
b1: BOX := formatExpression(args.1, MIN)
b2: BOX := formatExpression(args.2, MIN)
formatFunction("binomial", [b1, b2])
formatFunction(box "binomial", [b1, b2])

zag(prec: Z, args: LE): BOX ==
b1: BOX := formatExpression(args.1, MIN)
b2: BOX := formatExpression(args.2, MIN)
formatFunction("zag", [b1, b2])
formatFunction(box "zag", [b1, b2])

vconcat(h: H): H == bracket("vconcat[", "]", nary(", ", MAX, FE MIN))
pile(h: H): H == bracket("pile[", "]", nary(", ", MAX, FE MIN))
Expand All @@ -370,6 +363,7 @@ data structure.
NARY ==> -1 -- means n-ary.
PAREN p ==> bracket("(", ")", FE p)
PAREN2(p1, p2) ==> bracket("(", ")", infix(", ", MAX, FE MIN, FE MIN))
FUNCTION s ==> function(s, MAX, FE MIN)

o(n, op, hdl) ==> setHandler!(oh, n, op, hdl)

Expand Down Expand Up @@ -404,24 +398,24 @@ Here we (wrongly) return \verb|+%Infinity| and \verb|-%Infinity| for

o(0, "...", formatConstant "...")

o(1, "cos", prefix("cos", MAX, PAREN MIN))
o(1, "cot", prefix("cot", MAX, PAREN MIN))
o(1, "csc", prefix("csc", MAX, PAREN MIN))
o(1, "log", prefix("log", MAX, PAREN MIN))
o(1, "sec", prefix("sec", MAX, PAREN MIN))
o(1, "sin", prefix("sin", MAX, PAREN MIN))
o(1, "tan", prefix("tan", MAX, PAREN MIN))
o(1, "cosh", prefix("cosh", MAX, PAREN MIN))
o(1, "coth", prefix("coth", MAX, PAREN MIN))
o(1, "csch", prefix("csch", MAX, PAREN MIN))
o(1, "sech", prefix("sech", MAX, PAREN MIN))
o(1, "sinh", prefix("sinh", MAX, PAREN MIN))
o(1, "tanh", prefix("tanh", MAX, PAREN MIN))
o(1, "acos", prefix("acos", MAX, PAREN MIN))
o(1, "asin", prefix("asin", MAX, PAREN MIN))
o(1, "atan", prefix("atan", MAX, PAREN MIN))
o(1, "erf", prefix("erf", MAX, PAREN MIN))
o(1, "Gamma", prefix("Gamma", MAX, PAREN MIN))
o(1, "cos", FUNCTION "cos")
o(1, "cot", FUNCTION "cot")
o(1, "csc", FUNCTION "csc")
o(1, "log", FUNCTION "log")
o(1, "sec", FUNCTION "sec")
o(1, "sin", FUNCTION "sin")
o(1, "tan", FUNCTION "tan")
o(1, "cosh", FUNCTION "cosh")
o(1, "coth", FUNCTION "coth")
o(1, "csch", FUNCTION "csch")
o(1, "sech", FUNCTION "sech")
o(1, "sinh", FUNCTION "sinh")
o(1, "tanh", FUNCTION "tanh")
o(1, "acos", FUNCTION "acos")
o(1, "asin", FUNCTION "asin")
o(1, "atan", FUNCTION "atan")
o(1, "erf", FUNCTION "erf")
o(1, "Gamma", FUNCTION "Gamma")

o(1, "-", prefix("-", 710, FE 715))
o(1, "not", prefix("not ", 710, FE 715))
Expand Down
37 changes: 19 additions & 18 deletions src/algebra/fmt2d.spad
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ data structure.
NARY ==> -1 -- means n-ary.
PAREN p ==> bracket("(", ")", FE p)
PAREN2(p1, p2) ==> bracket("(", ")", infix(", ", MAX, FE MIN, FE MIN))
FUNCTION s ==> function(s, MAX, FE MIN)

o(n, op, hdl) ==> setHandler!(oh, n, op, hdl)

Expand All @@ -464,24 +465,24 @@ data structure.

o(0, "...", formatConstant "...")

o(1, "cos", prefix("cos", MAX, PAREN MIN))
o(1, "cot", prefix("cot", MAX, PAREN MIN))
o(1, "csc", prefix("csc", MAX, PAREN MIN))
o(1, "log", prefix("log", MAX, PAREN MIN))
o(1, "sec", prefix("sec", MAX, PAREN MIN))
o(1, "sin", prefix("sin", MAX, PAREN MIN))
o(1, "tan", prefix("tan", MAX, PAREN MIN))
o(1, "cosh", prefix("cosh", MAX, PAREN MIN))
o(1, "coth", prefix("coth", MAX, PAREN MIN))
o(1, "csch", prefix("csch", MAX, PAREN MIN))
o(1, "sech", prefix("sech", MAX, PAREN MIN))
o(1, "sinh", prefix("sinh", MAX, PAREN MIN))
o(1, "tanh", prefix("tanh", MAX, PAREN MIN))
o(1, "acos", prefix("acos", MAX, PAREN MIN))
o(1, "asin", prefix("asin", MAX, PAREN MIN))
o(1, "atan", prefix("atan", MAX, PAREN MIN))
o(1, "erf", prefix("erf", MAX, PAREN MIN))
o(1, "Gamma", prefix("Gamma", MAX, PAREN MIN))
o(1, "cos", FUNCTION "cos")
o(1, "cot", FUNCTION "cot")
o(1, "csc", FUNCTION "csc")
o(1, "log", FUNCTION "log")
o(1, "sec", FUNCTION "sec")
o(1, "sin", FUNCTION "sin")
o(1, "tan", FUNCTION "tan")
o(1, "cosh", FUNCTION "cosh")
o(1, "coth", FUNCTION "coth")
o(1, "csch", FUNCTION "csch")
o(1, "sech", FUNCTION "sech")
o(1, "sinh", FUNCTION "sinh")
o(1, "tanh", FUNCTION "tanh")
o(1, "acos", FUNCTION "acos")
o(1, "asin", FUNCTION "asin")
o(1, "atan", FUNCTION "atan")
o(1, "erf", FUNCTION "erf")
o(1, "Gamma", FUNCTION "Gamma")

o(1, "-", prefix("-", 710, FE 715))
o(1, "not", prefix("not ", 710, FE 715))
Expand Down
52 changes: 32 additions & 20 deletions src/algebra/fmtlatex.spad
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@ In order to keep spaces, we also escape spaces by a backslash.
formatString(s: S): BOX == tex1Escape("\STRING", s)
formatSymbol(s: S): BOX == tex1Escape("\SYMBOL", s)
formatFunctionSymbol(s: S): BOX == tex1Escape("\FUN", s)
formatFunction(b: BOX, lb: List BOX): BOX ==
import from OutputFormTools
empty? lb => hconcat [b, parenthesize("(", ")", empty()$BOX)]
bx: BOX := first lb
for a in rest lb repeat bx := hconcat [bx, box ", ", a]
hconcat [box "\FUNCTION{", b, box "}{", bx, box "}"]

-- a special handler for functions
function(op: S, p: Z, hh: H): H == (prec: Z, args: LE): BOX +->
bargs: List BOX := [formatExpression a for a in args]
parenthesizeIf(p < prec, formatFunction(box op, bargs))

-- If a is of the form (* x (CONCAT d y)) then replace it
-- by (INTSEP x (CONCAT d y)).
Expand Down Expand Up @@ -387,6 +398,7 @@ The operators have been mainly extracted from the definitions of
PAREN p ==> bracket("\PAREN{", "}", FE p)
BRACE p ==> bracket("{", "}", FE p)
BRACE2(p1, p2) ==> binary(BRACE p1, BRACE p2)
FUNCTION s ==> function(s, MAX, FE MIN)

o(n, op, hdl) ==> setHandler!(oh, n, op, hdl)

Expand All @@ -398,24 +410,24 @@ The operators have been mainly extracted from the definitions of
o(0, "infinity", formatConstant "\infty ") -- for %minusInfinity
o(0, "...", formatConstant "\ldots ")

o(1, "cos", prefix("\cos", 900, PAREN MIN))
o(1, "cot", prefix("\cot", 900, PAREN MIN))
o(1, "csc", prefix("\csc", 900, PAREN MIN))
o(1, "log", prefix("\log", 900, PAREN MIN))
o(1, "sec", prefix("\sec", 900, PAREN MIN))
o(1, "sin", prefix("\sin", 900, PAREN MIN))
o(1, "tan", prefix("\tan", 900, PAREN MIN))
o(1, "cosh", prefix("\cosh", 900, PAREN MIN))
o(1, "coth", prefix("\coth", 900, PAREN MIN))
o(1, "csch", prefix("\csch", 900, PAREN MIN))
o(1, "sech", prefix("\sech", 900, PAREN MIN))
o(1, "sinh", prefix("\sinh", 900, PAREN MIN))
o(1, "tanh", prefix("\tanh", 900, PAREN MIN))
o(1, "acos", prefix("\arccos", 900, PAREN MIN))
o(1, "asin", prefix("\arcsin", 900, PAREN MIN))
o(1, "atan", prefix("\arctan", 900, PAREN MIN))
o(1, "erf", prefix("\erf", 900, PAREN MIN))
o(1, "Gamma", prefix("\Gamma", 900, PAREN MIN))
o(1, "cos", FUNCTION "\cos")
o(1, "cot", FUNCTION "\cot")
o(1, "csc", FUNCTION "\csc")
o(1, "log", FUNCTION "\log")
o(1, "sec", FUNCTION "\sec")
o(1, "sin", FUNCTION "\sin")
o(1, "tan", FUNCTION "\tan")
o(1, "cosh", FUNCTION "\cosh")
o(1, "coth", FUNCTION "\coth")
o(1, "csch", FUNCTION "\csch")
o(1, "sech", FUNCTION "\sech")
o(1, "sinh", FUNCTION "\sinh")
o(1, "tanh", FUNCTION "\tanh")
o(1, "acos", FUNCTION "\arccos")
o(1, "asin", FUNCTION "\arcsin")
o(1, "atan", FUNCTION "\arctan")
o(1, "erf", FUNCTION "\erf")
o(1, "Gamma", FUNCTION "\Gamma")

o(1, "-", prefix("-", 710, FE 715))
o(1, "not", prefix("\lnot ", 710, FE 715))
Expand Down Expand Up @@ -552,7 +564,6 @@ The operators have been mainly extracted from the definitions of
\usepackage{amsmath,amssymb}
\usepackage[mathstyleoff]{breqn}
\usepackage{tensor}
\usepackage{mleftright}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newenvironment{fricasmath}[1]%
{\def\arg{#1}%
Expand All @@ -562,6 +573,7 @@ The operators have been mainly extracted from the definitions of
\def\END{\end{dmath}}%

\fi
\def\FUNCTION##1##2{##1\mathopen{}\PAREN{##2}\mathclose{}}%
\def\EulerE{e}% %e
\def\ImaginaryI{i}% %i
\def\csch{\operatorname{csch}}%
Expand All @@ -570,7 +582,7 @@ The operators have been mainly extracted from the definitions of
\def\embrace##1##2##3{\left##1\relax##3\right##2\relax}%
\def\BRACE{\embrace{\{}{\}}}%
\def\BRACKET{\embrace{[}{]}}%
\def\PAREN##1{\mleft(##1\mright)}%
\def\PAREN{\embrace{(}{)}}%
\def\ZAG##1##2{\frac{\left.{##1}\right|}{\left|{##2}\right.}}%
\def\QUOTE##1{\texttt{'}##1}%
\def\BOX##1{\boxed{##1}}%
Expand Down
Loading

0 comments on commit 24a23cb

Please sign in to comment.