0
$\begingroup$

This answer with some modification can generate a list of trigonometry functions with random angles.

texForm[ComplexInfinity] := "\\infty";
texForm[arg_] := ToString[arg, TeXForm];
mkTex[arg : (f_[HoldForm[Times[n_, Degree]]])] := 
  StringTemplate["`f` `n`^\\circ = `eval`"]@
   Association["f" -> "\\" <> ToLowerCase@SymbolName[f], "n" -> n, 
    "eval" -> texForm[ReleaseHold[arg]]];
mkTex[l_List] := 
  StringRiffle[
   mkTex /@ l, {"\\begin{enumerate}\n\t\\item $\\displaystyle ", 
    "$\n\t\\item $\\displaystyle ", "$\n\\end{enumerate}"}];
args = HoldForm[# Degree] & /@ Array[15 # &, 24, 0];
funcs = {Sin, Cos, Tan, Csc, Sec, Cot};
ques = RandomSample[#, 2] &@(#1@#2 & @@@ Tuples[{funcs, args}]);
mkTex[ques]
\begin{enumerate}
   \item $\displaystyle \csc 240^\circ = -\frac{2}{\sqrt{3}}$
   \item $\displaystyle \cot 120^\circ = -\frac{1}{\sqrt{3}}$
\end{enumerate}

However if I splice

% input.mtex
\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{amsmath}

\usepackage{comment}

\begin{comment}
<* 
texForm[ComplexInfinity] := "\\infty";
texForm[arg_] := ToString[arg, TeXForm];
mkTex[arg : (f_[HoldForm[Times[n_, Degree]]])] := 
  StringTemplate["`f` `n`^\\circ = `eval`"]@
   Association["f" -> "\\" <> ToLowerCase@SymbolName[f], "n" -> n, 
    "eval" -> texForm[ReleaseHold[arg]]];
mkTex[l_List] := 
  StringRiffle[
   mkTex /@ l, {"\\begin{enumerate}\n\t\\item $\\displaystyle ", 
    "$\n\t\\item $\\displaystyle ", "$\n\\end{enumerate}"}];
args = HoldForm[# Degree] & /@ Array[15 # &, 24, 0];
funcs = {Sin, Cos, Tan, Csc, Sec, Cot};
ques = RandomSample[#, 2] &@(#1@#2 & @@@ Tuples[{funcs, args}]);

*>
\end{comment}


\begin{document}
<* mkTex[ques] *>
\end{document}

with Splice["C:\\input.mtex"], I get the following result that does not make sense.

\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{amsmath}

\usepackage{comment}

\begin{comment}
\text{Null}
\end{comment}


\begin{document}
\text{$\backslash \backslash $begin$\{$enumerate$\}\backslash $n$\backslash
   $t$\backslash \backslash $item $\$\backslash \backslash $displaystyle
   $\backslash \backslash $cot 15${}^{\wedge}\backslash \backslash $circ =
   2+$\backslash \backslash $sqrt$\{$3$\}\$\backslash $n$\backslash
   $t$\backslash \backslash $item $\$\backslash \backslash $displaystyle
   $\backslash \backslash $sin 120${}^{\wedge}\backslash \backslash $circ =
   $\backslash \backslash $frac$\{\backslash \backslash
   $sqrt$\{$3$\}\}\{$2$\}\$\backslash $n$\backslash \backslash
   $end$\{$enumerate$\}$}
\end{document}

Question

How to make the spliced output as follows

\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{amsmath}

\usepackage{comment}

\begin{comment}
\text{Null}
\end{comment}


\begin{document}
\begin{enumerate}
   \item $\displaystyle \csc 240^\circ = -\frac{2}{\sqrt{3}}$
   \item $\displaystyle \cot 120^\circ = -\frac{1}{\sqrt{3}}$
\end{enumerate}
\end{document}
$\endgroup$
  • $\begingroup$ So this is a question about outputting in LaTeX, not actually about generating random angles, right? Consider changing your title to reflect your actual question. $\endgroup$ – march Mar 4 at 18:27
  • 1
    $\begingroup$ Thanks for the quick change! $\endgroup$ – march Mar 4 at 18:30
1
+50
$\begingroup$

It you must include everything inside of the actual template itself, you could use the following for input.mtex:

% input.mtex
\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{amsmath}

\usepackage{comment}

\begin{comment}
<* 
texForm[ComplexInfinity] := "\\infty";
texForm[arg_] := ToString[arg, TeXForm];
mkTex[arg : (f_[HoldForm[Times[n_, Degree]]])] := 
  Inactive[StringTemplate]["`f` `n`^\\circ = `eval`"]@
   Association["f" -> "\\" <> ToLowerCase@SymbolName[f], "n" -> n, 
    "eval" -> texForm[ReleaseHold[arg]]];
mkTex[l_List] := 
  StringRiffle[
   Activate@*mkTex /@ l, {"\\begin{enumerate}\n\t\\item $\\displaystyle ", 
    "$\n\t\\item $\\displaystyle ", "$\n\\end{enumerate}"}];
args = HoldForm[# Degree] & /@ Array[15 # &, 24, 0];
funcs = {Sin, Cos, Tan, Csc, Sec, Cot};
ques = RandomSample[#, 2] &@(#1@#2 & @@@ Tuples[{funcs, args}]);

*>
\end{comment}


\begin{document}
<* mkTex[ques] *>
\end{document}

Note the use of Inactive and Activate. I used these to make sure that the StringTemplate (which is inside of another template) gets evaluated when I want.

I would use FileTemplate and FileTemplateApply rather than Splice.

And then apply:

t = FileTemplate[File["/path/to/file/input.mtex"]];
FileTemplateApply[t, None, File["/path/to/file/output.tex"]]

which results in

% input.mtex
\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{amsmath}

\usepackage{comment}

\begin{comment}
Null
\end{comment}


\begin{document}
\begin{enumerate}
	\item $\displaystyle \cos 255^\circ = -\frac{\sqrt{3}-1}{2 \sqrt{2}}$
	\item $\displaystyle \sin 180^\circ = 0$
\end{enumerate}
\end{document}  
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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