On printing expressions using TexAct

504 views
Skip to first unread message

user657

unread,
Aug 7, 2018, 6:07:12 PM8/7/18
to xAct Tensor Computer Algebra
Dear all,

I have a rather simple question regarding printing expressions in xAct, using TexAct.

Suppose the manifold and metric have been defined as usual. Now an expression like RicciCD[a, b] prints as output the Ricci scalar with a covariant derivative operator inside square brackets. Implementing TexPrint[RicciCD[a, b]] gives "R[\\triangledown ]^{ab}". Note that I automatically get the double quotation marks in the last expression after copying/pasting the output cell.

Question: I want a (TeX) expression for the Ricci scalar as it is written in general usage. That is, I want to obtain R^{ab} from RicciCD[a, b]. How can I do this?

Comments: Getting the appropriate TeX expression entails, in  "R[\\triangledown ]^{ab}", getting rid of the double quotation marks, the double (instead of single) slash prefixed to triangledown, and the square brackets. Also note that, when implemented in LaTeX, \triangledown shows a (slightly) different output as compared to \nabla. I use the latter, not \triangledown, when writing covariant derivatives in my LaTeX file. My example of RicciCD[a, b] is just for illustrating a simple case where I run into problems trying to print it in the desired format; I ultimately wish to be able to write all curvatures (Ricci scalar/tensor and Riemann/Weyl tensor) in the standard format, and also have \triangledown replaced by \nabla whenever free covariant derivatives appear in any other complicated expression.

Thank you for your help.

Thomas Bäckdahl

unread,
Aug 8, 2018, 2:28:32 AM8/8/18
to xa...@googlegroups.com
Hi!

See my attached notebook. I hope this solves your problem and explains how TexAct can be used.

Regards
Thomas
--
You received this message because you are subscribed to the Google Groups "xAct Tensor Computer Algebra" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xact+uns...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

TexExample1.nb

user657

unread,
Aug 9, 2018, 2:53:58 AM8/9/18
to xAct Tensor Computer Algebra
Dear Thomas,

