You can use Splice
, which has been superseded by FileTemplate
since v10.
[ As of 12.1 Splice
has an entirely different meaning! ]
The way it works, although all of the documentation is now gone (except that a tutorial still exists tutorial/SplicingWolframLanguageOutputIntoExternalFiles
), is that in <* these kinds of matched star-angle-bracket delimiters *>
you can put Mathematica code. Then invoke Splice[input_file, output_file]
(and don't make them the same file!) In the few times I ever used this I called the file I wrote [whatever].mma.tex
and the output [whatever].tex
. Then run $\LaTeX$ on [whatever].tex
as usual.
Here is an example file, example.mma.tex
:
\documentclass[11pt]{article}
\usepackage{graphicx}
\author{evanb}
\title{Splicing in Mathematica}
\date{\today}
% Set myPath to "/path/to/directory/containing/example.mma.tex"
% <* myPath = "~/Desktop/mma_splice"; *>
\begin{document}
\maketitle
Here we go testing the splicing ability of Mathematica:
% Set up some variables that Mma sees:
% <* a = 3; b = 4; c = 5; *>
One example of a pythagorean triplet is <*{a,b,c}*> because we see that <*a^2*>+<*b^2*> is equal to <*a^2+b^2*>, which is the square of <*c*>. That is,
\begin{equation}
a^{2}+b^{2}=c^{2}
\end{equation}
corresponds to
\begin{equation}
<*a*>^{2}+<*b*>^{2} = <*a^2*> + <* b^2 *> = <* c^2 *> = <*c*>^{2}.
\end{equation}
You can also dynamically generate polynomials and otherwise do awesome things. Here is something that \emph{used} to work but doesn't anymore$\ldots$
% <*poly = x^2 + 4x + 4 *>
My polynomial is $<* poly *>$. It factors into
\begin{equation}
<*poly*>=<*Factor[poly]*>.
\end{equation}
But we can make plots programmatically:
% <* Export[myPath<>"/polyPlot.png",Plot[poly,{x,-3,3}]]; *>
\begin{figure}[htbp]
\centering
\includegraphics{polyPlot}
\caption{A plot of $<*poly*>$. You can see its zero at <*Union[Select[x/.(Solve[poly==0,x]),-3<#<3&]]*>. \label{}}
\end{figure}
\end{document}
When, from a notebook I invoke Splice["~/Desktop/mma_splice/example.mma.tex", "~/Desktop/mma_splice/example.tex"]
I wind up with a plot, polyPlot.png
(because of my Export
invocation), and a tex file, example.tex
:
\documentclass[11pt]{article}
\usepackage{graphicx}
\author{evanb}
\title{Splicing in Mathematica}
\date{\today}
% For exporting the figure below, set myPath to "/path/to/directory/containing/example.mma.tex"
% Null
\begin{document}
\maketitle
Here we go testing the splicing ability of Mathematica:
% Set up some variables that Mma sees:
% Null
One example of a pythagorean triplet is List(3,4,5) because we see that 9+16 is equal to 25, which is the square of 5. That is,
\begin{equation}
a^{2}+b^{2}=c^{2}
\end{equation}
corresponds to
\begin{equation}
3^{2}+4^{2} = 9 + 16 = 25 = 5^{2}.
\end{equation}
You can also dynamically generate polynomials and otherwise do awesome things. Here is something that \emph{used} to work but doesn't anymore$\ldots$
% 4 + 4*x + Power(x,2)
My polynomial is $4 + 4*x + Power(x,2)$. It factors into
\begin{equation}
4 + 4*x + Power(x,2)=Power(2 + x,2).
\end{equation}
But we can make plots programmatically:
% Null
\begin{figure}[htbp]
\centering
\includegraphics{polyPlot}
\caption{A plot of $4 + 4*x + Power(x,2)$. You can see its zero at List(-2). \label{}}
\end{figure}
\end{document}
which can then be run through pdflatex example.tex
to produce a PDF (which I can't seem to embed here, but imgur uploaded it OK: https://i.stack.imgur.com/6tyqx.jpg ). I also can't get TeXForm
or anything to work in a way that I expect. This used to work better, I think? Anyway, you may be better off learning how to use FileTemplate
.
Rather than opening a notebook to invoke Splice
, I would wager you could do it via invoking Mathematica on the command line---shouldn't be too tough.