There is an example on the Using the Format module page on the OCaml web site that gives an example of the differences between structural and packing hov boxes. I am trying to replicate this example. (I am using OCaml 3.12.1.)
With the following input:
Format.printf "@[<hov 2>(---@\n@[<hov 2>(---@\n@[<hov 2>(---@,)@]@,)@]@,)@]@\n"
I get the expected output:
(---
(---
(---)))
What I can not figure out is how to get the output the page ascribes to "structural boxes":
(---
(---
(---
)
)
)
I was uncertain as to which Format.printf
identifier maps to these boxes, so I tried several variations:
Format.printf "@[<hv 2>(---@\n@[<hv 2>(---@\n@[<hv 2>(---@,)@]@,)@]@,)@]@\n"
Format.printf "@[<2>(---@\n@[<2>(---@\n@[<2>(---@,)@]@,)@]@,)@]@\n"
Format.printf "@[<b 2>(---@\n@[<b 2>(---@\n@[<b 2>(---@,)@]@,)@]@,)@]@\n"
But all of the above give the same output as with the <hov 2>
initial example. Would anyone have any ideas how I can get output similar to the second example from web page?
I would have thought that your second example (with hv boxes) should work. But it seems that @\n does not have the behavior we expect.
Solution 1: force the line break before the right parenthesis, i.e. replace @, by @\n.
let () =
Format.printf "@.";
Format.printf "@[<hov 2>(---@\n@[<hov 2>(---@\n@[<hov 2>(---@\n)@]@\n)@]@\n)@]@\n";
Format.printf "@."
Result:
(---
(---
(---
)
)
)
Problem: there will always be a line break, it will never print:
(---(---(---)))
even if it has the room for it. If that is an issue for you, see solution 2.
Solution 2: Use longer lines :) If the break is caused by a long line then an hv box does break the line before the closing parenthesis.
let () =
Format.printf "@.";
Format.printf "@[<hv 2>(------------------------------------------------------------------------@,@[<hv 2>(---@,@[<hv 2>(---@,)@]@,)@]@,)@]";
Format.printf "@."
Result:
(------------------------------------------------------------------------
(---(---))
)
However, the closing parenthesis is not aligned with the opening one.
Solution 3: If you want the right parenthesis to be aligned with the left one, you need two boxes.
let () =
Format.printf "@.";
Format.printf
"@[<hv>@[<hv 2>(------------------------------------------------------------------------\
@[<hv>@[<hv 2>(------------------------------------------------------------------------\
@[<hv>@[<hv 2>(------------------------------------------------------------------------\
@]@,)@]@]@,)@]@]@,)@]";
Format.printf "@."
Result:
(------------------------------------------------------------------------
(------------------------------------------------------------------------
(------------------------------------------------------------------------
)
)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With