Well, if @kglr doesn't know a direct way to make Text[]
/Inset[]
work, I guess there isn't one. First, the 4th argument to Text[]
makes the use of Rotate[]
unnecessary. Second, the 4th argument doesn't work any better than Rotate[]
. Third, the angle depends on both the PlotRange
and the AspectRatio
, so the solution in the comments and @anon's answer work only if the PlotRange
has equal horizontal and vertical lengths.
For decades I did this as a two step process (or forced the graphics to work with AspectRatio -> Automatic
). But @b3m2a1's function getBoxSize[]
allows us to combine the steps into a single function. The poorly named withAspectRatio[]
calculates the correction factor (also incorrectly called $aspectRatio
) and stores it in a global variable, which may appear in the Graphics[..]
code; the graphics will be returned with the correction factor replacing the global variable. The user may use a different variable name and pass this as the second argument to withAspectRatio[]
.
(* @b2m2a1, https://mathematica.stackexchange.com/a/166772/46569 *)
getBoxSize[c_Cell] := {#[[1]], Total@#[[2 ;;]]} &@
First@FrontEndExecute@GetBoundingBoxSizePacket[c];
getBoxSize[c_CellObject] := getBoxSize[NotebookRead[c]];
getBoxSize[b_BoxObject] :=
getBoxSize[
Cell[BoxData@NotebookRead[b], "Output", PageWidth -> Infinity,
ShowCellBracket -> False, CellMargins -> {{0, 0}, {0, 0}}]];
getBoxSize[e_] :=
getBoxSize[
Cell[BoxData@ToBoxes[e], "Output", PageWidth -> Infinity,
ShowCellBracket -> False, CellMargins -> {{0, 0}, {0, 0}}]];
withAspectRatio // ClearAll;
withAspectRatio // Attributes = {HoldRest};
withAspectRatio[g_, a_Symbol : $aspectRatio] := Block[{a},
a = 1;
With[{boxes =
Show[g, Frame -> None, Ticks -> None, ImagePadding -> 0] //
MakeBoxes[#, StandardForm] &},
a = First@
Ratios[getBoxSize@Cell[BoxData@boxes, "Output"]/
Flatten[Differences /@ PlotRange@g]];
g
]];
Manipulate[
withAspectRatio[
Plot[{Exp[x], Exp[a] (x - a + 1)}
, {x, a - 1, a + 1}
, Epilog -> {
Text[
"my tangent line is parallel to this",
{a, Exp[a]}, {0, 2}, {1, $aspectRatio * Exp[a]}]}
, AspectRatio -> 2.3 + a]
]
, {{a, 0}, -2, 1}]
Also for decades, I became used to this and assumed that the 4th argument was supposed to behave this way. This question made the scales fall, and I don't see why this behavior is at all convenient. Furthermore I think the community should consider it a bug and bug WRI to fix it.
The OP's code refactored:
Manipulate[
With[{f = x |-> (x + 0.4)^2, x0 = 1, x1 = 2},
Module[{y0 = f[x0], y1 = f[x1]},
withAspectRatio[
Graphics[{Line[{{x0, y0}, {x1, y1}}],
Text[
"test test test", {(x0 + x1)/2, (y0 + y1)/2}, {0, -2}, {x1 -
x0, $aspectRatio (y1 - y0)}]}
, PlotRange -> {{0, 6}, {0, 6}}, Axes -> True, AspectRatio -> a,
AxesOrigin -> {0, 0}]
]
]]
, {{a, 1}, 1/2, 2}]
Rotate[Text["test", {(x0 + x1)/2, (y0 + y1)/2}, {0, -2}], ArcTan[1, (y1 - y0)/(x1 - x0)]]
orText[Rotate["test", ArcTan[1, a (y1 - y0)/(x1 - x0)]], {(x0 + x1)/2, (y0 + y1)/ 2}, {0, -2 a}]
? $\endgroup$