Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Mathematica and LyX - Graphics and equations

1,061 views
Skip to first unread message

TL

unread,
Jan 25, 2009, 6:53:38 AM1/25/09
to
I unintentionally started a fierce discussion recently about the "best"
editor for math stuff. Many people responded with a number of
suggestions and among them one particular solution seemed to prevail -
LaTeX. I decided to give it a try and installed the MiKTeX Win version
along with the LyX fronted for it, since I have absolutely no experience
with LaTeX.
It's almost a week now that I'm struggling to produce anything usable
with LyX and I have to admit I have mixed feelings about it. It's really
easy to produce good quality documents with the default
settings/templates. When something however doesn't look right I feel
like my hands are tied together - usually there's no user control over
it. I'm still not giving it up though and I'd like to ask a couple of
questions about the Mathematica/LyX interaction.

1) What is the best way to export graphics from Mathematica? I currently
use eps format, but there seems to be a weird bug with it - I'm not sure
whether it's a Mathematica or LyX or LaTeX issue - for example I have 2
vectors in my graphic and the head of the last one drawn is not
displayed in the final PDF version of my document. I found a workaround
for it by drawing objects twice in the graphic that way if some parts of
the object is missing it wouldn't matter since the one underneath will
be fine.

2) How do I export the equations from Mathematica? Even though there is
an Export to LaTeX option in Mathematica I couldn't succeed in just
pasting the content in LyX - so much junk comes along with the equation
that cleaning it up usually takes more time than just typing it fresh in LyX

3) And finally could someone please provide detailed instructions of how
to setup LyX to interact directly with the Mathematica kernel so I can
have some semi live math environment. May be that way the equations
will come in the right form

Thanks once again to all of you who took their time to reply
TL

Bill Rowe

unread,
Jan 25, 2009, 9:49:26 PM1/25/09
to
On 1/25/09 at 6:53 AM, la...@shaw.ca (TL) wrote:

>I decided to give it a try and installed the MiKTeX Win version along
>with the LyX fronted for it, since I have absolutely no experience with
>LaTeX.

But LyX really isn't a front end for LaTeX. LyX is a document
processor that can both import and export LaTeX. That is, you
don't create a document in LyX by entering LaTeX code into the
window opens when you select File->New.

>It's almost a week now that I'm struggling to produce anything usable
>with LyX and I have to admit I have mixed feelings about it. It's
>really easy to produce good quality documents with the default
>settings/templates. When something however doesn't look right I feel
>like my hands are tied together - usually there's no user control over
>it. I'm still not giving it up though and I'd like to ask a couple of
>questions about the Mathematica/LyX interaction.

I can answer some of your questions from the perspective of
LaTeX which I use but not LyX.

>1) What is the best way to export graphics from Mathematica?

I generally use PDF. But I've obtained good results with both
EPS and PDF

>2) How do I export the equations from Mathematica?

I generally use TeXForm and simply copy and paste the result
into the document I am working on. For example doing:

In[3]:= f == Sum[Subscript[x, n], {n, k}] // TeXForm

Out[3]//TeXForm=
f=\sum _n^k x_n

gives a result that can be pasted directly into LaTeX document.

>3) And finally could someone please provide detailed instructions of
>how to setup LyX to interact directly with the Mathematica kernel so
>I can have some semi live math environment. May be that way the
>equations will come in the right form

Given what LyX is, I doubt this is easily doable if at all.


JUN

unread,
Jan 26, 2009, 5:01:35 AM1/26/09
to

> >3) And finally could someone please provide detailed instructions of
> >how to setup LyX to interact directly with the Mathematica kernel so
> >I can have some semi live math environment. May be that way the
> >equations will come in the right form
>
> Given what LyX is, I doubt this is easily doable if at all.

The last remark needs fact-checking. It actually works - within
limits. It's a fun feature, but I don't really use it much.

What you have to do to make it work depends on your platform. On Mac
OS X, I first created a little script
/usr/local/bin/math
containing the following two lines:
#!/bin/sh
/Applications/Mathematica.app/Contents/MacOS/MathKernel "$@"

