Re: Q: How do I format text within a cell of GridBox[]?
- To: mathgroup at smc.vnet.net
- Subject: [mg64785] Re: Q: How do I format text within a cell of GridBox[]?
- From: bghiggins at ucdavis.edu
- Date: Thu, 2 Mar 2006 19:28:06 -0500 (EST)
- References: <dtrvfb$ltl$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Terry, Here is a modification of David's suggestion that allows the user to determine the size of the heading fragment on each line.It makes use of RegularExpressions(need Version 5.1 or later) My first post of this a few days ago never appeared on the mathgroup We need two auxillary functions.The first function called myNewLineLoc places a new line locator (\n) in front of any word after the first in a heading that has n or more word characters(digits,letters,or _) myNewLineLoc[str_String, n_Integer] := StringReplace[str, RegularExpression["((?!^)\\b\\w{" <> \ ToString[n] <> "})"] :> "\n" <> "$1"] The second function called setColumnWidths determines the size of the largest heading fragment using the same rule as above for splitting up the header SetColumnWidths[str_String, n_Integer] := Max[Map[StringLength[#] &, \ StringSplit[StringReplace[str, RegularExpression["((?!^)\\b\\w{" <> ToString[n] <> "})"] :> "\n" <> "$1"], "\n"]]] Finally we call these functions in a PrintHeading function PrintHeading[{str__String}, {colWidths__}] := \ Print[DisplayForm[GridBox[{Map[Cell[myNewLineLoc[#[[1]], #[[2]]], \ TextAlignment -> Center] &, Transpose[{{str}, {colWidths}}]]}, ColumnSpacings -> 0.8, ColumnWidths -> Map[SetColumnWidths[#[[1]], #[[2]]] \ &, Transpose[{{str}, {colWidths}}]]]]] The following shows how the options work: PrintHeading[{"Time", "Number of Times Parameters Changes Directions", \ "Error"}, {4, 6, 5}] PrintHeading[{"Time", "Number of Times Parameters Changes Directions", \ "Error"}, {4, 8, 5}] The one limitation is that the size of the columnwidth is based on the number of characters in the largest heading fragment,which does not map one-to-one on how Mathematica defines a width of a column.Hope this helps, Cheers Brian terry wrote: > Hi, > > I run a lot of simulations where I make use of GridBox[] to display a > nice table of my results. Naturally the first call to GridBox is to > display the column headings for my table and the call looks something > like this: > > > Print[GridBox[{{"Time","Number of Times Parameters Changes > Directions","Error","Parameter #1"}}, ColumnWidths->{5,5,4,6}] // > DisplayForm]; > > I pretty much got everything formatted correctly except for headings > that consist of more than one word (like the second heading). > Ideally in cases like this I would like Mathematica to automatically > break the sentence into appropriate fragments that fit into the cell > widths defined by ColumnWidths then to center each individual > fragment). Is there a simple way to do this? > > Thanks for all your help. I've been puzzling over this one for awhile. > > Terry