# Concatenating strings, unevaluated expressions and evaluated expressions/values

Maybe this is something very simple to some of you. Anyway, I think this can be useful to many others like me.

I would like to build an output that involves strings, unevaluated expressions and evaluated expressions/values —something like this:

The value for $C_\mathrm{p}$ is $1.5$ and the value for $\delta^*$ is $0.123$.

where 1.5 and 0.123 are the result of evaluating respective expressions.

This is what I tried:

Cp = 1.5; deltastar = 0.123;
HoldForm["The value for " HoldForm[Subscript[C, p]] " is " Evaluate[
Cp] " and the value for " HoldForm[
Superscript[\[Delta], "*"]] " is " Evaluate[deltastar] "."]


With this result: The outer HoldForm is necessary to maintain the order of the pieces, that are actually considered by Mathematica as being multiplied. For instance, this piece of code:

Cp = 1.5; deltastar = 0.123;
"The value for " HoldForm[Subscript[C, p]] " is " Evaluate[
Cp] " and the value for " HoldForm[
Superscript[\[Delta], "*"]] " is " Evaluate[deltastar] "."


yields to this output: Using ToString is not an option when there are superscripts and subscripts, as it is the case (I mean, the raw string output is awful). And because of that, I can't use string concatenation 😕, which would be the most natural approach to me.

There should be an easy way to do this in Mathematica, shouldn't there?

As I said, I guess there actually is a simple solution for this —that is, properly building an output text or message that involves strings, unevaluated expressions and evaluated expressions. Could you help me, please?

It sounds like you're merely looking for Row:

Cp = 1.5; deltastar = 0.123;

Row[{
"The value for ",
HoldForm[Subscript[C, p]],
" is ",
Cp,
" and the value for ",
HoldForm[Superscript[\[Delta], "*"]],
" is ",
deltastar,
"."
}] If this does not work for you please clearly state how it fails so that those issues can be directly addressed.

Looks like StringForm can achieve this:

Cp = 1.5; deltastar = 0.123;


Then:

StringForm["The value for 1 is 2 and the value for 3 is 4.",
HoldForm @ Subscript[C, p], Cp, HoldForm @ Superscript[\[Delta], "*"], deltastar] • It is worth noting that NumberMarks are displayed with this method. (e.g. "1.5") Using NumberMarks -> False does not seem to change this. – Mr.Wizard Sep 12 '14 at 13:42

In V10 there is a new function StringTemplate that allows us to build custom formatting functions in a new way. Here is how it can applied to the OP's problem.

fmt[args__] :=
Style[
StringTemplate[
"The value for 1 is 2 and the value for 3 is 4.",
CombinerFunction -> Row
][args],
"SR"]

cpForm = HoldForm@Subscript[C, p]; Cp = 1.5;
deltaForm =  HoldForm[Superscript[δ, "*"]]; deltastar = 0.123;
fmt[cpForm, Cp, deltaForm, deltastar] Cp = 1.3; deltastar = 0.42;
fmt[cpForm, Cp, deltaForm, deltastar] Note that the TemplateObject returned by StringTemplate acts like a function.

a = 1.5;
b = 0.123;

Grid[{{"The value for", RawBoxes[SubscriptBox["c", "p"]], "is", a,
"and the value for", RawBoxes[SuperscriptBox["\[Delta]", "*"]], "is", b}}] • +1 but I think Row` is easier to use an more "canonical" here. – Mr.Wizard Sep 12 '14 at 11:20