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

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.

# Question

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

• 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 Aug 7 '16 at 2:35
• for completeness you should include the expression for f in the question. I never used splice but Im puzzled how you ended up with a decimal from exact input. – george2079 Aug 7 '16 at 13:34
• @george2079: You should try it first. – Artificial Stupidity Aug 7 '16 at 14:23

## 1 Answer

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

• 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 Feb 6 '18 at 7:32