3
$\begingroup$

I am generating tex files with a "for" loop in Mathematica. In each file I need to have different quantities. So I am defining a variable in Mathematica, which is is evaluated during each loop. I want to paste its value (which actually may be a string) directly in the current tex file.

I have seen this done via the construction

<*variable*>

where "variable" is the Mathematica variable, which may be something like

variable="$$"<>ToString[TeXForm[1/i]]<>"$$"

However I have problems directly applying this construction- it does not replace anything, just prints "<variable>" in the corresponding pdf files, although, I create them via the Mathematica "Run" command (and pdflatex). Is there something more I should do?

$\endgroup$
5
  • $\begingroup$ Hi. You wrote "I have problems directly applying this construction". Like what kind of problems ? $\endgroup$ Commented Nov 20, 2022 at 22:13
  • 2
    $\begingroup$ it does not replace anything Can't really help without having an exact example showing the problem. just prints "<variable>" in the corresponding pdf files how did you save the variable string to the latex file? Need complete code you used, not fragments to see where the problem is. $\endgroup$
    – Nasser
    Commented Nov 20, 2022 at 22:36
  • $\begingroup$ Include code that attempts the replace. $\endgroup$
    – Edmund
    Commented Nov 20, 2022 at 22:44
  • $\begingroup$ @Nasser: I haven't saved it. I mean, I do not know what should be done, I just know that it is possible, but did not manage to find anything about this on the Internet. $\endgroup$ Commented Nov 20, 2022 at 23:04
  • 1
    $\begingroup$ I'll post a basic template example that you can use soon. $\endgroup$
    – Nasser
    Commented Nov 20, 2022 at 23:05

1 Answer 1

7
$\begingroup$

I haven't saved it.

Well, if you do not save the latex to the file, it will not work. Here is a small template to use. This creates index.tex file, solves few integrals and solves an ode and saves the result. This shows how to use variables inside the latex and the final latex file and the PDF generated after compiling the pdf file.

SetDirectory[NotebookDirectory[]]

fileName = "index.tex";
file = OpenWrite[fileName, PageWidth -> Infinity];
ClearAll[x];
integrals = {Sin[x], x*Cos[x], Tan[x]}
toX[expr_] := ToString@TeXForm[expr];
s = "\\documentclass[12pt]{article}
\\usepackage{amsmath}
\\begin{document}
";
WriteString[file, s];
s = "Let us solve few integral problems to show how good Mathematica is
\\begin{align*}
";
WriteString[file, s];
Do[
 currentIntegrand = integrals[[n]];
 If[n < Length[integrals], end = "\\\\ \n", end = "\n"];
 s = "\\int{" <> toX[currentIntegrand ] <> "\\,dx}&=" <> 
   toX[Integrate[currentIntegrand, x ]] <> " + c_1" <> end;
 WriteString[file, s];
 , {n, Length[integrals]}
 ]
s = "\\end{align*}
Mathematica can also solve differential equations. For example for \
the ode ";
WriteString[file, s];
ode = y''[x] + y'[x] + y[x] == 0;
s = "$" <> toX[ode] <> "$ it gives this solution 
\\[
" <> toX[DSolve[ode, y[x], x]] <> "
\\]
Now we can close the file. Good bye.
\\end{document}
"
WriteString[file, s]
Close[file]

After you run the above there will be a file index.tex in same folder.

\documentclass[12pt]{article}
\usepackage{amsmath}
\begin{document}
Let us solve few integral problems to show how good Mathematica is
\begin{align*}
\int{\sin (x)\,dx}&=-\cos (x) + c_1\\ 
\int{x \cos (x)\,dx}&=x \sin (x)+\cos (x) + c_1\\ 
\int{\tan (x)\,dx}&=-\log (\cos (x)) + c_1
\end{align*}
Mathematica can also solve differential equations. For example for the ode $y''(x)+y'(x)+y(x)=0$ it gives this solution 
\[
\left\{\left\{y(x)\to c_2 e^{-x/2} \cos \left(\frac{\sqrt{3} x}{2}\right)+c_1 e^{-x/2} \sin \left(\frac{\sqrt{3} x}{2}\right)\right\}\right\}
\]
Now we can close the file. Good bye.
\end{document}

After you compile it using your Latex compiler, here is the result

enter image description here

You can now adjust the above to your needs.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.