The purpose of this is simply to make the command "math" appear in the
path visible to LyX, and point to the Mathematica Kernel (so obviously
this is Mac specific, and I don't know the PC equivalent). Then
whenever you have a calculation that you want to do in LyX, do the
following:
* Open a math environment.
* Type the Mathematica expression you want to evaluate (e.g.: Sin[.2])
* Highlight the expression
* While you are still in that math box, go to Edit > Math > Use
Computer Algebra System > Mathematica
* Magically, an equals sign and the result of the evaluation will
appear in the LyX math box.

In general, in particular when graphics is involved, I would just work
with the Mathematica Notebook and the LyX editor side by side, and
copy/paste between the two. In LyX 1.6, you can paste graphics into
LyX from the clipboard. Equations in Mathematica's TeXForm can be
pasted directly into LyX math environments.

In the other direction, you can copy LaTeX from LyX into Mathematica
as follows:
* enter the desired math expression in LyX (e.g., \sin\alpha)
* select all the _contents_ of the math box, but not the math box
object itself (i.e., stop highlighting when you reach the end of the
math inset).
* Copy
* Paste literally into a Mathematica Cell, perhaps wrapped by
something like ToExpression["\sin\alpha", TeXForm] (where the pasted
text goes between the quotation marks)
* The result can then be used in Mathematica Labels etc.

Although LyX has a non-LaTeX internal file format, everything it does
goes through LaTeX, and in particular all its equations can be
understood by other LaTeX-aware programs. One may still ask why anyone
would you use LyX instead of Mathematica to write equations. I think
this has already been addressed, and it may really be a matter of
taste - but it's worth pointing out that LyX has unlimited Undo and
version control (there's more, but I better stop here).

As for the customization of the LyX output, it's true that this isn't
trivial if you don't know much LaTeX. The LyX-users mailing list would
be a place to start, they are very helpful...

Jens

Curtis Osterhoudt

unread,
Jan 27, 2009, 6:58:55 AM1/27/09
to
On Sunday 25 January 2009 04:53:43 TL wrote:

>
> 1) What is the best way to export graphics from Mathematica? I currently
> use eps format, but there seems to be a weird bug with it - I'm not sure
> whether it's a Mathematica or LyX or LaTeX issue - for example I have 2
> vectors in my graphic and the head of the last one drawn is not
> displayed in the final PDF version of my document. I found a workaround
> for it by drawing objects twice in the graphic that way if some parts of
> the object is missing it wouldn't matter since the one underneath will
> be fine.

[...]

> Thanks once again to all of you who took their time to reply
> TL
>
>

I don't see that you mention which OS you use Mathematica and LyX on; the following is what I use on linux and Windows, though it may work just as well on OS X.

I usually produce my graphics in Mathematica, and then use XFig to edit them (people familiar with XFig will probably shake their heads and shrug a little, as it's sometimes a beastly little program to use -- *but* it is the only one I have found which can easily do what I want to do). My steps are:

1. Produce the graphic in Mathematica.
2. Export said graphic (usually as .pdf).
3. using linux, run "pstoedit -f xfig the_exported_graphic.pdf the_new_graphic.fig" (this converts the .pdf into the .fig format). I think equivalent programs exist for Windows, but I haven't figured it out yet.
4. Edit as necessary, the .fig file using XFig (or JFig in Windows). XFig has the nice property that it can embed _proper_ LaTeX code into the figure, which eventually gets typeset by LyX. This means that all the fonts and so forth will look _right_ in the final document.
5. Include the .fig file in the LyX document ( Insert -> File -> External Material, then under "Template" choose xfig ). LyX should show a proper graphics preview, with a fully typeset figure.

It's an involved process, though I'm very used to it now. I even have a Mathematica package which will take an XFig file, change axes to proper weights, change the fonts the way I like them, and so on.

Best of luck. I am quite happy with Mathematica, I love LyX, and XFig has met my needs heretofore. Hopefully something analogous will obtain for you.

C.O.

--
==========================================================
Curtis Osterhoudt
cfo@remove_this.lanl.and_this.gov
PGP Key ID: 0x4DCA2A10
Please avoid sending me Word or PowerPoint attachments
See http://www.gnu.org/philosophy/no-word-attachments.html
==========================================================

Jens Noeckel

unread,
Jan 27, 2009, 7:00:05 AM1/27/09
to
Hi,
if I understand you correctly, the \text{ } is the (correct)
response by Mathematica to your entering single-letter variabes
without spaces between them. I.e., try
TeXForm[ a b ]
versus
TeXForm[ ab ]

So you just have to make sure you write multiplications of variables
with spaces in between. If you're referring to something more
complicated, an example would help.

Jens


On Jan 26, 2009, at 8:26 AM, TL wrote:

> That's what I was looking for, thanks a lot JUN!
>
> I have a WinXP environment here and I'm not sure your instructions
> will work but at least that's an excellent starting point.
> As for pasting content from Mathematica I noticed one really
> annoying fact, that's actually mentioned in the Mathematica help as
> well - when there are two or more symbol variables in your
> equations Mathematica treats them as text and displays them as
> such, the result being that part of the equation appears in italic
> font part in regular. I think that was the root of my issues in
> some of my previous posts and now it's haunting me in other
> environments too, cause the LaTeX form of the equations that is
> exported by Mathematica contains a whole bunch of text{} tags that
> I have to manually get rid of
>
> Does someone know how to change that behaviour?


>
> JUN wrote:
>>>> 3) And finally could someone please provide detailed
>>>> instructions of
>>>> how to setup LyX to interact directly with the Mathematica
>>>> kernel so
>>>> I can have some semi live math environment. May be that way the
>>>> equations will come in the right form
>>>>
>>> Given what LyX is, I doubt this is easily doable if at all.
>>>
>>

TL

unread,
Jan 27, 2009, 7:01:11 AM1/27/09
to
That's what I was looking for, thanks a lot JUN!

I have a WinXP environment here and I'm not sure your instructions will
work but at least that's an excellent starting point.
As for pasting content from Mathematica I noticed one really annoying
fact, that's actually mentioned in the Mathematica help as well - when
there are two or more symbol variables in your equations Mathematica
treats them as text and displays them as such, the result being that
part of the equation appears in italic font part in regular. I think
that was the root of my issues in some of my previous posts and now it's
haunting me in other environments too, cause the LaTeX form of the
equations that is exported by Mathematica contains a whole bunch of
text{} tags that I have to manually get rid of

Does someone know how to change that behaviour?

JUN wrote:
>>> 3) And finally could someone please provide detailed instructions of
>>> how to setup LyX to interact directly with the Mathematica kernel so
>>> I can have some semi live math environment. May be that way the
>>> equations will come in the right form
>>>
>> Given what LyX is, I doubt this is easily doable if at all.
>>
>

