In order to let other users with Mathematica 12.x test this problem for me, I now use FileTemplate instead of Splice.

I want to splice (with FileTemplate) the following file that has been simplified for the sake of illustration purpose only.

% input.mtex



   students = {{"Andy", 2}, {"Bob", 3}, {"Cindy", 5}, {"David", 1}, {"Eve", 9}};


	StringTemplate["\\item `name` can run at \\SI{`speed`}{\\meter\\per\\second}."][<|"name" -> #[[1]], "speed" -> #[[2]]|>] & /@ students


The preamble of LaTeX document has been trimmed in the code that follow. It is just for simplicity.

Unexpected Output

	\item  can run at \SI{}{\meter\per\second}.[<|name -> Andy, speed -> 2|>], 
	\item  can run at \SI{}{\meter\per\second}.[<|name -> Bob, speed -> 3|>], 
	\item  can run at \SI{}{\meter\per\second}.[<|name -> Cindy, speed -> 5|>], 
	\item  can run at \SI{}{\meter\per\second}.[<|name -> David, speed -> 1|>], 
	\item  can run at \SI{}{\meter\per\second}.[<|name -> Eve, speed -> 9|>]

Expected Output

    \item Andy can run at \SI{2}{\meter\per\second}.
    \item Bob can run at \SI{3}{\meter\per\second}.
    \item Cindy can run at \SI{5}{\meter\per\second}.
    \item David can run at \SI{1}{\meter\per\second}.
    \item Eve can run at \SI{9}{\meter\per\second}.


What is the proper way to splice it to get the expected output?

Update by Szabolcs:

The unexpected output seems to be due to the nesting of templates and can be reproduced with these smaller examples:

In[1]:= StringTemplate["x<* StringTemplate[\"a``b\"][123] *>y"][]
Out[1]= "xab[123]y"

In[2]:= TemplateApply[TemplateExpression[StringTemplate["a``b"][123]]]
Out[2]= "ab"[123]

Out[1] should be xa123by and Out[2] should be a123b.

  • $\begingroup$ Is there any reason you don't want to use FileTemplate? Splice has been changed in 12.1. People who have 12.1 cannot even test this for you. $\endgroup$
    – Szabolcs
    May 8, 2020 at 8:54
  • $\begingroup$ You failed to quote backslashes in strings. Can you fix that first please? $\endgroup$
    – Szabolcs
    May 8, 2020 at 12:03
  • 1
    $\begingroup$ @Szabolcs: Is there a better way to obtain the expected output than what I did above? Actually I don't like quoting the TeX contents as a string, especially with escape sequence double backslashes. Readability is the first priority. $\endgroup$ May 8, 2020 at 12:50
  • 1
    $\begingroup$ A more minimal example of the problem is the following: StringTemplate["<* StringTemplate[\"``\"][123] *>"][]. The output is "[123]". I would expect "123" (without the [ and ]). It must be somehow related to the nesting of templates ... $\endgroup$
    – Szabolcs
    May 8, 2020 at 13:05
  • 1
    $\begingroup$ I am sorry, I give up ... I would say that this is either due to a bug (shouldn't happen) or nested templates are not supported (which should have been documented, and preferably protected against). Of course, it is a very reasonable question to ask how to splice the output into such a template of a TeX file. $\endgroup$
    – Szabolcs
    May 8, 2020 at 13:17


Your Answer

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

Browse other questions tagged or ask your own question.