4

I'm trying out making a CDF and I want it to print within the margins of Letter paper. There is an ImageSize option that I would like to set; some plots will be in a grid, some alone. I thought using Quantity would be good for this since I know the width of the paper. I tried:

Plot[Sin[x], {x, 0, 4 Pi}, ImageSize -> {Quantity[4, "Inches"], Automatic}]

This gives an error:

The specified setting for the option Graphics3DBoxOptions, ImageSize cannot be used.

Does anyone know what is going on here? How do I set the image size in units? What are the units of ImageSize? Can I use UnitConvert on Quantity to get to the ImageSize units? I'm using Mma 10.

Thanks,

Edmund

Edmund
  • 42,267
  • 3
  • 51
  • 143
  • 1
    Possible duplicates?: (5442), (37345) – Mr.Wizard Sep 15 '14 at 11:51
  • It would be nice, though, if the Unit-based answers were also given there. – Michael E2 Sep 15 '14 at 12:29
  • @MichaelE2 If this is closed and if it is agreeable to those who answered this question I can merge the two, which will result in these answers being moved there. Some editing may be necessary. – Mr.Wizard Sep 15 '14 at 12:52
  • 1
    Those other answers don't use the built-in units support. You'd need to create variables for all of your conversions. With the unit based answer below you don't need to do this. You can built a simple function that has all of it built in. toPrinterPointsMagnitude[distance_?QuantityQ] := QuantityMagnitude@UnitConvert[distance, "PrinterPoints"]. This type of fully support solution is what I was looking for. – Edmund Sep 15 '14 at 12:53
  • @Edmund That's what I said. It seems to be better organized, though, IMO, if these approaches were collected in one place. – Michael E2 Sep 15 '14 at 12:54
  • @MichaelE2, Ah, ok. Yes. – Edmund Sep 15 '14 at 12:59
  • @Edmund +1 to your Q and the A's, btw. I see I made a typo above ("where" instead of "were" - oops) -- probably caused some confusion. – Michael E2 Sep 15 '14 at 13:03

2 Answers2

5

Perhaps

inches = 72;
Plot[Sin[x], {x, 0, 4 Pi}, ImageSize -> { 4  inches, Automatic}]

enter image description here

If you have to use Quantity you can set the ImageSize converting inches to printer points:

ImageSize -> { 72 QuantityMagnitude[Quantity[4, "Inches"]], Automatic}

Update: or, better yet,

Plot[Sin[x], {x, 0, 4 Pi}, PlotStyle->Thick,
 ImageSize -> { 
   QuantityMagnitude[
    UnitConvert[Quantity[4, "Inches"], 
        "PrinterPoints" (* or "DesktopPublishingPoints" *)]], Automatic}]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
  • Not exactly the solution I'm looking for. I'd like to use the proper units as supported by Mma. Why does 72 = 1 inch? What are the ImageSize units? – Edmund Sep 15 '14 at 11:26
  • @Edmunds, please see the update... – kglr Sep 15 '14 at 11:27
  • @kguler, Very nice. That is it! Thanks. – Edmund Sep 15 '14 at 11:30
  • Impressive, as PrinterPoints is not listed in QuantityUnits\Private`$UnitReplacementRules` and I cound't find it as a valid unit for conversion. Would that work offline? – rhermans Sep 15 '14 at 11:38
  • @kguler, With this info I have made the following: toPrinterPointsMagnitude[x_?QuantityQ] := QuantityMagnitude@UnitConvert[x, "PrinterPoints"] – Edmund Sep 15 '14 at 11:40
  • 1
    I love Mathematica!!! toPrinterPointsMagnitude[Quantity[4, "Light Year"]] LOL!! – Edmund Sep 15 '14 at 11:50
  • @rhermans, it was pure luck that the first thing i tried worked:) didn't check $UnitReplacementRules. – kglr Sep 15 '14 at 11:53
  • 1
    @rhermans, ... there is "DesktopPublishingPoints" that shows up when i use ?CalculateUnits`UnitCommonSymbols`*oint*. – kglr Sep 15 '14 at 12:28
4

The ImageSize documentation, under Details reads:

"Specifications for both width and height can be any of the following: "

d d printer's points (before magnification)

72di di inches (before magnification)

So if you want 4 inches you can use:

Plot[Sin[x], {x, 0, 4 Pi}, ImageSize -> (72 4)]

for other units

toPrintPoints = QuantityMagnitude[72 UnitConvert[#, "Inches"]] &
Plot[x, {x, 0, 1}, ImageSize -> toPrintPoints[Quantity[12, "cm"]]]
rhermans
  • 36,518
  • 4
  • 57
  • 149
  • 1
    It seems that I'm late... – rhermans Sep 15 '14 at 11:26
  • Nice solution. Thank you for taking the time to answer. I like kguler's better as I don't have to remember what 72 is. – Edmund Sep 15 '14 at 11:32
  • 1
    I melded your function into kguler's solution to create: toPrinterPointsMagnitude[x_?QuantityQ] := QuantityMagnitude@UnitConvert[x, "PrinterPoints"]. Thanks. – Edmund Sep 15 '14 at 11:42