I would like to use the File -> Save As Special ... -> TeX feature to
save one of my Mathematica notebooks as a *.tex file. Also, I would
like to use the ConversionRules option in a custom cell in a custom
style sheet to directly transfer TeX code into the final document. The
code I am using write now (in the options for the style cell) is this:
ConversionRules:> "TeX" -> { {"", Identity, ""}, {"", Identity, ""}
}
Which I wrote using this article as a guide:
http://documents.wolfram.com/v5/Built-inFunctions/NewInVersion3.x/InputAndOutput/AdditionalInformation/TeXSave.html
This should (and does, in some cases) put the text that I type in the
cells of the custom style directly into the final *.tex document.
According to the rule, the text in the cell is prefaced by nothing, is
formatted by "Identity" (as opposed to TeXForm), and nothing is placed
after it. The conversion rule works fine when I make standalone cells
of the custom style type, allowing me to put TeX code directly into my
Mathematica notebook and have it saved correctly in the final *.tex
document. However, when I try to make inline cells of the custom style
type (say, in cells of the NumberedEquation style) by using CTRL+9
<typestuff> CTRL+0, the conversion rule does not work on the inline
cell, and I end up with <typestuff> being interpreted as text and
presented as text in the final *.tex document (even if it were code,
for example "\today" ends up being not "May 24, 2006" but is "\today"
after compiling the *.tex document).
Is this error what is meant by "For the cases where TeXSave generates a
custom TeX macro, the delimiters specified in the conversion rule are
overridden, leaving only the formatting function to have an impact on
the output. A typical formatting function used in conversion rules is
TeXForm." on the page to which I linked? If so, I'm not really sure
how I can achieve nested environments consisitently (i.e.
\begin{environment-name} ... \end{environment-name}). It is easy to
nest environments when actually writing a TeX document manually, but
when I nest cells (which correspond to environments, as no lower level
construct has a ConversionRules option) in Mathematica notebooks, the
conversion rules for nested cells do not trigger.
I am hoping that there is some way for this to work.
However, what I am actually trying to implement is just a reference
system by which I can refer to equations by labels rather than their
number (which may or may not change as I add or delete equations from
my document). I would like to mimic the functionality of the
\label{keyname} and \ref{keyname} commands from LaTeX in a Mathematica
notebook, so that when I Save As Special ... -> TeX, my TeX document
will have correct cross-references. Mathematica to some extent already
has an implementation of this in its Labels and Hyperlinks, but I don't
know how to (or if I can) transfer these Labels and Hyperlinks to my
TeX document when I Save As Special ... -> TeX.
If you can help me at all, please leave a message.
Thanks in advance,
Emily
Mathematica's TeXExport can already satisfy questions 1 and 3 in a
straightforward manner by using ConversionRules. However, question 2,
regarding the handling of environments, may require additional
formatting. Both techniques are illustrated below.
Note, Publicon is another Wolfram product that can export to TeX.
Publicon may be easier to use for TeXExport than Mathematica because
Publicon offers templated documents with predefined conversion rules
that correspond directly to specific LaTeX Packages.
Answers to questions:
1. How do you write a ConversionRule that handles both inline and top
level cells independently?
First note that the rhs of a "TeX" ConversionRule must be a List whose
contents are either two scalar, three scalar, or two List elements.
Examples:
a) "TeX" ConversionRule whose rhs is a List of two scalars:
ConversionRules:>{"TeX"->{"\\macroname{", "}"}}
b) "TeX" ConversionRule whose rhs is a List of three scalars:
ConversionRules:>{"TeX"->{"\\macroname{", Identity, "}"}}
c) "TeX" ConversionRule whose rhs is a List of two Lists:
ConversionRules:>{"TeX"->{{"lead text", InlineConversionFunction,
"trail text"},
{"lead text", TopLevelConversionFunction, "trail text"}}}
The format we need to answer question 1 is found in Example c, where
the rhs of the ConversionRule is a List of two Lists. The two inner
List elements define a ConversionRule for inline cells and separate one
for the top level cell, respectively. From Example c, we see the
ConversionRule for inline cells is defined as {"lead text",
InlineConversionFunction, "trail text"}, where the first and third
elements of the list are always strings that are pre- and postpended,
respectively, to the inline conversion. The second element is the
conversion function itself.
The second list is defined as {"lead text", TopLevelConversionFunction,
"trail text"} It works in the same manner as the inline ConversionRule,
but it only acts on the top level cell as a whole.
2. How do you write ConversionRules that will form both syntactically
and functionally appropriate TeX environments?
If the content of the environment comes from only on a single cell,
like a single paragraph abstract, you could use a ConversionRule that
is similar to the following:
Example1--Style definition for "Abstract":
Cell[StyleData["Abstract"],
.
.
.
ConversionRules:>{"TeX"->{"\\begin{abstract}\n","\n\\end{abstract}"}}
]
This ConversionRule,
"TeX"->{"\\begin{abstract}\n","\n\\end{abstract}"}, will produce an
abstract environment that looks like:
\begin{abstract}
Contents of the abstract cell
\end{abstract}
However, if you want to produce a TeX environment that contains
contents from multiple, contiguous cells, the ConversionRule in
Example1 won't work correctly.
Example 2: Assume the "Abstract" style definition from Example 1. Also
assume that the notebook we want export to TeX contains the following
"Abstract" Cells:
Cell["Paragraph1", "Abstract"],
Cell["Paragraph2", "Abstract"],
Cell["Paragraph3", "Abstract"],
Cell["Paragraph4", "Abstract"]
According to the "Abstract" style definition from Example 1, the TeX
conversion rule prepends "\\begin{abstract}\n" to each cell's contents
and postpends "\n\\end{abstract}". Consequently, when we export to TeX,
TeXExport produces the following:
\begin{abstract}
Paragraph1
\end{abstract}
\begin{abstract}
Paragraph2
\end{abstract}
\begin{abstract}
Paragraph3
\end{abstract}
\begin{abstract}
Paragraph4
\end{abstract}
The output above isn't really appropriate. What we really want is the
following:
\begin{abstract}
Paragraph1
Paragraph2
Paragraph3
Paragraph4
\end{abstract}
To achieve the latter without generating an independent environment for
each cell, you will need to insert cells that do not contain content
and that precede and follow the cells to be included in the
environment. We can define these cells as "BeginAbstract" and
"EndAbstract" where their style definitions are:
Cell[StyleData["BeginAbstract"],
.
.
.
ConversionRules:> "TeX"->{"\\begin{abstract}\n",""}}
]
and
Cell[StyleData["EndAbstract"],
.
.
.
ConversionRules:> "TeX"->{"","\n\\end{abstract}"}}
]
and redefine "Abstract", without a specific ConversionRule.
Cell[StyleData["Abstract"],
.
.
.
]
Note, a style definition that does not specify a ConversionRule assumes
TeXExports default conversion. It doesn't mean there won't be any
output when the notebook is exported to TeX.
Using the Cells from earlier in the example, but applying the new
definition of "Abstract" and inserting the newly defined
"BeginAbstract" and "EndAbstract" cells above and below we have:
Cell["", "BeginAbstract"],
Cell["Paragraph1", "Abstract"],
Cell["Paragraph2", "Abstract"],
Cell["Paragraph3", "Abstract"],
Cell["Paragraph4", "Abstract"].
Cell["", "EndAbstract"]
When exported the Abstract environment should look like this.
\begin{abstract}
Paragraph1
Paragraph2
Paragraph3
Paragraph4
\end{abstract}
I will leave it as an exercise to the user to apply this technique to
nesting environments like enumerate or itemize.
3. How do you write ConversionRules that will preserve cross
references?
In Publicon we use ButtonBoxes to handle cross references. Specifically
we use a ButtonBox of style XRef for what becomes \ref{} in the TeX
output. Here is the Style Definition for XRef in the AMS Journal
Stylesheet of Publicon.
Cell[StyleData["XRef"],
ConversionRules:>{"TeX" -> {{"\\ref{",
ExtractButtonData[ "CounterTag"], "}"}, {"", Automatic, ""}}, "HTML"
-> {
"", XRefToHtmlHyperlink, ""}},
StyleMenuListing->None]
Note, we are using a function, ExtractButtonData[], to extract the
"CounterTag" from the Button Data to set the \ref{} macro. Although the
ConversionRule does not explicitly set the \label{} macro it would be a
straightforward Conversion Rule to write. The ConversionRule should
work fine in Mathematica, but you will still need to either define
ExtractButtonData in a package that you load prior to TeXSaving or
write the function code directly in the ConversionRule.
-- David Rogers
Documentation Programmer
Wolfram Research