Equations and Mathematics

Okay, I’m squarely on the academic/scientific writing side of the spectrum. I’m wondering if there is support for equations or mathematics, or how people get around the limitation if there isn’t any such support.

Thanks!

P.S. I am squarely in the noob catagory, so be overly detailed in any instructions you may leave.

Thanks!

There is nothing built-in for handling equations. I would imagine that the best way of doing this would be using an application designed to produce nicely typeset formulas, and then export them as static graphics which could then be included in the draft text. There are some basic baseline adjustment typesetting functions, but for anything more complex than “n squared” that’s not really doing to do the trick!

If you are at all familiar with LaTeX, and its high quality equation rendering, you might be interested to know that there is a way of producing nearly 100% ready-to-print .tex files out of Scrivener, using the MultiMarkdown system. If that is something that at all interests you, let me know and I’ll collect some links for getting started with that. Of course, if you are really familiar with LaTeX, you could also just compose it right in Scrivener, and compile as plain-text.

I do know my way around latex pretty well. I haven’t willingly used word in the better part of a decade. This was actually one of the features that really drew me to this application. I can send out doc files to my word dependent collaborators, while still keeping the basic mark-up in a latex friendly package. The links for doing latex within scrivener would be nice. It would be interesting to know if there was a way to have TeX-Shop or some similar application opened by Scrivener after the MMD export, do you know how that might be done? One would also wonder if there was a clean way to pass latex arguments to something like LaTeX-it during word format draft compiles. (i.e. set some kind of flag before and after a latex equation block that would be read by scrivener, which would then pass the code block to latex-it, receive the resulting equation image, and place it in the word document in place of the code block.) Any thoughts?

Thanks for the help!

You’ll find that the RTF generator for MMD is fairly simplistic. Technically, what it does is generate an XHTML file, and then run it through the [b]textutils[/b] command-line tool to produce an RTF, which has a heavy emphasis on appearance over structure. It will thus have all of the weaknesses of HTML (no real footnotes, etc), and in my experience nearly always requires a little clean-up, coming out of Scrivener. Assuming your colleagues do not require advanced RTF features, though, it is a nice way to produce quick rough-drafts out of a source that can also produce tex files.

I would myself use Hazel to accomplish this, since I already own it. Hazel, like AppleScript bound to a folder, can monitor a location and act on files according to various criteria. It would be pretty simple to create a Hazel script that monitors a known Scrivener export location for “.tex” extensions and opens them in TeXShop. The only drawback to Hazel is that it has a slight delay, which is fine for most tasks, but if you are expecting to have a file open in TeXShop immediately after compile quits, that won’t usually happen.

Otherwise, a simple AppleScript attached to a dedicated export folder is all you need, and that solution is free. More details here, with solutions from simple to complex, including Growl support, Rake automation, and even straight Scrivener Compile button to a typeset PDF in a single click.

For mixing ad hoc LaTeX with MMD, read this thread.

Sticking with pure MMD, equations can be typed into MMD using the ASCIIMathML syntax:

[code]<< e^(i pi) + 1 = 0 [Euler’s identity]>>

<< x_(1,2) = (-b±sqrt(b^2-4ac))/(2a) [quadratic equation solution]>>

You can also include formulas within a sentence, such as
<<x^2 + y^2 = 1>>. You can then make a reference to
[Euler’s identity].[/code]

(From the MMD documentation.)

Anything in double angle-brackets is ASCIIMathML, with the exception of the optional square-bracket item on the end, which will become a [b]\label[/b] reference.

That’s an interesting idea, but unfortunately there aren’t any hooks into the compile process at all, and in fact the Apple DOC exporter is a bit of a black box, so it would be very difficult to modify it to provide this level of support.

A great big “assuming” here, but assuming that LaTeXiT can take command-line arguments:

