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: http://imgur.com/a/yFRMM ). 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.

Interesting question. You can also do what you are asking with R through Sweave and knitr. Two years ago I found the use of R in LyX documents (through knitr) very convenient, but since then I moved to using almost exclusively the R-Markdown-Pandoc combination provided by RStudio.

– Anton Antonov – 2017-07-15T18:26:50.6577

It is also possible to call python from latex and have the output go into the latex. Here is example However, all these methods got it all the wrong way around, and they really do not work at all well in practice. The correct way to do all this is reverse the roles. Generate Latex from the program itself. Not call the program from Latex. Why? Because the Latex environment itself is not suited for using it as the container of such complex tasks. LaTex It is very fragile. Only lua can really be used with Lualatex with ease. IMHO ofcourse :)

– Nasser – 2017-07-15T20:12:20.7304.. what I mean is that if you could find some magic way to call Mathematica from Latex, it will not be practical use and will not scale well.... – Nasser – 2017-07-15T20:21:13.253

2

Related from TeX.SX: Automatically generated plots with Mathematica.

– jkuczm – 2017-07-23T13:25:03.550Is this what you're after? This calls W|A and Wolfram cloud: https://devpost.com/software/alphatex

– Chip Hurst – 2017-07-31T03:06:35.273AlphaTeX is similar to what I'm after. However, unless I'm mistaken, it seems to call the online service Wolfram Alpha and not the kernel of any local copy of

Mathematica. – murray – 2017-07-31T22:29:56.157I've examined the docs that come with alphatex and haven't a clue as to where the package

`cloud.m`

should be located or how it is loaded. – murray – 2017-08-01T16:09:30.183And running

`pdflatex --shell-escape medoc.tex`

with a`\usepackage{latex alpha}`

and the body`$\WolframAlpha{biggest city in U.S.}$`

causes an error of a missing`$`

, with no return from W|A included in the resulting .pdf. (Yes,`curl`

is installed on my Mac.) – murray – 2017-08-01T16:19:33.973