4
$\begingroup$

I would like to style each element of my list using Style. But when I try

Lista = {{"a", "b", "a"}, {"n", "g", "r"}, {"d", "fgh", "Df"}}
Dimensions[Lista]
FormattedLista = Style[Lista, FontSize -> 10]
Dimensions[FormattedLista]

the structure of the original list is lost.

How can I map Style onto elements at the last level only, i.e. without changing the structure of the list?

$\endgroup$
3
  • 1
    $\begingroup$ "Mapping onto individual elements" is a job for MapAt. What you want is mapping onto all elements at the last level. I changed the wording of your question accordingly. $\endgroup$ Feb 28, 2013 at 2:06
  • 1
    $\begingroup$ Thanks for the help Oleksandr R. Just a comment - I see you edited the title of the question. I know it is more Mathematica-correct but I intentionally made it a simple title so that other beginner users can understand the question. For example, if I had searched for the solution to my problem and had seen this version of the question title, I wouldn't have understood what the question was about and I would not have looked at the answer. $\endgroup$
    – lara
    Feb 28, 2013 at 7:00
  • $\begingroup$ I understand. The problem with the original title was twofold: the question doesn't depend on the particular function to be mapped (i.e. Style), and also it isn't clear to me that there is an unambiguous concept of an "individual list element" in the case of nested lists. I had hoped my edit would make the essence of the question clearer, but if you disagree then please feel free to change the title to something else you think more appropriate. $\endgroup$ Feb 28, 2013 at 12:34

4 Answers 4

11
$\begingroup$
Map[Style[#, FontSize -> 10] &, Lista, {2}]

The Style[#,FontSize->10]& is an anonymous function.

To see why this works, evaluate TreeForm[Lista]. The {2} as the third argument of Map means that the Style function will be inserted just above the second level of your expression and thus preserve the dimensions you desire.

$\endgroup$
3
  • $\begingroup$ I understand the anonymous function. When I look at the TreeForm I see three levels, the main {} then the three sub {} then the individual elements. What I don't understnad here is why we don't use Style on the individual elements by specifying {3} at the end of the the Map function since the individual elements are at the 3rd level not the second? $\endgroup$
    – lara
    Feb 28, 2013 at 6:32
  • $\begingroup$ The very top level is level 0. $\endgroup$ Feb 28, 2013 at 12:56
  • $\begingroup$ Oh right, thanks! $\endgroup$
    – lara
    Mar 1, 2013 at 8:39
8
$\begingroup$

How about a rule?

Lista = {{"a", "b", "a"}, {"n", "g", "r"}, {"d", "fgh", "Df"}};
formattedlist=Lista /. a_String -> Style[a, FontSize -> 10]

----EDIT----

and adding to your comment (and my confusion somewhat), if you want to apply styles to different heads (is that what you want?) you may amend your rules to something a little ugly:

Listb = {{"a", "b", "a", 12}, {"n", "g", "r", π}, {"d", "fgh", 
"Df", 0.2341}};
formattedlist = Listb /.{b___, a_Integer, c___} -> {b, Style[a, Red, 33], c}
                      /.{b___, a_Symbol, c___} -> {b, Style[a, Orange, 21], c} 
                      /.{b___, a_Real, c___} -> {b, Style[a, Brown, 9], c}
                      /.a_String -> Style[a, 11]

which gives

Mathematica graphics

but I don't really understand if you want to apply Style to expressions so that they look different in your output stream or so that you use them as, say, a PlotLabel.

If this is a little confusing, it is worth noting that atoms (symbols, strings etc) with a specific Style stop being atoms:

LeafCount["a"]
(*Out*) 1

but

LeafCount[Style["a", 12]]
(*Out*) 3

which, can be misleading because, at the end of the day, all you are seeing is a string. In this case though, adding Style means that you can't "write" on a small, red thing, whether it is a string, symbol or whatever, i.e.

Style[a, Red, 12] = Interpolation[RandomReal[{0, 2}, 10]]

tells you a is tagged with the tag Style which is protected.

$\endgroup$
2
  • $\begingroup$ This only works with strings. If I use numbers or a combination of strings and numbers what would be appropriate instead of _String? $\endgroup$
    – lara
    Feb 28, 2013 at 6:40
  • $\begingroup$ Hopefully the edit is more illuminating? $\endgroup$
    – gpap
    Feb 28, 2013 at 10:23
6
$\begingroup$
SetAttributes[style, Listable]
style[a_String] := Style[a, FontSize -> 10]

style has Listable attribute. It works on list, and keeps the shape.

$\endgroup$
1
$\begingroup$

Here's a way to apply formatting without changing the structure of the expression.

This is a bit unusual, and I do not recommend it as I am not absolutely sure it is reliable, but I thought it's worth showing:

fun = Function[str, "\!\(\*" <> ToString[ToBoxes@Style[str, Red], InputForm] <> "\)"]

list = {{"a", "b", "a"}, {"n", "g", "r"}, {"d", "fgh", "Df"}}
Map[fun, list, {-1}]

Mathematica graphics

{Dimensions[list], Depth[list]}

(* ==> {{3,3}, 3} *)

Neither the depth nor the dimensions have changed because the formatting information has been embedded in the string.

$\endgroup$

Your Answer

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

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