Automatizing PSfrag export

Let me try to give you some hints.

Problem 1.

When you look at a specific tick in a Graphics it usually looks similar to the following

{200., 200., {0.00625, 0.}, {GrayLevel[0.], AbsoluteThickness[0.25]}}

it means, at position 200 draw the number 200. The next list is the specification of the tick-length and then follows a list of graphic-directives to use. I assume if you convert the label into another form, like a string, you may have more luck with your approach. Let's try something

transformTick[{pos_, label_, length_List, spec_List}] :=
 {pos, ToString[label, TraditionalForm], length, spec}

p1 = Plot[x^2, {x, 0, 1000}, Frame -> True]
p2 = Show[p1, FrameTicks ->
  Map[transformTick, FrameTicks /. AbsoluteOptions[p, FrameTicks], {2}]]

Mathematica graphics Mathematica graphics

It seems the Ticks are a bit smaller and the dot after the number does not appear in the original but otherwise it looks OK for me. If you now compare

ImportString[ExportString[#, "EPS"], "EPS"] & /@ {p1, p2}

it seems (at least on my machine) that the numbers are tight together in the second case. Even if this simple idea does not work, you now know how you can transform the Ticks into another form.

Problem 2.

Maybe I can help you with a workaround. It seems to me that when you use All as value to FrameTicks you just get the same ticks on the other side. Other differences are really small.

p1 = Plot[x^2, {x, 0, 1000}, Frame -> True]
p2 = Plot[x^2, {x, 0, 1000}, Frame -> True, FrameTicks -> All]

Mathematica graphics Mathematica graphics

Now you say, you need to extract the Ticks for the All setting but I don't understand why. Let's look again on my first code-block with the tick. Note, that it does not contain a position! It does only contain an x-value where the tick is, the y-value is calculated automatically. Therefore, the tick-specification for the same tick on the top does look exactly the same.

Well, if this were true, than the only thing necessary to clone FrameTicks->All is to use the default setting and copy ticks for the first axis to the third and the ticks for the second axis to the fourth axis? Right!

Show[p1, FrameTicks -> (#[[{1, 2, 1, 2}]] &[FrameTicks /. AbsoluteOptions[p]])]

Mathematica graphics

And it's getting even better: If you study the help-page of FrameTicks carefully, you find this example

Plot[Cos[x], {x, 0, 10}, Frame -> True, 
 FrameTicks -> {{0, {Pi, Pi, {.4, 0}, Directive[Blue, Thick]}, 2 Pi, 
    3 Pi}, None}, FrameStyle -> Directive[Gray, Dashed], 
 FrameTicksStyle -> Directive[Orange, 12]]

Mathematica graphics

which suggests, that for setting ticks on top and bottom equally, you just have to supply only 1 list. This can be generalized to when you give 2 lists and not {} explicitly for the 3rd and 4th list, you get ticks everywhere.

Et voila:

Show[p1, FrameTicks -> Take[FrameTicks /. AbsoluteOptions[p], 2]]

works as expected.

Conclusion here is, that you have all information about the ticks even if you don't use FrameTicks->All. If I'm right, and you needed the ticks only to adjust the label, than you can check the setting of FrameTicks with Options[gr,FrameTicks] and if it is set to All, you can build your own version with

Show[gr, FrameTicks -> Automatic]

and extracting and transforming the ticks of this.

Mathematica Graphics and PSFrag

Since we cleared in chat that the overall goal is to replace FrameTicks in an EPS graphics with the appropriate or custom LaTeX code I want to give an implementation which might be of use. The approach will work as follows:

  • I extract the FrameTicks from a given Mathematica Graphics.
  • I will create a labels for each tick which will be a string-replacements that appears in the eps file and can be found easily by PSFrag.
  • Therefore, the label must be unique and
  • should have about the same width as the original tick

Labels will consist of a unique id and, if necessary a padding with "x" to make it as large as the original tick. The calculation of the width will work as follows: (1) we estimate the width of an "x" and (2) create a Rasterized version of the tick to give its ImageDimensions in means of x-widths.

xwidth[] := xwidth[] = First[ImageDimensions[
      Rasterize@StringJoin@ConstantArray["x", 10]]]/10.;

SetAttributes[expressionWidth, {HoldFirst}];
expressionWidth[expr_] := With[ {
 x = xwidth[], 
 exprwidth = First[ImageDimensions[Rasterize[HoldForm[expr]]]]},
 Round[exprwidth/x]
]

Creating a new label is then (1) asking a global counter for a new id and create the label-string which is about the same width as the original tick. Minimum tick-width is 2 here

$label = 10;
getLabel[] := $label++;
SetAttributes[createLabel, {HoldFirst}];
createLabel[tick_] := 
 Module[{size = Max[2, expressionWidth[tick]], label},
  label = Which[size > 2,
    StringJoin[ConstantArray["x", size - 2]] <> 
     IntegerString[getLabel[]],
    True,
    IntegerString[getLabel[]]
    ]
  ]

The transformation of the Graphics itself is simple. I use a transformTick function which creates a new encoded tick and Sows the LaTeX code for PSFrag. In createPSFragGraphics I replace every FrameTick with the encoded ones and collect the LaTeX-snips with Reap

SetAttributes[transformTick, {HoldFirst}];
transformTick[t : {_, "", _List, _List}] := t;
transformTick[{pos_, tick_, length_List, spec_List}] :=

 With[{label = createLabel[tick]},
  Sow["\\psfrag{" <> label <> "}{$" <> 
    ToString[TeXForm[ToString[tick, TraditionalForm]]] <> "$}"];
  {pos, label, length, spec}
]

createPSFragGraphics[gr_] := Reap[Show[#, FrameTicks -> 
  Map[transformTick, FrameTicks /. AbsoluteOptions[#, FrameTicks], {2}]] &@gr];

Final note, since you may have seen all the HoldFirst attributes. I want to prevent ticks like those

Mathematica graphics

from evaluation into 3000000. It would give a wrong width otherwise.

Testing the label-encoding

gr = Plot[x^2, {x, 0, 1000}, Frame -> True];
{grenc, labels} = 
  createPSFragGraphics[Plot[x^2, {x, 0, 1000}, Frame -> True]];
grenc
Column[Flatten[labels]]

In grenc you find the encoded Graphics you want to export to EPS. In comparison with the original one, you see that label-lengths almost match with the tick-width

Mathematica graphics

Furthermore, you get the LaTeX code required for PSFrag in labels

\psfrag{10}{$0.$}
\psfrag{xx11}{$200.$}
\psfrag{xx12}{$400.$}
\psfrag{xx13}{$600.$}
\psfrag{xx14}{$800.$}
\psfrag{xxx15}{$1000.$}
\psfrag{16}{$0.$}
\psfrag{xxxxx17}{$200000.$}
\psfrag{xxxxx18}{$400000.$}
\psfrag{xxxxx19}{$600000.$}
\psfrag{xxxxx20}{$800000.$}
\psfrag{xxxx21}{$1.\times 10^6$}

Final note

I had to learn the hard way that Linux seems to export every string or number as a separate letter in an EPS file. This makes is completely unusable for PSFrag since this relies on the fact that the labels in EPS files are given as single show command. If you never looked at postscript-code, then the documentation to PSFrac explains this quite nice.