7
$\begingroup$

I'm trying to display a number tables of data in my document which have column headings that are longer than the cell width for those columns so they need to wrap within the grid cell. I have no problem getting the text to wrap however when it does the wrapped text is indented from the first line rather than in line with it - which produces other undesirable results.

I'd like the wrapped text to be in vertical alignment with the first line of text.
As an alternative, I'd like to have all the text in the cell centered.

This is what I get: Table with undesirable word wrap behaviour

The code to generate this table is as follows

inputs = {{{231001, "BaseCase", "Base"}, {231001, "BaseCase", 
 "10"}, {231001, "BaseCase", "8"}, {231001, "BaseCase", 
 "Base"}, {231001, "BaseCase", "Base"}, {231001, "BaseCase", 
 "Base"}, {231001, "BaseCase", "Base"}, {231001, "BaseCase", 
 "Inactive"}, {231001, "BaseCase", "Base"}}, {{231002, 
 "BaseCase + 4xFail", "Base"}, {231002, "BaseCase + 4xFail", 
 "10"}, {231002, "BaseCase + 4xFail", "4"}, {231002, 
 "BaseCase + 4xFail", "Base"}, {231002, "BaseCase + 4xFail", 
 "Base"}, {231002, "BaseCase + 4xFail", "Base"}, {231002, 
 "BaseCase + 4xFail", "Base"}, {231002, "BaseCase + 4xFail", 
 "Inactive"}, {231002, "BaseCase + 4xFail", "Base"}}, {{231003, 
 "BaseCase + 6xFail", "Base"}, {231003, "BaseCase + 6xFail", 
 "10"}, {231003, "BaseCase + 6xFail", "6"}, {231003, 
 "BaseCase + 6xFail", "Base"}, {231003, "BaseCase + 6xFail", 
 "Base"}, {231003, "BaseCase + 6xFail", "Base"}, {231003, 
 "BaseCase + 6xFail", "Base"}, {231003, "BaseCase + 6xFail", 
 "Inactive"}, {231003, "BaseCase + 6xFail", "Base"}}, {{231004, 
 "BaseCase + 8xFail", "Base"}, {231004, "BaseCase + 8xFail", 
 "10"}, {231004, "BaseCase + 8xFail", "8"}, {231004, 
 "BaseCase + 8xFail", "Base"}, {231004, "BaseCase + 8xFail", 
 "Base"}, {231004, "BaseCase + 8xFail", "Base"}, {231004, 
 "BaseCase + 8xFail", "Base"}, {231004, "BaseCase + 8xFail", 
 "Inactive"}, {231004, "BaseCase + 8xFail", "Base"}}, {{231005, 
 "BaseCase + 10xFail", "Base"}, {231005, "BaseCase + 10xFail", 
 "10"}, {231005, "BaseCase + 10xFail", "10"}, {231005, 
 "BaseCase + 10xFail", "Base"}, {231005, "BaseCase + 10xFail", 
 "Base"}, {231005, "BaseCase + 10xFail", "Base"}, {231005, 
 "BaseCase + 10xFail", "Base"}, {231005, "BaseCase + 10xFail", 
 "Inactive"}, {231005, "BaseCase + 10xFail", "Base"}}};

factorNames = {\[ThinSpace]"Animal Purpose", "Event Duration", 
 "Long Failure Count", "Smelly Dam Capacity", "Septic Dam Capacity",
  "New Dam Purpose", "Big Pump Status", 
 "Temp Elephant Status (Train 0)", "Plant Variety"\[ThinSpace]};

tableText = Join[
 {Join[{"Scenario Number", "Scenario Name"}, factorNames]},
 Join[inputs[[#, 1, {1, 2}]], inputs[[#, All, 3]]] & /@ 
  Range[Length[inputs]]
 ];

Text[Grid[tableText, Frame -> All, 
 ItemStyle -> {FontSize -> 10, {Bold}}, ItemSize -> {{5, 15, {6}}}]]
$\endgroup$
  • 3
    $\begingroup$ You can use BaseStyle -> {LineIndent -> 0}. In general you will have no guarantee that the string won't be split just before whitespace which will later look like an indent anyway. From my experience, prepare labels exactly as you want them to be, don't count on automatic text styling. $\endgroup$ – Kuba Jul 30 '15 at 7:50
  • $\begingroup$ @Kuba thanks, that option is the one. You are correct about the variability in line splitting. Unfortunately in this instance the column headings are quite a dynamic thing as they are pulled from a database so it's not practical to have a predefined format for each one that could pop up - going with the automatic wrapping is my only feasible option. $\endgroup$ – CrustyNoodle Jul 30 '15 at 23:32
7
$\begingroup$

You can control this using the LineIndent option:

Grid[tableText[[All, 7 ;; 9]],  (* smaller table for example *)
 Frame -> All, 
 ItemStyle -> {FontSize -> 10, LineIndent -> 0, {Bold}},
 ItemSize -> {{5, 15, {6}}}
]

enter image description here

Related:

$\endgroup$
  • $\begingroup$ Terrific - just what I needed. It could be argued that I should have been able to find this one myself - but I clearly I didn't! Thank you $\endgroup$ – CrustyNoodle Jul 30 '15 at 23:27
  • $\begingroup$ @CrustyNoodle Glad I could help. It does not appear to be mentioned in the documentation for Grid so I think it is not "easily" found. One has to already know where to look as it were. $\endgroup$ – Mr.Wizard Jul 31 '15 at 4:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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