36
$\begingroup$

I am generating a complicated graphics with Mathematica, but with objects of given sizes (e.g. a line of length 1 inch, between two points). I want to make sure that when I export the graphics, e.g. as pdf, and then when I print it, the line length on paper will be one inch, without additional tweaking (assuming no scaling when printing).

$\endgroup$
2

4 Answers 4

15
$\begingroup$

Quite redundant after the other answers and links, but I use something like this for vector-based technical drawings and CNC data (using mm as unit). This is not foolproof as it might e.g. screw up with conflicting options and such, so make sure to check the output.

Important: SetPlotRange for your graphics explicitely:

gfx = Graphics[Line[{{10, 10}, {110, 10}, {110, 110}}], 
  PlotRange -> {{0, 200}, {0, 200}}]

ExportScaled[filename_, gfx_, format_: {210, 297}, opts___?OptionQ] :=
  Module[{mm}, mm = 72/25.4;
  Export[filename, 
   Show[gfx, ImageSize -> format*mm, ImageMargins -> 0, 
    ImagePadding -> None, AspectRatio -> Automatic], opts]]

For proper scaling, your graphics´ PlotRange and the exported ImageSize need to correspond.

ExportScaled["test.pdf", gfx, -Subtract @@@ (PlotRange /. 
     Options[gfx, PlotRange])]

Below: Output measured in Acrobat (Again: It is usually a good idea to test a few cases by measuring them with Ghostview, Acrobat or such. Works just as well for EPS export).

Mathematica graphics

$\endgroup$
9
  • $\begingroup$ Thank you, that is helpful. The actual graphics I have is larger than the page size. How do I divide it to a few pages without scaling ? $\endgroup$
    – Yigal Meir
    Commented May 11, 2012 at 20:04
  • $\begingroup$ @YigalMeir You can set PlotRange accordingly, i.e. with an offset of your choice. Example: PlotRange -> {{0+50, 200+50}, {0-40, 200-40}}. $\endgroup$
    – Yves Klett
    Commented May 12, 2012 at 9:09
  • $\begingroup$ I know this answer is over 5 years old, but I can't find anything similar. Can you please explain how this works so I can use it for other more complex graphics? I am trying to create complex geometric origami CPs, similar to yours, and I need size accuracy. Thanks. $\endgroup$
    – hmj6jmh
    Commented Aug 7, 2017 at 1:52
  • $\begingroup$ @hmj6jmh could you tell me what is unclear / not working for you? $\endgroup$
    – Yves Klett
    Commented Aug 8, 2017 at 19:41
  • $\begingroup$ I did finally adapt this code to give me precisely dimensioned output based on the actual input dimensions–exactly what I was looking for. As a fairly new user of Mathematica, I didn't understand the syntax of the ExportScaled input fields, especially the opts___?OptionQ field. I gather that this accepts graphics options but, for example, I couldn't get the option AbsoluteThickness to work. I don't know the exact syntax of the expected options so that may be the problem. I tried different options but none of these changed the line thickness. How would you set the AbsoluteThickness. $\endgroup$
    – hmj6jmh
    Commented Aug 10, 2017 at 0:09
13
$\begingroup$

You can specify ImageSize in inches. From docs > ImageSize> MoreInformation:

 Specifications for both width and height can be any of the following: 
     ...
     72di         di inches (before magnification)
     ... 

Examples:

Row[{Plot[{Sin[x], Cos[x]}, {x, -2 Pi, 2 Pi}, Frame -> True, 
AspectRatio -> 1/GoldenRatio, ImageSize -> 72 2], 
Plot[{Sin[x], Cos[x]}, {x, -2 Pi, 2 Pi}, Frame -> True, 
AspectRatio -> 1/GoldenRatio, ImageSize -> 72 3], 
Plot[{Sin[x], Cos[x]}, {x, -2 Pi, 2 Pi}, Frame -> True, 
AspectRatio -> 1/GoldenRatio, ImageSize -> 72 5]}]

enter image description here

EDIT: Per Mr.Wizard's suggestion, adding various Paddings in inches:

inches = 72; 
Panel@ Plot[{Sin[x], Cos[x]}, {x, -2 Pi, 2 Pi}, Frame -> True, 
PlotRangePadding -> {0.001 inches, 0.001 inches}, 
ImagePadding -> 0.3 inches, ImageMargins -> 0.3 inches, 
AspectRatio -> 1/GoldenRatio, ImageSize -> 5 inches]

enter image description here

$\endgroup$
0
9
$\begingroup$

Your question isn't very specific, so here is a generic example from another question

From the documentation for ImageSize:

The following settings can be given:

72di di inches (before magnification)

Suppose we want to give sizes in centimeters. We establish a scale:

cm = 72/2.54;

And we give the ImageSize in this scale, also making sure to set PlotRangePadding -> None. This gives a 1cm x 1cm orange square on a 19cm x 28cm field.

g = Graphics[{Rectangle[{0, 0}, {19, 28}], Orange, 
    Rectangle[{0, 0}, {1, 1}]}, PlotRangePadding -> None, 
   ImageSize -> {19, 28}*cm];

When exporting you can give a specific resolution to use. For printing 300dpi may be appropriate:

Export["print.tif", g, ImageResolution -> 300]
$\endgroup$
6
$\begingroup$

(I posted a similar answer on MathGroup recently.)

Here's an alternative to the other very good answers you already received.

If you need high accuracy, I recommend exporting to DXF. DXF is a format used by CAD applications requiring precision. Then you can use one of the many DXF-viewers or CAD programs to print to precision. (There seem to be a number of free programs available.)

Please see this answer of mine as well, where I described a little bit how Mathematica treats different units and how one can export figures to size.

$\endgroup$
3
  • $\begingroup$ I agree that DXF comes in handy in the CAD area and export is to scale by default, but I feel the possible higher precision as such may only be useful in very special cases. One possible catch is the Mathematica DXF format, which does not go down well with some programs (but DXF seems to be quite a nest of snakes versionwise anyway). $\endgroup$
    – Yves Klett
    Commented May 11, 2012 at 13:45
  • $\begingroup$ @Yves I don't have much experience with DXF. I did try exporting to DXF and verified that the sizes are preserved, but I haven't used the format otherwise. Are you saying that you ran into problems when using it? (BTW this answer was meant to be complementary to the others, I'll update to make it clear) $\endgroup$
    – Szabolcs
    Commented May 11, 2012 at 13:54
  • $\begingroup$ Mathematica exports 3D DXF data, but some CAD/CAM programs (e.g. CATIA) do not work properly with that. Cannot say who is mistreating the spec, though. Otherwise exported DXFs seem fine (I ran across some problems when exporting non-convex polygons which are not always triangulated properly, but that is not DXF-specific). $\endgroup$
    – Yves Klett
    Commented May 11, 2012 at 14:18

Your Answer

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

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