Mapping over the last level of a list, or equivalent operations

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?

-
"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. – Oleksandr R. Feb 28 '13 at 2:06
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. – Lara Jordan Feb 28 '13 at 7:00
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. – Oleksandr R. Feb 28 '13 at 12:34

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.

-
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? – Lara Jordan Feb 28 '13 at 6:32
The very top level is level 0. – Seth Chandler Feb 28 '13 at 12:56
Oh right, thanks! – Lara Jordan Mar 1 '13 at 8:39

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

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.

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


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

-

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}]


{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.

-