Thank you very much for your quick and comprehensive answer. It definitely solved my problem, and very easily, at that. Thank you for your (and others') great work on TexAct/xAct.

user657

unread,
Nov 12, 2018, 5:37:35 PM11/12/18
to xAct Tensor Computer Algebra
Dear Thomas,

Thanks for your example again. I have a question about the notebook you shared. I get an error when I implement TexPrintAlignedEquations@longequation. I get the following messages:

DeleteFile: Directory or file TexActWidthTest.log not found.
Tex Error: could not delete temporary files
Tex Error:
Throw: Uncaught Throw[Tex Error] returned to top level.

Hold[Throw[Tex Error]]

I'm not sure why I get these errors. Could you point out what I'd need to do to rectify it?

Thank you for your help.

On Wednesday, August 8, 2018 at 8:28:32 AM UTC+2, TB wrote:

Thomas Bäckdahl

unread,
Nov 13, 2018, 12:48:20 AM11/13/18
to xa...@googlegroups.com
Hi!

Are you sure that you are using the latest version 0.4.1, {2018,2,28} of TexAct?

I think the problem is that it can not find your pdflatex executable to compile it.
The new version is better at finding it, otherwise you can tell it where it is by something like
$LatexExecutable="pathtoyourversionoflatex/pdflatex -interaction nonstopmode -halt-on-error"

Regards
Thomas

user657

unread,
Nov 13, 2018, 1:18:27 PM11/13/18
to xAct Tensor Computer Algebra
Dear Thomas,

Yes! It was not updated. Thank you for pointing that out. It works now.

Shubham

unread,
Dec 10, 2018, 6:58:24 PM12/10/18
to xAct Tensor Computer Algebra
Hi Thomas,

I have a question off the notebook you sent me. The command TexPrintAlignedEquations is quite nice, however, I am unable to customize it. For instance, I have expressions with many terms that cover many lines in pdf. Is there a way I can tell TexAct to leave a linebreak after, say, 4 terms? Also, is it possible to tell TexAct to cover a certain given length of horizontal space when LaTeXing each line of a big expression, before providing a linebreak and then starting over from the next line? I want to be able to have some control over the linebreak \\ and ampersand &.

Thanks again,
Shubham 


On Wednesday, August 8, 2018 at 8:28:32 AM UTC+2, TB wrote:

Thomas Bäckdahl

unread,
Dec 11, 2018, 1:07:51 PM12/11/18
to xa...@googlegroups.com
Hi!

TexPrintAlignedEquations will linebreak so that every term will fit within the width of the paper when passed through LaTeX.
Of course how wide that is depends on your document style etc.
You can set the document style to whatever you want in
$TexInitLatexClassCode
Specify the packages you want to use in
$TexInitLatexPackages
and specify extra initialization code in
$TexInitLatexExtraCode

If you want to specify the maximal width you can do it in
$TexPrintPageWidth
There the symbol latextextwidth means the actual textwidth of your document.

If you want to break by number of characters or number of terms you can use The command TexBreak. It will insert linebreaks in a string containing tex code.
For instance
TexBreak[TexPrint@longequation, 4, TexBreakBy -> "Term"]

Also look at Options[TexBreak] for extra options. You can for instance change TexBreakString to insert something else for each line break.
Unfortunately, I have not implemented many options for the TexPrintAlignedEquations function, and the source code for that particular function is perhaps a bit complicated.

Hopefully the things above should give you ideas on how to customize TexPrintAlignedEquations enough. If that is not enough you can use TexPrint together with TexBreak, or alternatively copy and modify TexPrintAlignedExpressions (which is simpler than TexPrintAlignedEquations).

Please let me know if you want some very specific option implemented in TexPrintAlignedEquations.

I hope this helps.

Regards
Thomas

Shubham

unread,
Dec 11, 2018, 7:33:53 PM12/11/18
to xAct Tensor Computer Algebra
Hi Thomas,

Thank you very much for your detailed reply. Hopefully this will be enough. In the meantime, I realized where exactly I was having a problem. Suppose I have a big expression expr with a parameter "g". After using ToCanonical, ContractMetric and Simplify, I want to Collect all the terms in the expression like Collect[expr,g]. If I use TexPrintAlignedEquations on this, the expression does not undergo a linebreak and flows out of the page. If I do Expand[expr] so that all terms are now spread out, TexPrintAlignedEquations then works just fine. Using Collect seems to spoil what TexPrintAlignedEquations is supposed to do. Could you please tell me what I could do to correct this?

Thank you again

Thomas Bäckdahl

unread,
Dec 12, 2018, 1:46:58 AM12/12/18
to xa...@googlegroups.com
Hi!

TexBreak and therefore also TexPrintAlignedEquations are by default set to not linebreak inside a parenthesis. This can be changed with
SetOptions[TexBreak, TexBreakInParenthesis -> True]

This affects both TexPrintAlignedEquations and TexBreak.

The option TexBreakInParenthesis -> False is very useful when you have many terms with factors like (r^2+a^2) or other short expressions you don't want broken up.

Regards
Thomas

Guillaume Faye

unread,
Dec 12, 2018, 4:59:47 AM12/12/18
to Thomas Bäckdahl, xa...@googlegroups.com

Dear Thomas,

>
> TexBreak and therefore also TexPrintAlignedEquations are by default set to
> not linebreak inside a parenthesis. This can be changed with
> SetOptions[TexBreak, TexBreakInParenthesis -> True]
>
> This affects both TexPrintAlignedEquations and TexBreak.
>
> The option TexBreakInParenthesis -> False is very useful when you have many
> terms with factors like (r^2+a^2) or other short expressions you don't want
> broken up.

I enjoy very much the new management of parenthesis in TexAct v0.4.1 (in
Mathematica 11.0 for Linux in my case). However, as I was playing with it last
week, I noticed (through the command TexPrintAlignedEquations) that
TexBreakInParenthesis -> True does not seem to work well anymore when the
parenthesis level exceeds 4. I didn't have time to explore this so it is quite
possible that the problem comes actually from me but I prefer to tell you
anyway. Of course, quick tricks can be used to avoid the problem, e,g.,

-----------------
OpenParenthesis[4, "("] := "\\text{o4}";
CloseParenthesis[4, ")"] := "\\text{c4}";
OpenParenthesis[5, "("] := "\\text{o5}";
CloseParenthesis[5, ")"] := "\\text{c5}"; (* etc. *)

TexPrintAlignedEquationsFix[expr_] :=
StringReplace[TexPrintAlignedEquations[expr], ruleparen];

ruleparen =
MapThread[
Rule, {{"\\text{o4}", "\\text{c4}", "\\text{o5}", "\\text{c5}" (* etc. *)},
{"\\Biggl\\{", "\\Biggr\\}",
"\\Biggl[", "\\Biggr]" (* etc. *)}];
------------------


Yours,
Guillaume

Thomas Bäckdahl

unread,
Dec 12, 2018, 5:34:14 AM12/12/18
to Guillaume Faye, xAct Tensor Computer Algebra
Hi Guillaume,

Good catch, I had not thought about this possible issue.
The origin of the problem is that up to parenthesis level 4, the code looks a bit like
OpenParenthesis[4, str_] := "\\Biggl" <> str;
CloseParenthesis[4, str_] := "\\Biggr" <> str;

Latex accepts line breaks between \Biggl( and \Biggr) etc, so that allows  TexBreakInParenthesis -> True.

However, the default for parenthesis level 5 and up is given by the code
OpenParenthesis[level_Integer?Positive, str_] := "\\left" <> str;
CloseParenthesis[level_Integer?Positive, str_] := "\\right" <> str;

Latex does not allow line breaks between \left( and \right), so TexBreakInParenthesis -> True will not work
 with this code.
The simplest workaround should be to write something like
OpenParenthesis[level_Integer?Positive, str_] := "\\Biggl" <> str;
CloseParenthesis[level_Integer?Positive, str_] := "\\Biggr" <> str;
or add special cases up to the level you need.

I don't see the reason for your \text{o4} etc. A direct specification like
OpenParenthesis[5, "("] := "\\Biggl[";
CloseParenthesis[5, ")"] := "\\Biggr]";
should work fine.

\left and \right is of course also possible to use with linebreaking, but then one would need to add the correct number of \right. before the linebreak and enough \left. after the linebreak.
It would require some effort to write a code that could do that though.

Regards
Thomas

Guillaume Faye

unread,
Dec 12, 2018, 5:59:30 AM12/12/18
to Thomas Bäckdahl, xAct Tensor Computer Algebra

Dear Thomas,

Thanks for your quick reply.

>
> I don't see the reason for your \text{o4} etc. A direct specification like
> OpenParenthesis[5, "("] := "\\Biggl[";
> CloseParenthesis[5, ")"] := "\\Biggr]";

I tried that first, of course. But it didn't work!
>
> \left and \right is of course also possible to use with linebreaking, but
> then one would need to add the correct number of \right. before the
> linebreak and enough \left. after the linebreak.
> It would require some effort to write a code that could do that though.

Yes, I know... Your current suggestion is excellent, though. I'll do that from
now on.

Yours,
Guillaume

Guillaume Faye

unread,
Oct 16, 2019, 3:30:36 PM10/16/19
to Thomas Bäckdahl, xa...@googlegroups.com

Dear xAct and TexAct users,

> On 2018-11-12 23:25, user657 wrote:
> Dear Thomas,
> Thanks for your example again. I have a question about the notebook
> you shared. I get an error when I
> implement TexPrintAlignedEquations@longequation. I get the following
> messages:
>
> DeleteFile: Directory or file TexActWidthTest.log not found.
> Tex Error: could not delete temporary files
> Tex Error:
> Throw: Uncaught Throw[Tex Error] returned to top level.
>
> Hold[Throw[Tex Error]]
>
> I'm not sure why I get these errors. Could you point out what I'd need
> to do to rectify it?
>
> Thank you for your help.

I recently faced the same problem as described in the above message with
Mathematica11.0.1.0 under Ubuntu 18.04.3. It quickly appeared that, in my
case, it was not due to a failure of TexAct in identifying the correct pdflatex
executable. Instead, it was related to a version
incompatibility between the libz library (libz.so.1) used by default in
Mathematica and the libpng system library (here libpng16.so.16), preventing
pdflatex to work properly. The simplest solution I thought of, i.e. upgrading
Mathematica to version 12, having been helpless, I had to force Mathematica to
resort to the standard system libraries whenever calling pdflatex. This can be
achieved by changing locally the environment variable LD_LIBRARY_PATH.

More precisely, here are the modifications I suggest:

In TexView:

Module[{TexOut, errorpos, templdlib = Environment["LD_LIBRARY_PATH"]},

[...]

TexOut = ReadList[
StringJoin["!" <> $LatexExecutable <> " ", file, ".tex"], String];
If[Length@TexOut == 0, SetEnvironment["LD_LIBRARY_PATH" -> ""];
TexOut = ReadList[
StringJoin["!" <> $LatexExecutable <> " ", file, ".tex"], String];
SetEnvironment["LD_LIBRARY_PATH" -> templdlib]];
If[Length@TexOut == 0,
"Could not find Latex. Check if $LatexExecutable contains the correct name and path."];

[...]

In TexWidths:

Module[{TexOut, errorpos, templdlib = Environment["LD_LIBRARY_PATH"]},

[...]

TexOut = ReadList["!" <> $LatexExecutable <> " TexActWidthTest.tex",
String];
If[Length@TexOut == 0, SetEnvironment["LD_LIBRARY_PATH" -> ""];
TexOut = ReadList["!" <> $LatexExecutable <> " TexActWidthTest.tex",
String];
SetEnvironment[
"LD_LIBRARY_PATH" ->
templdlib]];
If[
Length@TexOut ==
0, "Could not find Latex. Check if $LatexExecutable contains the correct name and path."];

[...]

I did not check whether similar corrections had to be applied in other places,
but at least TexView becomes fully operational after the previous patches.

Hoping that those considerations may be useful for some of you,
Yours,
Guillaume

Thomas Bäckdahl

unread,
Oct 19, 2019, 3:50:38 PM10/19/19
to Guillaume Faye, xa...@googlegroups.com
Hi!

Thanks for tracking down what the problem really was for you. I would
not have guessed this. Is this a problem for all Ubuntu user, did it
only happen on your machine?

The only linux computer I have right now is a raspberry pi 4, and I
don't get this problem there.
However, the expression
$LatexExecutable=StringJoin[First@latexlist," -interaction nonstopmode
-halt-on-error"
needed to be changed to
$LatexExecutable=StringJoin[Last@latexlist," -interaction nonstopmode
-halt-on-error"
because the shell printed out some other message on the first line.
Due to a bug in the Mathematica compiler add-on "mcc" for raspberry pi
4, the compilation of xperm.linux.32-bit became a bit more complicated
though. The compiler flag -m32 needed to be removed from mcc for the
compilation to work. Please tell me if you want to know how to do this.

I will have to think a bit about how our changes should be implemented
so it would not interfere with other operative systems.

Regards
Thomas

Guillaume Faye

unread,
Oct 21, 2019, 12:17:27 PM10/21/19
to Thomas Bäckdahl, xa...@googlegroups.com
Dear Thomas,

>
> Thanks for tracking down what the problem really was for you. I would not
> have guessed this. Is this a problem for all Ubuntu user, did it only happen
> on your machine?

The problem showed up both on my desktop computer and my laptop, which turn
out to be equipped with the same version of Ubuntu (18.04) but two different
versions of Mathematica (11.0 and 12.0). After some experiments made by one of
our system administrator, it appears that the issue is likely to be user
independent.

> The only linux computer I have right now is a raspberry pi 4, and I don't get
> this problem there.
> However, the expression
> $LatexExecutable=StringJoin[First@latexlist," -interaction nonstopmode
> -halt-on-error"
> needed to be changed to
> $LatexExecutable=StringJoin[Last@latexlist," -interaction nonstopmode
> -halt-on-error"
> because the shell printed out some other message on the first line.
> Due to a bug in the Mathematica compiler add-on "mcc" for raspberry pi 4, the
> compilation of xperm.linux.32-bit became a bit more complicated though. The
> compiler flag -m32 needed to be removed from mcc for the compilation to work.
> Please tell me if you want to know how to do this.

I don't really need such a fix right now, but, as a lambda user potentially
subject to this type of problems, I would be interested to learn about the
solution you have found...

> I will have to think a bit about how our changes should be implemented so it
> would not interfere with other operative systems.

That would be great...

Yours,
Guillaume
Reply all
Reply to author
Forward
0 new messages