5
$\begingroup$

When I use MaTeX, I find that the displayed text is always centered with respect to the specified location. Adding TextAlignment -> Left seems to be ignored: Graphics[Text[MaTeX["a=b+c+d", Magnification -> 4], {0, 0}, TextAlignment -> Left]]

Is there a way to control where the string starts?

$\endgroup$
  • $\begingroup$ You should probably ping @Szabolcs, the creator of that package. $\endgroup$ – march Apr 20 '17 at 18:27
4
$\begingroup$

TextAlignment is not an option of Text. The standard way to align text when using Text as a Graphics primitive is to use the optional third argument which represents an offset. This works with MaTeX or just plain text.

Also, it would probably best to include some other objects in your image so that we can see what's going on.

Thus, maybe you want something like:

Graphics[{
 {LightGray, EdgeForm[Black], Disk[{0, 0}, 1]},
  Point[{0, 0}],
 Text[MaTeX["a=b+c+d", Magnification -> 2], {0, 0}, {1, 0}]
}]

enter image description here

Update: Mathematica 11.1 and 11.2 are affected by a bug where Text offsetting does not work correctly. To work around the problem, use

Text[MaTeX["a=b+c+d", Magnification -> 2], {0, 0}, ImageScaled[{1, 1/2}]]

instead of

Text[MaTeX["a=b+c+d", Magnification -> 2], {0, 0}, {1, 0}]
$\endgroup$
  • $\begingroup$ Thank you, This did it. So MaTeX produces an object without any awareness of its position and Graphics lays it out? $\endgroup$ – Wynne Apr 24 '17 at 1:08
  • $\begingroup$ What's the default value for the offset? It seems to change the behavior qualitatively: rather than putting the center of an object at a particular point, it puts the bottom left corner. $\endgroup$ – Wynne Apr 24 '17 at 13:09
  • $\begingroup$ @Lem.ma Mathematica 11.1 and 11.2 are affected by a bug where Text positioning does not work correctly. To work around this problem, use ImageScaled[{1,1/2}] instead of {1,0} in the third argument of Text. $\endgroup$ – Szabolcs Sep 19 '17 at 20:35
1
$\begingroup$

tl;dr: I would use Inset instead.

The output of MaTeX` is a graphic, not text:

MaTeX["a=b+c+d", Magnification -> 2] // FullForm // Shallow 

Graphics[List[Skeleton[2]],List[Skeleton[6]]]

The thing about Text is that it turns ordinary expressions into expressions suitable for display. For example, it turns x^2 into $x^2$. Since the output of MaTeX` is a graphic, Text is not the most natural choice here. I would instead go with Inset:

Graphics[{
  {LightGray, EdgeForm[Black], Disk[{0, 0}, 1]},
  Inset[MaTeX["a=b+c+d", Magnification -> 2], {-0.5, 0.2}]
  }]

Mathematica graphics

Note that Inset has an additional argument called opos which can be used to determine the relationship between the text and the given position. To simulate left justification, one might set it to {Left, Center}.

$\endgroup$
  • 1
    $\begingroup$ I think Inset and Text are almost interchangeable. Can you see any difference between them when using only 1-3 arguments? They don't only hold text/graphics, they (both) hold anything that can appear inline in a notebook: text, graphics, 2D math, GUI elements, grids, tables, etc. AFAIK most things that appear in a notebook have a BaselinePosition option, which allows them to flow well together with text (MaTeX tries to set the baseline appropriately, but unfortunately the result does not always look great because the front end rounds to integer pixels) $\endgroup$ – Szabolcs Apr 20 '17 at 19:39
  • $\begingroup$ @Szabolcs I agree with you, but I think it is misleading to apply Text to something which is not text. If you read the code quickly, it might look as though Text is rendering the expression (generating the display form.) In fact, it is MaTeX which does that. Better then to use a head which does not affect the display form, so that readers who skim the code (or do not know what MaTeX is) cannot make that mistake. In fact, I'm not sure whether OP chose Text because he knew that Text and Inset are the same in this case, or because he thought he was dealing with text. $\endgroup$ – C. E. Apr 20 '17 at 20:53
  • $\begingroup$ This response helped me understand that the output of MaTeX is an object suitable to display, but it doesn't solve the problem with not being able to control where the text starts. $\endgroup$ – Wynne Apr 24 '17 at 0:48
  • $\begingroup$ @Lem.ma Maybe you do not properly understand how to use the opos argument of Inset. For left justification it should be set to {Left, Center}. Tell me if this doesn't solve your problem. $\endgroup$ – C. E. Apr 24 '17 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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