4
$\begingroup$

I am trying to achieve the following effect produced in Excel in Wolfram Dataset:

enter image description here

The following attempt doesn't work:

Dataset[
    {<|"small column"->123,"very large column heading"->123|>,<|"small column"->45678910,"very large column heading"->456|>,<|"small column"->789,"very large column heading"->78910|>},
    HeaderDisplayFunction->(Style[#,AutoIndent->True,LineIndent->0,TextAlignment->Center]&)
]

enter image description here

$\endgroup$

2 Answers 2

6
$\begingroup$

You can wrap the text in Pane with an UpTo style width specification to get the wrapping, and use HeaderAlignment for the centering:

automatic = Automatic;
Dataset[{<|"small column" -> 123, 
   "very large column heading" -> 123|>, <|"small column" -> 45678910,
    "very large column heading" -> 456|>, <|"small column" -> 789, 
   "very large column heading" -> 78910|>}, 
 HeaderDisplayFunction -> (Style[Pane[#, {UpTo@75, Automatic}], 
     TextAlignment -> Center, LineBreakWithin -> automatic] &), 
 HeaderAlignment -> {Center, Center}]

enter image description here

Note the use of LineBreakWithin to allow the header lines to actually wrap (thanks @lericr for pointing out that it doesn't work on desktop otherwise!). The last thing needed to make this work is using automatic as value for LineBreakWithin - this is due to a bug in HeaderDisplayFunction, which replaces any literal Automatic with Nothing (which would break the option). To work around this, I simply hide Automatic by using a variable.

$\endgroup$
2
  • $\begingroup$ Surprisingly, this doesn't work for me with version 13.2. The text is simply truncated, not wrapped. The Pane works fine on its own, just not inside the HeaderDisplayFunction option. $\endgroup$
    – lericr
    Apr 1, 2023 at 15:32
  • $\begingroup$ Interesting... I wrote this answer on my phone, and there it works as expected - I never would have thought that the wolfram cloud app does something "better" than the desktop app. Anyways, I have updated the answer with a fixed version $\endgroup$
    – Lukas Lang
    Apr 1, 2023 at 16:23
5
$\begingroup$

One could use ToString with PageWidth options. Since it adds two \n, we'll do a little post-processing:

Dataset[ ... , 
 HeaderDisplayFunction -> 
  (StringReplace[ToString[#, PageWidth -> 15], "\\\n" -> ""] &)]

Result:

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.