Skip to content

Commit 46adb40

Browse files
authored
[Utilities] fix printing of LaTeX model (#2141)
1 parent 4ea435d commit 46adb40

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

src/Utilities/print.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ using Printf
88

99
_drop_moi(s) = replace(string(s), "MathOptInterface." => "")
1010

11+
_escape_braces(s) = replace(replace(s, "{" => "\\{"), "}" => "\\}")
12+
1113
struct _PrintOptions{T<:MIME}
1214
simplify_coefficients::Bool
1315
default_name::String
@@ -330,8 +332,7 @@ function _to_string(options::_PrintOptions, set::MOI.AbstractSet)
330332
end
331333

332334
function _to_string(::_PrintOptions{MIME"text/latex"}, set::MOI.AbstractSet)
333-
set_str = replace(replace(_drop_moi(set), "{" => "\\{"), "}" => "\\}")
334-
return string("\\in \\text{", set_str, "}")
335+
return string("\\in \\text{", _escape_braces(_drop_moi(set)), "}")
335336
end
336337

337338
#------------------------------------------------------------------------
@@ -575,7 +576,9 @@ function _print_model(
575576
println(io, "\\text{Subject to}\\\\")
576577
for (F, S) in MOI.get(model, MOI.ListOfConstraintTypesPresent())
577578
if options.print_types
578-
println(io, " & \\text{$(_drop_moi(F))-in-$(_drop_moi(S))} \\\\")
579+
f_str = _escape_braces(_drop_moi(F))
580+
s_str = _escape_braces(_drop_moi(S))
581+
println(io, " & \\text{$f_str-in-$s_str} \\\\")
579582
end
580583
for cref in MOI.get(model, MOI.ListOfConstraintIndices{F,S}())
581584
println(io, " & ", _to_string(options, model, cref), " \\\\")

test/Utilities/print.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -384,24 +384,24 @@ function test_latex()
384384
$$ \begin{aligned}
385385
\min\quad & 2.0 + 1.0 x + 3.1 y - 1.2 z \\
386386
\text{Subject to}\\
387-
& \text{ScalarAffineFunction{Float64}-in-EqualTo{Float64}} \\
387+
& \text{ScalarAffineFunction\{Float64\}-in-EqualTo\{Float64\}} \\
388388
& 0.0 + 1.0 x - 1.0 y = 0.0 \\
389-
& \text{ScalarAffineFunction{Float64}-in-GreaterThan{Float64}} \\
389+
& \text{ScalarAffineFunction\{Float64\}-in-GreaterThan\{Float64\}} \\
390390
& 0.0 + 2.0 x \ge 1.0 \\
391-
& \text{ScalarAffineFunction{Float64}-in-Interval{Float64}} \\
391+
& \text{ScalarAffineFunction\{Float64\}-in-Interval\{Float64\}} \\
392392
& 0.0 + 2.0 x \in [1.0, 2.0] \\
393-
& \text{ScalarQuadraticFunction{Float64}-in-LessThan{Float64}} \\
393+
& \text{ScalarQuadraticFunction\{Float64\}-in-LessThan\{Float64\}} \\
394394
& 0.0 + 1.0 y - 1.0 z + 2.0 x^2 \le 1.0 \\
395395
& \text{VectorOfVariables-in-SecondOrderCone} \\
396396
& \begin{bmatrix}
397397
x\\
398398
y\end{bmatrix} \in \text{SecondOrderCone(2)} \\
399-
& \text{VectorAffineFunction{Float64}-in-SecondOrderCone} \\
399+
& \text{VectorAffineFunction\{Float64\}-in-SecondOrderCone} \\
400400
& \begin{bmatrix}
401401
1.0\\
402402
0.0 + 1.0 x\\
403403
0.0 + 1.0 y\end{bmatrix} \in \text{SecondOrderCone(2)} \\
404-
& \text{VectorQuadraticFunction{Float64}-in-ExponentialCone} \\
404+
& \text{VectorQuadraticFunction\{Float64\}-in-ExponentialCone} \\
405405
& \begin{bmatrix}
406406
0.0 + 1.0 x^2\\
407407
0.0 + 1.0 y\\
@@ -410,7 +410,7 @@ function test_latex()
410410
1.0\\
411411
0.0 + 1.0 x^2\\
412412
0.0 + 1.0 y\end{bmatrix} \in \text{ExponentialCone()} \\
413-
& \text{VariableIndex-in-GreaterThan{Float64}} \\
413+
& \text{VariableIndex-in-GreaterThan\{Float64\}} \\
414414
& x \ge 0.1 \\
415415
& \text{VariableIndex-in-Integer} \\
416416
& z \in \mathbb{Z} \\
@@ -597,12 +597,12 @@ function test_nlp()
597597
$$ \begin{aligned}
598598
\min\quad & x_{1} \times x_{4} \times (x_{1} + x_{2} + x_{3}) + x_{3} \\
599599
\text{Subject to}\\
600-
& \text{VariableIndex-in-GreaterThan{Float64}} \\
600+
& \text{VariableIndex-in-GreaterThan\{Float64\}} \\
601601
& x_{1} \ge 1.1 \\
602602
& x_{2} \ge 1.2 \\
603603
& x_{3} \ge 1.3 \\
604604
& x_{4} \ge 1.4 \\
605-
& \text{VariableIndex-in-LessThan{Float64}} \\
605+
& \text{VariableIndex-in-LessThan\{Float64\}} \\
606606
& x_{1} \le 5.1 \\
607607
& x_{2} \le 5.2 \\
608608
& x_{3} \le 5.3 \\

0 commit comments

Comments
 (0)