Skip to content

Commit 8a11cea

Browse files
committed
Update
1 parent 9586122 commit 8a11cea

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

src/Utilities/print.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,13 +683,25 @@ end
683683

684684
Base.show(io::IO, ::MIME"text/latex", model::_LatexModel) = show(io, model)
685685

686-
function Base.print(model::MOI.ModelLike; kwargs...)
686+
function _get_ijulia_latex_display()
687687
for d in Base.Multimedia.displays
688688
if Base.Multimedia.displayable(d, "text/latex") &&
689689
startswith("$(typeof(d))", "IJulia.")
690-
return display(d, "text/latex", latex_formulation(model; kwargs...))
690+
return d
691691
end
692692
end
693+
return
694+
end
695+
696+
function Base.print(
697+
model::MOI.ModelLike;
698+
_latex_display = _get_ijulia_latex_display(),
699+
kwargs...,
700+
)
701+
if _latex_display !== nothing
702+
formulation = latex_formulation(model; kwargs...)
703+
return display(_latex_display, "text/latex", formulation)
704+
end
693705
return print(stdout, model; kwargs...)
694706
end
695707

test/Utilities/print.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,32 @@ function test_show_2505()
724724
return
725725
end
726726

727+
function test_print_model_to_stdout()
728+
model = MOI.Utilities.Model{Float64}()
729+
dir = mktempdir()
730+
d = MOI.Utilities._get_ijulia_latex_display()
731+
# d is only not nothing if IJulia is running.
732+
if d === nothing
733+
open(joinpath(dir, "stdout.log"), "w") do io
734+
redirect_stdout(io) do
735+
print(model)
736+
return
737+
end
738+
return
739+
end
740+
@test read(joinpath(dir, "stdout.log"), String) ==
741+
"Feasibility\n\nSubject to:\n"
742+
end
743+
io = IOBuffer()
744+
d = Base.Multimedia.TextDisplay(io)
745+
@test Base.Multimedia.displayable(d, "text/latex")
746+
print(model; _latex_display = d)
747+
seekstart(io)
748+
@test read(io, String) ==
749+
"\$\$ \\begin{aligned}\n\\text{feasibility}\\\\\n\\text{Subject to}\\\\\n\\end{aligned} \$\$\n"
750+
return
751+
end
752+
727753
end
728754

729755
TestPrint.runtests()

test/attributes.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ end
375375
function test_showerror_OptimizeInProgress()
376376
err = MOI.OptimizeInProgress(MOI.VariablePrimal())
377377
@test sprint(showerror, err) ==
378-
"$err: Cannot get result as the `MOI.optimize!` has not finished."
378+
"$(typeof(err)): Cannot get result as the `MOI.optimize!` has not finished."
379379
return
380380
end
381381

0 commit comments

Comments
 (0)