What would be feasible with a little mucking around in the MMD source code and XSLT converters, is modifying the MMD->RTF workflow so that LaTeXiT images are rendered in the compile process, placed into the export folder, and then linked as images in the XHTML. This could then be opened in OpenOffice and saved out as a Word document (the MMD->RTF export doesn’t do images). So one extra step, but not too bad considering all that would be going on.

As far as I know, no one has attempted anything like that, so I have no links for you there. Knowledge of Perl and XSL would be required to pull this off. If you went this route, I would recommend creating an MMD meta-data option to trigger this behaviour, as it would greatly increase Compile duration. Creating a new option is simple. You don’t have to do anything special to make a new one, you just insert a key and value into the [b]File/MultiMarkdown Settings[/b] dialogue, like “ExportOption”, “LateXiT_integration”. Then modify the core Perl script to look for that option and add extra handling in the switch case which finds and handles equations.

That’s how I would approach it. I tried to think of a solution involving XSLT alone, but the fundamental problem is that you need to somehow invoke LaTeXiT from the shell, and XSL doesn’t have a facility for that.

Finally, for a pure LaTeX and Scrivener workflow, I would recommend this thread.

The main thing you want to watch for, of course, is keeping Scrivener from generating automated content (in the first tab of compile). I will say this much though, using straight LaTeX in Scrivener will be a lot better in 2.0. The ability to insert code around generated content (such as titles) with hierarchy depth-sensitive triggers, between various section joints, and so on will make it much easier to keep the dynamic aspects of Scrivener truly dynamic. For now though, you’ll have to define headings manually.

P.S. Is that “UO” the University of Oregon by any chance?

Go Ducks! :smiley: … Ahem, and thanks, this gives me a lot to think about. My coding skills are pretty weak (outside of La/TeX), so I’ll have to just use the other work flows you indicated. I am really looking forward to the 2.0 release, especially the comment support and comparison features. With those features in place I think that I’ll finally have something of a reasonable solution to the this whole Word/LaTeX issue.

Thanks!

Here’s another thought on how to collaborate with your colleagues: You could use a LaTeX based workflow and convert MMD’s .tex output with latex2rtf or TeX4HT/OOlatex. I use the following TeXShop script (saved as ~/Library/TeXShop/Engines/TeX4ht-ODF.engine) to create ODT files for OpenOffice:

#!/bin/tcsh set fbName = `basename "$1" .tex` htlatex "$1" "xhtml,ooffice" "ooffice/! -cmozhtf" "-coo" open "${fbName}.odt"

and the following to convert LaTeX to HTML:

#!/bin/tcsh htlatex "$1" "xhtml,charset=utf-8" " -cunihtf -utf8"

so, I’m not quite sure that this is the right spot for this, if one of the moderators wants to move it some where more appropriate, thats cool with me. But since this is a discussion that has migrated towards LaTeX a little and I can’t find anything about this elsewhere I thought I’d ask this here.

I keep getting a bunch of symbols (specifically a cluster of “,” and some accented letters, usually in 2’s or 3’s) and “\ensuremath{\sim}” commands on exporting to LaTeX through MMD. I was wondering if this has been seen before and what characters might be causing the confusion with MMD.

Thanks!

MMD can be a little fragile when encountering special characters, though usually you’ll just get an error during compile. It is usually safest to use HTML entities for such. In order to help diagnose what is happening, could you paste in several stages of the problem? Here is what would help:

  1. Paste a portion of text that is causing the problem
  2. In a code block here, paste the intermediate MMD from that same area. To do this, compile the manuscript using the plain MultiMarkdown export format.
  3. Finally, paste into another code block, the resulting LaTeX

Here are the code snippets and the scrivener source. Let me know what you think.

