6
$\begingroup$

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?

$\endgroup$
3
  • $\begingroup$ 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! :) $\endgroup$
    – Michael E2
    Aug 7, 2016 at 2:35
  • $\begingroup$ 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. $\endgroup$
    – george2079
    Aug 7, 2016 at 13:34
  • $\begingroup$ @george2079: You should try it first. $\endgroup$ Aug 7, 2016 at 14:23

1 Answer 1

4
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$ Feb 6, 2018 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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