MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Q: How do I format text within a cell of GridBox[]?

  • To: mathgroup at
  • Subject: [mg64785] Re: Q: How do I format text within a cell of GridBox[]?
  • From: bghiggins at
  • Date: Thu, 2 Mar 2006 19:28:06 -0500 (EST)
  • References: <dtrvfb$ltl$>
  • Sender: owner-wri-mathgroup at


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 _)

    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



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

  • Prev by Date: Re: Re: Re: General--Making the DisplayFormula style in ArticleModern look like Traditional
  • Next by Date: Re: Possible Bug in ArcTan ?
  • Previous by thread: Re: Q: How do I format text within a cell of GridBox[]?
  • Next by thread: Re: Re: Re: Re: finding the position of a pattern in list (Correction)