Scrivener Entry
Depositions were performed on commercially available (001)-oriented silicon substrates to a minimum target thickness of ≈50 nm. Samples were annealed on an custom fabricated hot plate which was allowed to equilibrate at temperature for 15 min before each heat treatment. Annealing was conducted under a high-purity nitrogen atmosphere (O2 content of ≈ 500 nL/L) at 600C for 1 h. The resulting thin films were then analyzed using electron probe microanalysis (EPMA), X-ray diffraction (XRD) and reflectivity (XRR), and aberration corrected scanning transmission electron microscopy (STEM). Powder samples were prepared by depositing films of ≈ 150 nm on polymethymethacrylate (PMMA) coated substrates. The powder films were then lifted off using an acetone bath followed by filtration to collect the powder. Nominal float yields were ≈ 4 mg of powder. Powders were annealed under identical conditions to the substrate supported samples.

MMD Code

Depositions were performed on commercially available (001)-oriented silicon substrates to a minimum target thickness of  ≈50 nm. Samples were annealed on an custom fabricated hot plate which was allowed to equilibrate at temperature for 15 min before each heat treatment.  Annealing was conducted under a high-purity nitrogen atmosphere (O2 content of  ≈ 500 nL/L) at 600C for 1 h. The resulting thin films were then analyzed using electron probe microanalysis (EPMA), X-ray diffraction (XRD) and reflectivity (XRR), and aberration corrected scanning transmission electron microscopy (STEM).  Powder samples were prepared by depositing films of  ≈ 150 nm on polymethymethacrylate (PMMA) coated substrates.  The powder films were then lifted off using an acetone bath followed by filtration to collect the powder.  Nominal float yields were  ≈ 4 mg of powder.  Powders were annealed under identical conditions to the substrate supported samples.

LaTeX Code

Depositions were performed on commercially available (001)-oriented silicon substrates to a minimum target thickness of\ensuremath{\sim} ≈50 nm. Samples were annealed on an custom fabricated hot plate which was allowed to equilibrate at temperature for\ensuremath{\sim}15 min\ensuremath{\sim}before each heat treatment.\ensuremath{\sim} Annealing was conducted under a high-purity nitrogen atmosphere (O2\ensuremath{\sim}content of\ensuremath{\sim} ≈\ensuremath{\sim}500 nL/L) at\ensuremath{\sim}600C\ensuremath{\sim}for\ensuremath{\sim}1 h. The resulting thin films were then analyzed using electron probe microanalysis (EPMA), X-ray diffraction (XRD) and reflectivity (XRR), and aberration corrected scanning transmission electron microscopy (STEM).\ensuremath{\sim} Powder samples were prepared by depositing films of\ensuremath{\sim} ≈\ensuremath{\sim}150 nm\ensuremath{\sim}on polymethymethacrylate (PMMA) coated substrates.\ensuremath{\sim} The powder films were then lifted off using an acetone bath followed by filtration to collect the powder.\ensuremath{\sim} Nominal float yields were \ensuremath{\sim}≈\ensuremath{\sim}4 mg\ensuremath{\sim}of powder.\ensuremath{\sim} Powders were annealed under identical conditions to the substrate supported samples.

Apologies for the delay in response here. It looks like there are two problems going on. The first is that the encoding is getting messed up some time in between compiled and LaTeX. You can see a lot of garbage characters inserted wherever the Unicode characters were in the original document. Scrivener exports UTF-8, and when I use it to generate a LaTeX file directly out of compile, the symbols come through into the final file okay.

Therein lies the second problem. The clean-text routine for MMD is somewhat simplified, handling only the most common symbols (adding the many thousands of possibilities would cause the export to become very slow), and \approx is not one of the symbols being handled properly. I was able to fix this really easily by adding a clause to the clean-text.xslt file in the MMD/XSLT folder. This can be done by adding a pair of calls to the top of the section which declares the “clean-text-utility” template.

<xsl:call-template name="replace-substring"> <xsl:with-param name="original">

Then, at the bottom, adding:

	</xsl:with-param>
	<xsl:with-param name="substring">
		<xsl:text>≈</xsl:text>
	</xsl:with-param>
	<xsl:with-param name="replacement">
		<xsl:text>$ \approx $</xsl:text>
	</xsl:with-param>
</xsl:call-template>