> The last remark needs fact-checking. It actually works - within
> limits. It's a fun feature, but I don't really use it much.
>
> What you have to do to make it work depends on your platform. On Mac
> OS X, I first created a little script
> /usr/local/bin/math
> containing the following two lines:
> #!/bin/sh
> /Applications/Mathematica.app/Contents/MacOS/MathKernel "$@"
>
> The purpose of this is simply to make the command "math" appear in the
> path visible to LyX, and point to the Mathematica Kernel (so obviously
> this is Mac specific, and I don't know the PC equivalent). Then
> whenever you have a calculation that you want to do in LyX, do the
> following:
> * Open a math environment.

> * Type the Mathematica expression you want to evaluate (e.g.: Sin[.2])

TL

unread,
Jan 28, 2009, 6:33:36 AM1/28/09
to
Sorry I didn;t write an example

Here's the issue - I have variables such as P_AB - that is P and AB as
an index. Mathematica writes \text{} whenever it sees the AB which is
unwanted - I want the whole thing to appear in italic form. May be it's
a standard convention I'm not really sure, but it just looks ugly and I
looked in some math books and saw it all written in italic font there.
So is there a way to turn this off?


Jens Noeckel wrote:
> Hi,
> if I understand you correctly, the \text{ } is the (correct)
> response by Mathematica to your entering single-letter variabes
> without spaces between them. I.e., try
> TeXForm[ a b ]
> versus
> TeXForm[ ab ]
>
> So you just have to make sure you write multiplications of variables
> with spaces in between. If you're referring to something more
> complicated, an example would help.
>
> Jens
>
>
> On Jan 26, 2009, at 8:26 AM, TL wrote:
>

>> That's what I was looking for, thanks a lot JUN!
>>
>> I have a WinXP environment here and I'm not sure your instructions
>> will work but at least that's an excellent starting point.
>> As for pasting content from Mathematica I noticed one really annoying
>> fact, that's actually mentioned in the Mathematica help as well -
>> when there are two or more symbol variables in your equations
>> Mathematica treats them as text and displays them as such, the result
>> being that part of the equation appears in italic font part in
>> regular. I think that was the root of my issues in some of my
>> previous posts and now it's haunting me in other environments too,
>> cause the LaTeX form of the equations that is exported by Mathematica
>> contains a whole bunch of text{} tags that I have to manually get rid of
>>
>> Does someone know how to change that behaviour?
>>
>> JUN wrote:

>>>>> 3) And finally could someone please provide detailed instructions of
>>>>> how to setup LyX to interact directly with the Mathematica kernel so
>>>>> I can have some semi live math environment. May be that way the
>>>>> equations will come in the right form
>>>>>
>>>> Given what LyX is, I doubt this is easily doable if at all.
>>>>
>>>

JUN

unread,
Feb 1, 2009, 4:40:49 AM2/1/09
to
On Jan 28, 3:33 am, TL <la...@shaw.ca> wrote:
> Sorry I didn;t write an example
>
> Here's the issue - I have variables such as P_AB - that is P and AB as
> an index. Mathematica writes \text{} whenever it sees the AB which is
> unwanted - I want the whole thing to appear in italic form. May be it's
> a standard convention I'm not really sure, but it just looks ugly and I
> looked in some math books and saw it all written in italic font there.
> So is there a way to turn this off?

Actually Mathematica is doing the stylistically correct thing here
because AB is a single name according to what you describe. In LaTeX,
authors sometimes get lazy and type names in math environments without
surrounding them by \text or similar things, but then LaTeX not only
sets them in italics but can get fooled into typesetting them the
wrong way because it doesn't know all characters belong to one name.

So you should in fact declare textual names as such. The best approach
depends a lot on what you're actually trying to do with your
expression in Mathematica itself (manipulate it algebraically, use it
as a label, etc.). To "emulate" the lazy italic LaTeX text, you could
mislead Mathematica with the same thing that fools LaTeX: TeXForm
[Subscript[p, A B]].

Of course if you actually meant for A and B to be separate variables,
then you might also want to consider using Subscript[p, A, B] to avoid
any ambiguities.

0 new messages