3
$\begingroup$

I'm probably missing something obvious here, but I haven't been able to figure out how to get the Mathematica function Labeled to print an expression with a left-justified label on top of it. The Help page for Labeled says that

Labeled[expr, lbl, pos] places lbl at a position specified by pos

and that pos may have the form {side, align}, in which case the label appears "on the specified side with the specified alignment." It then says that side may be either Left, Right, Top, or Bottom, while align may be either Center, Left, Right, Top, or Bottom. Although it's possible that I've misinterpreted this, I would have thought that the following code would thus place the label "Foo" left-justified on top of the expression {a, b, c, d}:

Labeled[
    Framed[ {a, b, c, d} ],
     "Foo",
     {Top, Left}
]

The above code, though, places the label "Foo" centered on top of the expression {a, b, c, d}. Any idea what's going on here?

$\endgroup$
  • $\begingroup$ I get the same on Win 8.1 Pro in 10.4.1 and 11.0. I think it could be a bug. Report to WRI. $\endgroup$ – Edmund Sep 21 '16 at 10:08
  • $\begingroup$ Many thanks, @Edmund! That was my guess as well, but based on the response from @Mr.Wizard, I wonder whether there's a problem with my code and/or my analysis. I'm looking forward to reading his response to the comments I just appended to his response. $\endgroup$ – grateful user Sep 21 '16 at 22:55
7
$\begingroup$

This is a syntax ambiguity arising from the allowance for this form:

Labeled[Framed[{a, b, c, d}], {"Foo", "Bar"}, {Top, Left}]

enter image description here

Instead you need:

Labeled[Framed[{a, b, c, d}], "Foo", {{Top, Left}}]

enter image description here

$\endgroup$
  • $\begingroup$ Many thanks, Mr. Wizard! First of all, many thanks for providing a way to implement this correctly.In my experience, you really are a Wizard, so I want to make sure I understand what your diagnosis and treatment. Just to clarify, are you saying that this is, in fact, a bug in Mathematica (that is to say, that my original code should have worked), but that Wolfram's bug is the result of their allowing a syntax that introduces an ambiguity? $\endgroup$ – grateful user Sep 21 '16 at 21:12
  • $\begingroup$ Many thanks, @Mr.Wizard! Although your solution indeed works, I'm not sure why, since I don't see a syntactic form on the Help page that allows a single symbol (representing a single label) as the second argument, but a list of {side, align} position specifications as the third argument. If I'm interpreting the Help page correctly, it seems like the expression Labeled[ Framed[{a, b, c, d}], {"Foo"}, {{Top, Left}} ] should yield the behavior I was looking for (which it does), but not the expression Labeled[ Framed[{a, b, c, d}], "Foo", {{Top, Left}} ]. Is there an error in my analysis? $\endgroup$ – grateful user Sep 21 '16 at 22:25
  • $\begingroup$ Sorry for the multiple comments, @Mr.Wizard. Regarding the syntactic ambiguity of Labeled[ Framed[{a, b, c, d}], "Foo", {Top, Left} ]: Couldn’t Mathematica interpret the 3rd arg of a 3-arg form of Labeled based on the type of the 2nd arg? If the 2nd arg is a symbol, {Top, Left} would be interpreted as I'd intended, but if the 2nd arg is a list, "Top" would apply to the first label in that list, while "Left" would apply to the second label. It may be that Mathematica doesn’t parse it this way, but I’m not sure I see any inherent ambiguity. Am I wrong about this? $\endgroup$ – grateful user Sep 21 '16 at 22:46
  • $\begingroup$ @gratefuluser Honestly I did not analyze it to this degree, I just hoped to offer a quick solution. I tend to agree (without exhaustive consideration) that the function could recognize Labeled[x, "foo", {Top, Left}] as you intended, but I am not prepared to say that the fact that it does not is a bug. If you do contact support regarding this I am curious to know what they say. $\endgroup$ – Mr.Wizard Sep 22 '16 at 14:30
  • 1
    $\begingroup$ Me neither, but i) the argument format does allow for multiple labels on the same side without warning and ii) the documentation never states that there is such limitation. So I expected it to work (though I knew it wouldn't, but that is only because I know that it is implemented by Grid or similar with a limited number of positions instead of Graphics and practically unlimited coordinates). $\endgroup$ – István Zachar May 14 '19 at 21:17

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.