After making that change, I was able to go straight from Scrivener to LaTeX to PDF with no glitches and a nice looking result, with the provided sample text.

But first you’ll need to figure out why Unicode isn’t working in the first place. It might just be as simple as the text editor you are using to open the MD file not correctly identifying it as UTF-8. Hopefully it is that simple, otherwise this could be more difficult to figure out.

One thing I would try first is going straight to LaTeX from Scrivener, with the above patch, and see if that solves it.

Just for the sake of completeness, here is what that section of the XSLT should look like. Don’t paste all of this in—this is just to verify everything is opened and closed correctly:

		</xsl:call-template>
			</xsl:with-param>
			<xsl:with-param name="substring">
				<xsl:text>&#8593;</xsl:text>
			</xsl:with-param>
			<xsl:with-param name="replacement">
				<xsl:text>${\displaystyle \uparrow}$</xsl:text>
			</xsl:with-param>
		</xsl:call-template>
			</xsl:with-param>
			<xsl:with-param name="substring">
				<xsl:text>&#8230;</xsl:text>
			</xsl:with-param>
			<xsl:with-param name="replacement">
				<xsl:text>{\ldots}</xsl:text>
			</xsl:with-param>
		</xsl:call-template>
			</xsl:with-param>
			<xsl:with-param name="substring">
				<xsl:text>≈</xsl:text>
			</xsl:with-param>
			<xsl:with-param name="replacement">
				<xsl:text>$ \approx $</xsl:text>
			</xsl:with-param>
		</xsl:call-template>
	</xsl:template>

</xsl:stylesheet>

Okay, where is the file that I need to edit? I can’t seem to find it.


Edit: Scratch that… I figured out that I needed to install the separate MMD distro. BTW, there isn’t any application to install at the link in the help entry for MMD. Unless you mean git there doesn’t seem to be any application anymore.

I also figured out that TeXShop was set up for a different encoding (Western, Mac OS X) which was probably causing the other problem.

Yeah, I had a feeling that was the problem with TeXShop. Let me know if the above patch sorts things out.

The git version is the best way to get MMD these days. For a while you could get the zip file separately, but I think Fletcher has switched everything over to git because that makes it easier to keep things up to date. Just unarchive that into into your Application Support folder. In the “MultiMarkdown” directory it creates, you’ll find an XSLT folder, which as the “clean-text.xslt” file you need to add the approx command to.

It did indeed. I really like this MMD thing. I might have to delve further into it. Since you seem to know your way around git, could you post the command line to do that operation? Or point me at the thread that has it?

Which operation, unarchiving it? You can just double click on the zip file and move the folder into Application Support for that. If you wanted to use the command line though, it is unzip FILENAME.

The nice thing is that Scrivener should check that spot first before using its internal copy, so any changes you make there should show up in anything you export from Scrivener without any need for the command line.

If you want to write in markdown, using LaTeX (or ConTeXt) for equations, and generate documents that are friendly for non-LaTeX users, you should look into using pandoc http://johnmacfarlane.net/pandoc/ instead of MMD. It generates ODT and RTF output (and lots of other formats, now including EPUB!) directly, not via XHTML. It’s ODT support, in particular, is quite good.

Until a few months ago, it was hard to install, because it depends on the Haskell platform http://hackage.haskell.org/platform/, and the Mac version of the Haskell platform wouldn’t install cleanly. But the Haskell platform now installs easily and cleanly, so that shouldn’t be seen as a barrier anymore.

(Don’t be tempted to install using macports. That version is out of date and will only lead to trouble.)

David

Nope, I was more interested in a workflow that allowed me to collaborate with my co-authors in word and then generate a LaTeX code that I could then refine for my final submissions. It does look interesting though.

Doesn’t Pandoc read MMD syntax files? I don’t know much about it, though I’ve seen it mentioned a few times before. If it can, then someone would still be able to use Scrivener’s MMD tools to produce files that they could then post-process in Pandoc if they pleased.