How to use FileTemplate instead of Splice that has been superseded?

6

Having invoked Splice[main.mtex],

% main.mtex

\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{amsmath}
% <* f[x_] := x^2 - 5 x +6 *>
\begin{document}
\section{Problem}
Evaluate $ <* f[x] *> $ for $x=\frac 1 2$.

\section{Solution}
$$ <* f[1/2] *> $$

\end{document}

we have main.tex

% main.mtex

\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{amsmath}
% \text{Null}
\begin{document}
\section{Problem}
Evaluate $ x^2-5 x+6 $ for $x=\frac 1 2$.

\section{Solution}
$$ \frac{15}{4} $$

\end{document}

that will produce the following output when compiled with pdflatex.

enter image description here

Question

How to use FileTemplate instead of Splice that has been superseded by FileTemplate?

Money Oriented Programmer

Posted 2016-08-07T02:03:36.267

Reputation: 1 420

Oops, yes, I realize that now. Your post came through the review queue, and I neglected to look carefully before posting the boilerplate greeting. Nonetheless, welcome to the site! :) – Michael E2 – 2016-08-07T02:35:37.093

for completeness you should include the expression for f in the question. I never used splice but I`m puzzled how you ended up with a decimal from exact input. – george2079 – 2016-08-07T13:34:24.017

@george2079: You should try it first. – Money Oriented Programmer – 2016-08-07T14:23:53.800

Answers

4

the essential equivalent of Splice is:

 FileTemplateApply[FileTemplate["main.mtxt"], "main.txt"]

which produces:

 % main.mtex
\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{amsmath}
% Null
\begin{document}
\section{Problem}
Evaluate $ Plus[6, Times[-5, x], Power[x, 2]] $ for $x=\frac 1 2$.
\section{Solution}
$$ 3.75 $$
\end{document}

evidently its applying FullForm to the first expression and CForm to the numeric result, while Splice seems to use CForm for both.

this seems to fix the formatting:

FileTemplateApply[
 FileTemplate["main.mtxt", 
  InsertionFunction -> (ToString[InputForm[#]] &)],"main.txt"]
 % main.mtex
\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{amsmath}
% Null
\begin{document}
\section{Problem}
Evaluate $ 6 - 5*x + x^2 $ for $x=\frac 1 2$.
\section{Solution}
$$ 15/4 $$
\end{document} 

better yet..

insert[v_Rational] := 
 TemplateApply[
  StringTemplate["\\frac{`1`}{`2`}"], {Numerator[v], Denominator[v]}]
insert[v_] := ToString[TraditionalForm[v]]
FileTemplateApply[FileTemplate["main.mtxt",
    InsertionFunction :> insert],"main.txt"]
% main.mtex
\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{amsmath}
% Null
\begin{document}
\section{Problem}
Evaluate $ x^2-5 x+6 $ for $x=\frac 1 2$.
\section{Solution}
$$ \frac{15}{4} $$
\end{document} 

Be aware TraditionalForm will have undesirable results if the output wants to be multi-line, InputForm is safer

george2079

Posted 2016-08-07T02:03:36.267

Reputation: 37 858

An even better option would be to use TeXForm as in insert[v_] := ToString[TeXForm[v]]. This handles all special formatting associated with LaTeX and behaves, AFAIK, exactly the same as Splice. – Henrik Hansen – 2018-02-06T07:32:51.283