1

How might I construct a function called EmToPX[p] such that the following 2 InputFields would be the same width. If not please explain why doing such isn't possible.

InputField["a", ImageSize -> 100]
InputField["a", FieldSize -> EmToPX[100]]

After some trial an error ~7.2 appears to be the proper number of EMs above( InputField["a", FieldSize -> 7.2]), but after trying to convert to a function using proper ratios it appears such technique doesn't work very well.

EmToPX[p_] := (N[
    p (1.888888 + CurrentValue[EvaluationNotebook[], "FontSize"]) ]);
Manipulate[
 Column[{
   InputField["a", ImageSize -> EmToPX[a]],
   InputField["a", FieldSize -> a],
   EmToPX[7.2]
   }]
 , {a, 1, 100}
 ]

Background: Ultimately I am working with Grid elements and would like an easy way to switch back and froth between CellSize,ImageSize,ItemSize,FieldSize.

William
  • 7,595
  • 2
  • 22
  • 70
  • You might want to also read this – rm -rf Sep 16 '13 at 22:52
  • I believe this is effectively a duplicate but I cannot find the original. Can anyone help? – Mr.Wizard Sep 17 '13 at 00:32
  • 2
    If you're working with Grid, then for the love of all that is good and holy, don't do it this way. Use Pane to define your constraints, and let Grid just snap itself to the Pane elements. – John Fultz Sep 17 '13 at 05:17
  • @JohnFultz You are quit right. That does solve my most immediate problem. I think it would be beneficial though if there there was a way to seamlessly add, and subtract between the EMs and pixels. At minimum a reason why such can't be done. – William Sep 17 '13 at 11:21
  • Discussion about why it can't be done: http://stackoverflow.com/questions/1462997/what-is-the-relationship-between-ems-and-pixels (implementations seem to vary though. Might be possible in MMA even if it shouldn't be possible...) – C. E. Sep 17 '13 at 11:27
  • @Anon In CSS and HTML the most common implementation that I'm aware of that isn't is correct. Read the comment below the selected answer. In traditional typography it is. In CSS, however, it is the font height. I could be wrong but Mathematica is quit tied to the web's creation, and Wolfram did so many things so clean and elegantly that I doubt that he ignored the current web standard. – William Sep 17 '13 at 11:32
  • @LiamWilliam I read those answers before and still I was of the opinion that it would likely not be possible to find a relationship. CSS and MMA have nothing in common. But I now read the documentation for FieldSize: it seems you just have to figure out what the line height is and you're done. MMA has its own definition for em. They're not real ems. – C. E. Sep 17 '13 at 11:38
  • @Anon You are quit right about the line height. I believe the line-height in CSS defaults to the current font size. I just hope we can determine how Mathematica determines the line height. It doesn't appear to be in the Stylesheet or Option in the option inspector, but that is just after some quick searching. – William Sep 17 '13 at 12:18
  • 1
    @Mr.Wizard See above comment. – István Zachar Sep 18 '13 at 06:49
  • @István Thanks; that's the one. – Mr.Wizard Sep 18 '13 at 06:52
  • @IstvánZachar Thank you for the link. – William Sep 18 '13 at 13:20

0 Answers0